MediaRecorder 類是 Android 提供的用來錄製視訊(Video)和音訊(Audio)的元件,MediaRecorder 和 MediaPlayer API 提供了經過更高層抽象和封裝介面,直接對檔進行操作,而且他倆功能更豐富,同時支援音訊和視頻。
MediaPlayer State Diagram |
Nested classes |
Description |
|
class |
MediaPlayer.DrmInfo |
Encapsulates the DRM properties of the source. |
class |
MediaPlayer.MetricsConstants |
|
class |
MediaPlayer.NoDrmSchemeException |
Thrown when a DRM method is called before preparing a DRM scheme through prepareDrm(). |
interface |
MediaPlayer.OnBufferingUpdateListener |
Interface definition of a callback to be invoked indicating buffering status of a media resource being streamed over the network. |
interface |
MediaPlayer.OnCompletionListener |
Interface definition for a callback to be invoked when playback of a media source has completed. |
interface |
MediaPlayer.OnDrmConfigHelper |
Interface definition of a callback to be invoked when the app can do DRM configuration (get/set properties) before the session is opened. |
interface |
MediaPlayer.OnDrmInfoListener |
Interface definition of a callback to be invoked when the DRM info becomes available |
interface |
MediaPlayer.OnDrmPreparedListener |
Interface definition of a callback to notify the app when the DRM is ready for key request/response |
interface |
MediaPlayer.OnErrorListener |
Interface definition of a callback to be invoked when there has been an error during an asynchronous operation (other errors will throw exceptions at method call time). |
interface |
MediaPlayer.OnInfoListener |
Interface definition of a callback to be invoked to communicate some info and/or warning about the media or its playback. |
interface |
MediaPlayer.OnMediaTimeDiscontinuityListener |
Interface definition of a callback to be invoked when discontinuity in the normal progression of the media time is detected. |
interface |
MediaPlayer.OnPreparedListener |
Interface definition for a callback to be invoked when the media source is ready for playback. |
interface |
MediaPlayer.OnSeekCompleteListener |
Interface definition of a callback to be invoked indicating the completion of a seek operation. |
interface |
MediaPlayer.OnSubtitleDataListener |
Interface definition of a callback to be invoked when a player subtitle track has new subtitle data available. |
interface |
MediaPlayer.OnTimedMetaDataAvailableListener |
Interface definition of a callback to be invoked when a track has timed metadata available. |
interface |
MediaPlayer.OnTimedTextListener |
Interface definition of a callback to be invoked when a timed text is available for display. |
interface |
MediaPlayer.OnVideoSizeChangedListener |
Interface definition of a callback to be invoked when the video size is first known or updated |
class |
MediaPlayer.ProvisioningNetworkErrorException |
Thrown when the device requires DRM provisioning but the provisioning attempt has failed due to a network error (Internet reachability, timeout, etc.). |
class |
MediaPlayer.ProvisioningServerErrorException |
Thrown when the device requires DRM provisioning but the provisioning attempt has failed due to the provisioning server denying the request. |
class |
MediaPlayer.TrackInfo |
Class for MediaPlayer to return each audio/video/subtitle track's metadata. |
Constants:
Constants |
Description |
|
int |
MEDIA_ERROR_IO |
File or network related operation errors. |
int |
MEDIA_ERROR_MALFORMED |
Bitstream is not conforming to the related coding standard or file spec. |
int |
MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK |
The video is streamed and its container is not valid for progressive playback i.e the video's index (e.g moov atom) is not at the start of the file. |
int |
MEDIA_ERROR_SERVER_DIED |
Media server died. |
int |
MEDIA_ERROR_TIMED_OUT |
Some operation takes too long to complete, usually more than 3-5 seconds. |
int |
MEDIA_ERROR_UNKNOWN |
Unspecified media player error. |
int |
MEDIA_ERROR_UNSUPPORTED |
Bitstream is conforming to the related coding standard or file spec, but the media framework does not support the feature. |
int |
MEDIA_INFO_AUDIO_NOT_PLAYING |
Informs that audio is not playing. |
int |
MEDIA_INFO_BAD_INTERLEAVING |
Bad interleaving means that a media has been improperly interleaved or not interleaved at all, e.g has all the video samples first then all the audio ones. |
int |
MEDIA_INFO_BUFFERING_END |
MediaPlayer is resuming playback after filling buffers. |
int |
MEDIA_INFO_BUFFERING_START |
MediaPlayer is temporarily pausing playback internally in order to buffer more data. |
int |
MEDIA_INFO_METADATA_UPDATE |
A new set of metadata is available. |
int |
MEDIA_INFO_NOT_SEEKABLE |
The media cannot be seeked (e.g live stream) |
int |
MEDIA_INFO_STARTED_AS_NEXT |
The player was started because it was used as the next player for another player, which just completed playback. |
int |
MEDIA_INFO_SUBTITLE_TIMED_OUT |
Reading the subtitle track takes too long. |
int |
MEDIA_INFO_UNKNOWN |
Unspecified media player info. |
int |
MEDIA_INFO_UNSUPPORTED_SUBTITLE |
Subtitle track was not supported by the media framework. |
int |
MEDIA_INFO_VIDEO_NOT_PLAYING |
Informs that video is not playing. |
int |
MEDIA_INFO_VIDEO_RENDERING_START |
The player just pushed the very first video frame for rendering. |
int |
MEDIA_INFO_VIDEO_TRACK_LAGGING |
The video is too complex for the decoder: it can't decode frames fast enough. |
int |
PREPARE_DRM_STATUS_PREPARATION_ERROR |
The DRM preparation has failed . |
int |
PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR |
The device required DRM provisioning but couldn't reach the provisioning server. |
int |
PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR |
The device required DRM provisioning but the provisioning server denied the request. |
int |
PREPARE_DRM_STATUS_SUCCESS |
The status codes for OnDrmPreparedListener#onDrmPrepared listener. |
int |
SEEK_CLOSEST |
This mode is used with seekTo(long, int) to move media position to a frame (not necessarily a key frame) associated with a data source that is located closest to or at the given time. |
int |
SEEK_CLOSEST_SYNC |
This mode is used with seekTo(long, int) to move media position to a sync (or key) frame associated with a data source that is located closest to (in time) or at the given time. |
int |
SEEK_NEXT_SYNC |
This mode is used with seekTo(long, int) to move media position to a sync (or key) frame associated with a data source that is located right after or at the given time. |
int |
SEEK_PREVIOUS_SYNC |
This mode is used with seekTo(long, int) to move media position to a sync (or key) frame associated with a data source that is located right before or at the given time. |
int |
VIDEO_SCALING_MODE_SCALE_TO_FIT |
Specifies a video scaling mode. |
int |
VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING |
Specifies a video scaling mode. |
String |
MEDIA_MIMETYPE_TEXT_SUBRIP |
This constant was deprecated in API level 28. use MediaFormat#MIMETYPE_TEXT_SUBRIP |
Public methods:
Public methods |
Description |
|
void |
addOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener, Handler handler) |
Adds an AudioRouting.OnRoutingChangedListener to receive notifications of routing changes on this MediaPlayer. |
void |
addTimedTextSource(FileDescriptor fd, String mimeType) |
Adds an external timed text source file (FileDescriptor). |
void |
addTimedTextSource(String path, String mimeType) |
Adds an external timed text source file. |
void |
addTimedTextSource(FileDescriptor fd, long offset, long length, String mime) |
Adds an external timed text file (FileDescriptor). |
void |
addTimedTextSource(Context context, Uri uri, String mimeType) |
Adds an external timed text source file (Uri). |
void |
attachAuxEffect(int effectId) |
Attaches an auxiliary effect to the player. |
void |
clearOnMediaTimeDiscontinuityListener() |
Clears the listener previously set with setOnMediaTimeDiscontinuityListener(android.media.MediaPlayer.OnMediaTimeDiscontinuityListener) or setOnMediaTimeDiscontinuityListener(android.media.MediaPlayer.OnMediaTimeDiscontinuityListener, android.os.Handler) |
void |
clearOnSubtitleDataListener() |
Clears the listener previously set with setOnSubtitleDataListener(android.media.MediaPlayer.OnSubtitleDataListener) or setOnSubtitleDataListener(android.media.MediaPlayer.OnSubtitleDataListener, android.os.Handler). |
static MediaPlayer |
create(Context context, Uri uri, SurfaceHolder holder, AudioAttributes audioAttributes, int audioSessionId) |
Same factory method as create(android.content.Context, android.net.Uri, android.view.SurfaceHolder) but that lets you specify the audio attributes and session ID to be used by the new MediaPlayer instance. |
static MediaPlayer |
create(Context context, int resid, AudioAttributes audioAttributes, int audioSessionId) |
Same factory method as create(android.content.Context, int) but that lets you specify the audio attributes and session ID to be used by the new MediaPlayer instance. |
static MediaPlayer |
create(Context context, Uri uri, SurfaceHolder holder) |
Convenience method to create a MediaPlayer for a given Uri. |
static MediaPlayer |
create(Context context, int resid) |
Convenience method to create a MediaPlayer for a given resource id. |
static MediaPlayer |
create(Context context, Uri uri) |
Convenience method to create a MediaPlayer for a given Uri. |
VolumeShaper |
createVolumeShaper(VolumeShaper.Configuration configuration) |
Returns a VolumeShaper object that can be used modify the volume envelope of the player or track. |
void |
deselectTrack(int index) |
Deselect a track. |
int |
getAudioSessionId() |
Returns the audio session ID. |
int |
getCurrentPosition() |
Gets the current playback position. |
MediaPlayer.DrmInfo |
getDrmInfo() |
Retrieves the DRM Info associated with the current source |
String |
getDrmPropertyString(String propertyName) |
Read a DRM engine plugin String property value, given the property name string. |
int |
getDuration() |
Gets the duration of the file. |
MediaDrm.KeyRequest |
getKeyRequest(byte[] keySetId, byte[] initData, String mimeType, int keyType, Map<String, String> optionalParameters) |
A key request/response exchange occurs between the app and a license server to obtain or release keys used to decrypt encrypted content. |
PersistableBundle |
getMetrics() |
Return Metrics data about the current player. |
PlaybackParams |
getPlaybackParams() |
Gets the playback params, containing the current playback rate. |
AudioDeviceInfo |
getPreferredDevice() |
Returns the selected output specified by setPreferredDevice(AudioDeviceInfo). |
AudioDeviceInfo |
getRoutedDevice() |
Returns an AudioDeviceInfo identifying the current routing of this MediaPlayer Note: The query is only valid if the MediaPlayer is currently playing. |
int |
getSelectedTrack(int trackType) |
Returns the index of the audio, video, or subtitle track currently selected for playback, The return value is an index into the array returned by getTrackInfo(), and can be used in calls to selectTrack(int) or deselectTrack(int). |
SyncParams |
getSyncParams() |
Gets the A/V sync mode. |
MediaTimestamp |
getTimestamp() |
Get current playback position as a MediaTimestamp. |
TrackInfo[] |
getTrackInfo() |
Returns an array of track information. |
int |
getVideoHeight() |
Returns the height of the video. |
int |
getVideoWidth() |
Returns the width of the video. |
boolean |
isLooping() |
Checks whether the MediaPlayer is looping or non-looping. |
boolean |
isPlaying() |
Checks whether the MediaPlayer is playing. |
void |
pause() |
Pauses playback. |
void |
prepare() |
Prepares the player for playback, synchronously. |
void |
prepareAsync() |
Prepares the player for playback, asynchronously. |
void |
prepareDrm(UUID uuid) |
Prepares the DRM for the current source, If OnDrmConfigHelper is registered, it will be called during preparation to allow configuration of the DRM properties before opening the DRM session. |
byte[] |
provideKeyResponse(byte[] keySetId, byte[] response) |
A key response is received from the license server by the app, then it is provided to the DRM engine plugin using provideKeyResponse. |
void |
release() |
Releases resources associated with this MediaPlayer object. |
void |
releaseDrm() |
Releases the DRM session, The player has to have an active DRM session and be in stopped, or prepared state before this call is made. |
void |
removeOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener) |
Removes an AudioRouting.OnRoutingChangedListener which has been previously added to receive rerouting notifications. |
void |
reset() |
Resets the MediaPlayer to its uninitialized state. |
void |
restoreKeys(byte[] keySetId) |
Restore persisted offline keys into a new session. |
void |
seekTo(int msec) |
Seeks to specified time position. |
void |
seekTo(long msec, int mode) |
Moves the media to specified time position by considering the given mode. |
void |
selectTrack(int index) |
Selects a track. |
void |
setAudioAttributes(AudioAttributes attributes) |
Sets the audio attributes for this MediaPlayer. |
void |
setAudioSessionId(int sessionId) |
Sets the audio session ID. |
void |
setAudioStreamType(int streamtype) |
This method was deprecated in API level 26. use setAudioAttributes(android.media.AudioAttributes) |
void |
setAuxEffectSendLevel(float level) |
Sets the send level of the player to the attached auxiliary effect. |
void |
setDataSource(AssetFileDescriptor afd) |
Sets the data source (AssetFileDescriptor) to use. |
void |
setDataSource(FileDescriptor fd) |
Sets the data source (FileDescriptor) to use. |
void |
setDataSource(FileDescriptor fd, long offset, long length) |
Sets the data source (FileDescriptor) to use. |
void |
setDataSource(String path) |
Sets the data source (file-path or http/rtsp URL) to use. |
void |
setDataSource(Context context, Uri uri, Map<String, String> headers, List<HttpCookie> cookies) |
Sets the data source as a content Uri. |
void |
setDataSource(Context context, Uri uri, Map<String, String> headers) |
Sets the data source as a content Uri. |
void |
setDataSource(MediaDataSource dataSource) |
Sets the data source (MediaDataSource) to use. |
void |
setDataSource(Context context, Uri uri) |
Sets the data source as a content Uri. |
void |
setDisplay(SurfaceHolder sh) |
Sets the SurfaceHolder to use for displaying the video portion of the media. |
void |
setDrmPropertyString(String propertyName, String value) |
Set a DRM engine plugin String property value. |
void |
setLooping(boolean looping) |
Sets the player to be looping or non-looping. |
void |
setNextMediaPlayer(MediaPlayer next) |
Set the MediaPlayer to start when this MediaPlayer finishes playback (i.e. |
void |
setOnBufferingUpdateListener(MediaPlayer.OnBufferingUpdateListener listener) |
Register a callback to be invoked when the status of a network stream's buffer has changed. |
void |
setOnCompletionListener(MediaPlayer.OnCompletionListener listener) |
Register a callback to be invoked when the end of a media source has been reached during playback. |
void |
setOnDrmConfigHelper(MediaPlayer.OnDrmConfigHelper listener) |
Register a callback to be invoked for configuration of the DRM object before the session is created. |
void |
setOnDrmInfoListener(MediaPlayer.OnDrmInfoListener listener) |
Register a callback to be invoked when the DRM info is known. |
void |
setOnDrmInfoListener(MediaPlayer.OnDrmInfoListener listener, Handler handler) |
Register a callback to be invoked when the DRM info is known. |
void |
setOnDrmPreparedListener(MediaPlayer.OnDrmPreparedListener listener, Handler handler) |
Register a callback to be invoked when the DRM object is prepared. |
void |
setOnDrmPreparedListener(MediaPlayer.OnDrmPreparedListener listener) |
Register a callback to be invoked when the DRM object is prepared. |
void |
setOnErrorListener(MediaPlayer.OnErrorListener listener) |
Register a callback to be invoked when an error has happened during an asynchronous operation. |
void |
setOnInfoListener(MediaPlayer.OnInfoListener listener) |
Register a callback to be invoked when an info/warning is available. |
void |
setOnMediaTimeDiscontinuityListener(MediaPlayer.OnMediaTimeDiscontinuityListener listener, Handler handler) |
Sets the listener to be invoked when a media time discontinuity is encountered. |
void |
setOnMediaTimeDiscontinuityListener(MediaPlayer.OnMediaTimeDiscontinuityListener listener) |
Sets the listener to be invoked when a media time discontinuity is encountered. |
void |
setOnPreparedListener(MediaPlayer.OnPreparedListener listener) |
Register a callback to be invoked when the media source is ready for playback. |
void |
setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener listener) |
Register a callback to be invoked when a seek operation has been completed. |
void |
setOnSubtitleDataListener(MediaPlayer.OnSubtitleDataListener listener) |
Sets the listener to be invoked when a subtitle track has new data available. |
void |
setOnSubtitleDataListener(MediaPlayer.OnSubtitleDataListener listener, Handler handler) |
Sets the listener to be invoked when a subtitle track has new data available. |
void |
setOnTimedMetaDataAvailableListener(MediaPlayer.OnTimedMetaDataAvailableListener listener) |
Register a callback to be invoked when a selected track has timed metadata available. |
void |
setOnTimedTextListener(MediaPlayer.OnTimedTextListener listener) |
Register a callback to be invoked when a timed text is available for display. |
void |
setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener listener) |
Register a callback to be invoked when the video size is known or updated. |
void |
setPlaybackParams(PlaybackParams params) |
Sets playback rate using PlaybackParams. |
boolean |
setPreferredDevice(AudioDeviceInfo deviceInfo) |
Specifies an audio device (via an AudioDeviceInfo object) to route the output from this MediaPlayer. |
void |
setScreenOnWhilePlaying(boolean screenOn) |
Control whether we should use the attached SurfaceHolder to keep the screen on while video playback is occurring. |
void |
setSurface(Surface surface) |
Sets the Surface to be used as the sink for the video portion of the media. |
void |
setSyncParams(SyncParams params) |
Sets A/V sync mode. |
void |
setVideoScalingMode(int mode) |
Sets video scaling mode. |
void |
setVolume(float leftVolume, float rightVolume) |
Sets the volume on this player. |
void |
setWakeMode(Context context, int mode) |
Set the low-level power management behavior for this MediaPlayer. |
void |
start() |
Starts or resumes playback. |
void |
stop() |
Stops playback after playback has been started or paused. |
Protected methods:
Protected methods |
Description |
|
void |
finalize() |
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |
Inherited methods - From class java.lang.Object:
From class java.lang.Object |
Description |
|
Object |
clone() |
Creates and returns a copy of this object. |
boolean |
equals(Object obj) |
Indicates whether some other object is "equal to" this one. |
void |
finalize() |
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |
final Class<?> |
getClass() |
Returns the runtime class of this Object. |
int |
hashCode() |
Returns a hash code value for the object. |
final void |
notify() |
Wakes up a single thread that is waiting on this object's monitor. |
final void |
notifyAll() |
Wakes up all threads that are waiting on this object's monitor. |
String |
toString() |
Returns a string representation of the object. |
final void |
wait(long timeout, int nanos) |
Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed. |
final void |
wait(long timeout) |
Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. |
final void |
wait() |
Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. |
Inherited methods - From interface android.media.VolumeAutomation:
From interface android.media.VolumeAutomation |
Description |
|
abstract VolumeShaper |
createVolumeShaper(VolumeShaper.Configuration configuration) |
Returns a VolumeShaper object that can be used modify the volume envelope of the player or track. |
Inherited methods - From interface android.media.AudioRouting:
From interface android.media.AudioRouting |
Description |
|
abstract void |
addOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener, Handler handler) |
Adds an AudioRouting.OnRoutingChangedListener to receive notifications of routing changes on this AudioTrack/AudioRecord. |
abstract AudioDeviceInfo |
getPreferredDevice() |
Returns the selected output/input specified by setPreferredDevice(AudioDeviceInfo). |
abstract AudioDeviceInfo |
getRoutedDevice() |
Returns an AudioDeviceInfo identifying the current routing of this AudioTrack/AudioRecord. |
abstract void |
removeOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener) |
Removes an AudioRouting.OnRoutingChangedListener which has been previously added to receive rerouting notifications. |
abstract boolean |
setPreferredDevice(AudioDeviceInfo deviceInfo) |
Specifies an audio device (via an AudioDeviceInfo object) to route the output/input to/from. |
參考網址:
※ MediaPlayer 概覽
沒有留言:
張貼留言