MediaPlayer簡介
android多媒體類,播放流程如下方法:
setDataSource() 設置一個數據源,
prepare() 播放之前需要調用此方法,在准備狀態
setDisplay() 需要SurfaceHolder對象,畫面顯示在SurfaceView上
start() 開始播放
pause() 播放暫停
stop() 停止播放(處於停止狀態必須調用prepare()讓其回到准備狀態才能播放)
MediaPlayer源碼里的方法
package android.media; import android.content.Context; import android.content.res.AssetFileDescriptor; import android.media.MediaDrm.KeyRequest; import android.media.VolumeShaper.Configuration; import android.net.Uri; import android.os.Handler; import android.os.Parcel; import android.os.Parcelable; import android.os.PersistableBundle; import android.view.Surface; import android.view.SurfaceHolder; import java.io.FileDescriptor; import java.io.IOException; import java.net.HttpCookie; import java.util.List; import java.util.Map; import java.util.UUID; public class MediaPlayer implements VolumeAutomation { //文件或者是網絡相關操作錯誤 public static final int MEDIA_ERROR_IO = -1004; //添加不符合的編碼標准或文件格式 public static final int MEDIA_ERROR_MALFORMED = -1007; //視頻索引(例如,MOOVAtom)不在文件的開頭 public static final int MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK = 200; //媒體服務關閉 public static final int MEDIA_ERROR_SERVER_DIED = 100; //有些操作花費的時間太長,通常超過3-5秒 public static final int MEDIA_ERROR_TIMED_OUT = -110; //未指定的媒體,播放錯誤 public static final int MEDIA_ERROR_UNKNOWN = 1; //媒體框架不支持該規格 public static final int MEDIA_ERROR_UNSUPPORTED = -1010; //無法播放 public static final int MEDIA_INFO_AUDIO_NOT_PLAYING = 804; //播放文件損壞,視頻跟音頻不同步 public static final int MEDIA_INFO_BAD_INTERLEAVING = 800; //加載滿緩沖 開始播放 public static final int MEDIA_INFO_BUFFERING_END = 702; //MyIApple暫時暫停播放,以緩沖更多的數據 public static final int MEDIA_INFO_BUFFERING_START = 701; //一系列新的metedata可被使用 public static final int MEDIA_INFO_METADATA_UPDATE = 802; //不可移動幀,對於直播流 public static final int MEDIA_INFO_NOT_SEEKABLE = 801; //渲染字幕時間過長 public static final int MEDIA_INFO_SUBTITLE_TIMED_OUT = 902; //獲取的視頻信息是未知的信息 public static final int MEDIA_INFO_UNKNOWN = 1; //字幕軌道不被媒體服務支持 public static final int MEDIA_INFO_UNSUPPORTED_SUBTITLE = 901; public static final int MEDIA_INFO_VIDEO_NOT_PLAYING = 805; //用戶只推第一幀視頻渲染 public static final int MEDIA_INFO_VIDEO_RENDERING_START = 3; //視頻編碼過於復雜,解碼器無法足夠快的解碼出幀 public static final int MEDIA_INFO_VIDEO_TRACK_LAGGING = 700; public static final String MEDIA_MIMETYPE_TEXT_SUBRIP = "application/x-subrip"; public static final int PREPARE_DRM_STATUS_PREPARATION_ERROR = 3; public static final int PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR = 1; public static final int PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR = 2; public static final int PREPARE_DRM_STATUS_SUCCESS = 0; public static final int SEEK_CLOSEST = 3; public static final int SEEK_CLOSEST_SYNC = 2; public static final int SEEK_NEXT_SYNC = 1; public static final int SEEK_PREVIOUS_SYNC = 0; public static final int VIDEO_SCALING_MODE_SCALE_TO_FIT = 1; public static final int VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING = 2; public MediaPlayer() { throw new RuntimeException("Stub!"); } //設置用SurfaceView來顯示多媒體 public void setDisplay(SurfaceHolder sh) { throw new RuntimeException("Stub!"); } public void setSurface(Surface surface) { throw new RuntimeException("Stub!"); } public void setVideoScalingMode(int mode) { throw new RuntimeException("Stub!"); } //設置一個uri網絡視頻源 public static MediaPlayer create(Context context, Uri uri) { throw new RuntimeException("Stub!"); } //設置一個uri網絡視頻源並且在surfaceView播放 public static MediaPlayer create(Context context, Uri uri, SurfaceHolder holder) { throw new RuntimeException("Stub!"); } public static MediaPlayer create(Context context, Uri uri, SurfaceHolder holder, AudioAttributes audioAttributes, int audioSessionId) { throw new RuntimeException("Stub!"); } //設置資源文件為播放源 public static MediaPlayer create(Context context, int resid) { throw new RuntimeException("Stub!"); } public static MediaPlayer create(Context context, int resid, AudioAttributes audioAttributes, int audioSessionId) { throw new RuntimeException("Stub!"); } //設置一個uri網絡視頻源 public void setDataSource(Context context, Uri uri) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException { throw new RuntimeException("Stub!"); } public void setDataSource(Context context, Uri uri, Map<String, String> headers, List<HttpCookie> cookies) throws IOException { throw new RuntimeException("Stub!"); } public void setDataSource(Context context, Uri uri, Map<String, String> headers) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException { throw new RuntimeException("Stub!"); } //設置視頻源播放路徑 public void setDataSource(String path) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException { throw new RuntimeException("Stub!"); } //設置assets資源文件為播放源 public void setDataSource(AssetFileDescriptor afd) throws IOException, IllegalArgumentException, IllegalStateException { throw new RuntimeException("Stub!"); } public void setDataSource(FileDescriptor fd) throws IOException, IllegalArgumentException, IllegalStateException { throw new RuntimeException("Stub!"); } public void setDataSource(FileDescriptor fd, long offset, long length) throws IOException, IllegalArgumentException, IllegalStateException { throw new RuntimeException("Stub!"); } public void setDataSource(MediaDataSource dataSource) throws IllegalArgumentException, IllegalStateException { throw new RuntimeException("Stub!"); } //進入同步准備狀態 public void prepare() throws IOException, IllegalStateException { throw new RuntimeException("Stub!"); } //進入異步准備狀態 public native void prepareAsync() throws IllegalStateException; //開始播放 public void start() throws IllegalStateException { throw new RuntimeException("Stub!"); } //停止播放 public void stop() throws IllegalStateException { throw new RuntimeException("Stub!"); } //暫停播放 public void pause() throws IllegalStateException { throw new RuntimeException("Stub!"); } public VolumeShaper createVolumeShaper(Configuration configuration) { throw new RuntimeException("Stub!"); } public void setWakeMode(Context context, int mode) { throw new RuntimeException("Stub!"); } //設置用SurfaceHolder顯示 public void setScreenOnWhilePlaying(boolean screenOn) { throw new RuntimeException("Stub!"); } //得到視頻的寬度 public native int getVideoWidth(); //得到視頻的高度 public native int getVideoHeight(); public PersistableBundle getMetrics() { throw new RuntimeException("Stub!"); } //判斷是否在播放狀態 public native boolean isPlaying(); public native void setPlaybackParams(PlaybackParams var1); public native PlaybackParams getPlaybackParams(); public native void setSyncParams(SyncParams var1); public native SyncParams getSyncParams(); public void seekTo(long msec, int mode) { throw new RuntimeException("Stub!"); } //指定播放的位置(以毫秒為單位) public void seekTo(int msec) throws IllegalStateException { throw new RuntimeException("Stub!"); } public MediaTimestamp getTimestamp() { throw new RuntimeException("Stub!"); } //得到當前播放位置 public native int getCurrentPosition(); //得到視頻總時長 public native int getDuration(); public native void setNextMediaPlayer(MediaPlayer var1); //釋放播放資源 public void release() { throw new RuntimeException("Stub!"); } //重置MediaPlayer對象為剛剛創建的狀態 public void reset() { throw new RuntimeException("Stub!"); } //設置流媒體的類型 例如AudioManager.STREAM_MUSIC為音樂 @Deprecated public void setAudioStreamType(int streamtype) { throw new RuntimeException("Stub!"); } public void setAudioAttributes(AudioAttributes attributes) throws IllegalArgumentException { throw new RuntimeException("Stub!"); } //設置循環播放 public native void setLooping(boolean var1); //判斷是否循環播放 public native boolean isLooping(); //設置音量 public void setVolume(float leftVolume, float rightVolume) { throw new RuntimeException("Stub!"); } public native void setAudioSessionId(int var1) throws IllegalArgumentException, IllegalStateException; public native int getAudioSessionId(); public native void attachAuxEffect(int var1); public void setAuxEffectSendLevel(float level) { throw new RuntimeException("Stub!"); } public MediaPlayer.TrackInfo[] getTrackInfo() throws IllegalStateException { throw new RuntimeException("Stub!"); } public void addTimedTextSource(String path, String mimeType) throws IOException, IllegalArgumentException, IllegalStateException { throw new RuntimeException("Stub!"); } public void addTimedTextSource(Context context, Uri uri, String mimeType) throws IOException, IllegalArgumentException, IllegalStateException { throw new RuntimeException("Stub!"); } public void addTimedTextSource(FileDescriptor fd, String mimeType) throws IllegalArgumentException, IllegalStateException { throw new RuntimeException("Stub!"); } public void addTimedTextSource(FileDescriptor fd, long offset, long length, String mime) throws IllegalArgumentException, IllegalStateException { throw new RuntimeException("Stub!"); } public int getSelectedTrack(int trackType) throws IllegalStateException { throw new RuntimeException("Stub!"); } public void selectTrack(int index) throws IllegalStateException { throw new RuntimeException("Stub!"); } public void deselectTrack(int index) throws IllegalStateException { throw new RuntimeException("Stub!"); } protected void finalize() { throw new RuntimeException("Stub!"); } public void setOnPreparedListener(MediaPlayer.OnPreparedListener listener) { throw new RuntimeException("Stub!"); } public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener) { throw new RuntimeException("Stub!"); } public void setOnBufferingUpdateListener(MediaPlayer.OnBufferingUpdateListener listener) { throw new RuntimeException("Stub!"); } public void setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener listener) { throw new RuntimeException("Stub!"); } //視頻尺寸監聽 public void setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener listener) { throw new RuntimeException("Stub!"); } public void setOnTimedTextListener(MediaPlayer.OnTimedTextListener listener) { throw new RuntimeException("Stub!"); } public void setOnTimedMetaDataAvailableListener(MediaPlayer.OnTimedMetaDataAvailableListener listener) { throw new RuntimeException("Stub!"); } //設置錯誤信息監聽 public void setOnErrorListener(MediaPlayer.OnErrorListener listener) { throw new RuntimeException("Stub!"); } public void setOnInfoListener(MediaPlayer.OnInfoListener listener) { throw new RuntimeException("Stub!"); } public void setOnDrmConfigHelper(MediaPlayer.OnDrmConfigHelper listener) { throw new RuntimeException("Stub!"); } public void setOnDrmInfoListener(MediaPlayer.OnDrmInfoListener listener) { throw new RuntimeException("Stub!"); } public void setOnDrmInfoListener(MediaPlayer.OnDrmInfoListener listener, Handler handler) { throw new RuntimeException("Stub!"); } public void setOnDrmPreparedListener(MediaPlayer.OnDrmPreparedListener listener) { throw new RuntimeException("Stub!"); } public void setOnDrmPreparedListener(MediaPlayer.OnDrmPreparedListener listener, Handler handler) { throw new RuntimeException("Stub!"); } public MediaPlayer.DrmInfo getDrmInfo() { throw new RuntimeException("Stub!"); } public void prepareDrm(UUID uuid) throws UnsupportedSchemeException, ResourceBusyException, MediaPlayer.ProvisioningNetworkErrorException, MediaPlayer.ProvisioningServerErrorException { throw new RuntimeException("Stub!"); } public void releaseDrm() throws MediaPlayer.NoDrmSchemeException { throw new RuntimeException("Stub!"); } public KeyRequest getKeyRequest(byte[] keySetId, byte[] initData, String mimeType, int keyType, Map<String, String> optionalParameters) throws MediaPlayer.NoDrmSchemeException { throw new RuntimeException("Stub!"); } public byte[] provideKeyResponse(byte[] keySetId, byte[] response) throws MediaPlayer.NoDrmSchemeException, DeniedByServerException { throw new RuntimeException("Stub!"); } public void restoreKeys(byte[] keySetId) throws MediaPlayer.NoDrmSchemeException { throw new RuntimeException("Stub!"); } public String getDrmPropertyString(String propertyName) throws MediaPlayer.NoDrmSchemeException { throw new RuntimeException("Stub!"); } public void setDrmPropertyString(String propertyName, String value) throws MediaPlayer.NoDrmSchemeException { throw new RuntimeException("Stub!"); } public static final class MetricsConstants { public static final String CODEC_AUDIO = "android.media.mediaplayer.audio.codec"; public static final String CODEC_VIDEO = "android.media.mediaplayer.video.codec"; public static final String DURATION = "android.media.mediaplayer.durationMs"; public static final String ERRORS = "android.media.mediaplayer.err"; public static final String ERROR_CODE = "android.media.mediaplayer.errcode"; public static final String FRAMES = "android.media.mediaplayer.frames"; public static final String FRAMES_DROPPED = "android.media.mediaplayer.dropped"; public static final String HEIGHT = "android.media.mediaplayer.height"; public static final String MIME_TYPE_AUDIO = "android.media.mediaplayer.audio.mime"; public static final String MIME_TYPE_VIDEO = "android.media.mediaplayer.video.mime"; public static final String PLAYING = "android.media.mediaplayer.playingMs"; public static final String WIDTH = "android.media.mediaplayer.width"; MetricsConstants() { throw new RuntimeException("Stub!"); } } public static final class ProvisioningServerErrorException extends MediaDrmException { public ProvisioningServerErrorException(String detailMessage) { super((String)null); throw new RuntimeException("Stub!"); } } public static final class ProvisioningNetworkErrorException extends MediaDrmException { public ProvisioningNetworkErrorException(String detailMessage) { super((String)null); throw new RuntimeException("Stub!"); } } public static final class NoDrmSchemeException extends MediaDrmException { public NoDrmSchemeException(String detailMessage) { super((String)null); throw new RuntimeException("Stub!"); } } public static final class DrmInfo { DrmInfo() { throw new RuntimeException("Stub!"); } public Map<UUID, byte[]> getPssh() { throw new RuntimeException("Stub!"); } public UUID[] getSupportedSchemes() { throw new RuntimeException("Stub!"); } } public interface OnDrmPreparedListener { void onDrmPrepared(MediaPlayer var1, int var2); } public interface OnDrmInfoListener { void onDrmInfo(MediaPlayer var1, MediaPlayer.DrmInfo var2); } public interface OnDrmConfigHelper { void onDrmConfig(MediaPlayer var1); } public interface OnInfoListener { boolean onInfo(MediaPlayer var1, int var2, int var3); } public interface OnErrorListener { boolean onError(MediaPlayer var1, int var2, int var3); } public interface OnTimedMetaDataAvailableListener { void onTimedMetaDataAvailable(MediaPlayer var1, TimedMetaData var2); } public interface OnTimedTextListener { void onTimedText(MediaPlayer var1, TimedText var2); } public interface OnVideoSizeChangedListener { void onVideoSizeChanged(MediaPlayer var1, int var2, int var3); } public interface OnSeekCompleteListener { void onSeekComplete(MediaPlayer var1); } public interface OnBufferingUpdateListener { void onBufferingUpdate(MediaPlayer var1, int var2); } public interface OnCompletionListener { void onCompletion(MediaPlayer var1); } public interface OnPreparedListener { void onPrepared(MediaPlayer var1); } public static class TrackInfo implements Parcelable { public static final int MEDIA_TRACK_TYPE_AUDIO = 2; public static final int MEDIA_TRACK_TYPE_METADATA = 5; public static final int MEDIA_TRACK_TYPE_SUBTITLE = 4; public static final int MEDIA_TRACK_TYPE_TIMEDTEXT = 3; public static final int MEDIA_TRACK_TYPE_UNKNOWN = 0; public static final int MEDIA_TRACK_TYPE_VIDEO = 1; TrackInfo() { throw new RuntimeException("Stub!"); } public int getTrackType() { throw new RuntimeException("Stub!"); } public String getLanguage() { throw new RuntimeException("Stub!"); } public MediaFormat getFormat() { throw new RuntimeException("Stub!"); } public int describeContents() { throw new RuntimeException("Stub!"); } public void writeToParcel(Parcel dest, int flags) { throw new RuntimeException("Stub!"); } public String toString() { throw new RuntimeException("Stub!"); } } }