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, Mapheaders, List cookies) throws IOException { throw new RuntimeException("Stub!"); } public void setDataSource(Context context, Uri uri, Map 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 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 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!"); } } }