android音视频播放学习之MediaPlayer学习记录

本文详细介绍了Android中MediaPlayer的工作状态及各种方法的有效调用状态,包括idle、initialized、prepared等,并解析了不同状态下调用start、pause等方法的行为差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     最近项目中涉及到音视频开发,选择MediaPlayer+surfaceView的方式播放视频。翻阅开发者文档学习了解下MediaPlaer的介绍和使用,在这里记录下对MediaPlaer的理解。

    MediaPlayer可以用于播放本地、网络或者apk包中的音视频文件,用setDataSource()方法设置文件的路径。首先MediaPlayer是一个状态机,有下面几个状态:idle(闲置)、initialized、prepared、start、pause、stop、playbackCompleted、end、error。当mediaplayer使用new创建出来或者调用了reset()方法后mediaplayer处于idle状态,此时需要调用prepare()(同步方法)或者​​​​​​​ prepareAsync()(异步方法)才能使mediaplayer处于可用状态。

  当mediaplayer刚创建或调用reset()方法使mediaplayer处于idle状态调用start()等方法时出现的一些区别:
当mediaplayer刚创建时,调用start()方法,mediaplayer的状态不会改变,如果设置了
OnErrorListener,其中的onError()方法不会被调用。

  当调用reset()方法处于idle状态时调用start()方法会是mediaplayer处于error状态,此时如果设置了OnErrorListener,其中的onError()方法将会被调用。

      调用 release()会释放的资源,使mediaplayer处于end状态,此时在调用其他方法都不会改变mediaplayer的状态,此方法调用应该在不再使用mediaplayer时调用。

  MediaPlayer可以设置一些监听:

    mediaplayer中的方法需要在对应的状态中调用否则会将mediaplayer转为error状态,以下为各个方法调用的有效状态和无效状态表格:

  •  Valid Sates

     

    Invalid States

     

    Comments

     

    getCurrentPosition

     

    {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}

     

    {Error}

     

    Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.

     

    getDuration

     

    {Prepared, Started, Paused, Stopped, PlaybackCompleted}

     

    {Idle, Initialized, Error}

     

    Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.

     

    getVideoHeight

     

    {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}

     

    {Error}

     

    Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.

     

    getVideoWidth

     

    {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}

     

    {Error}

     

    Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.

     

    isPlaying

     

    {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}

     

    {Error}

     

    Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.

     

    pause

     

    {Started, Paused}

     

    {Idle, Initialized, Prepared, Stopped, PlaybackCompleted, Error}

     

    Successful invoke of this method in a valid state transfers the object to the Paused state. Calling this method in an invalid state transfers the object to the Error state.

     

    prepare

     

    {Initialized, Stopped}

     

    {Idle, Prepared, Started, Paused, PlaybackCompleted, Error}

     

    Successful invoke of this method in a valid state transfers the object to the Prepared state. Calling this method in an invalid state throws an IllegalStateException.

     

    prepareAsync

     

    {Initialized, Stopped}

     

    {Idle, Prepared, Started, Paused, PlaybackCompleted, Error}

     

    Successful invoke of this method in a valid state transfers the object to the Preparing state. Calling this method in an invalid state throws an IllegalStateException.

     

    release

     

    any

     

    {}

     

    After release(), the object is no longer available.

     

    reset

     

    {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error}

     

    {}

     

    After reset(), the object is like being just created.

     

    seekTo

     

    {Prepared, Started, Paused, PlaybackCompleted}

     

    {Idle, Initialized, Stopped, Error}

     

    Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.

     

    setAudioStreamType

     

    {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted}

     

    {Error}

     

    Successful invoke of this method does not change the state.

     

    setDataSource

     

    {Idle}

     

    {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error}

     

    Successful invoke of this method in a valid state transfers the object to the Initialized state. Calling this method in an invalid state throws an IllegalStateException.

     

    setDisplay

     

    any

     

    {}

     

    This method can be called in any state and calling it does not change the object state.

     

    setLooping

     

    {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted}

     

    {Error}

     

    Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.

     

    isLooping

     

    any

     

    {}

     

    This method can be called in any state and calling it does not change the object state.

     

    setOnBufferingUpdateListener

     

    any

     

    {}

     

    This method can be called in any state and calling it does not change the object state.

     

    setOnCompletionListener

     

    any

     

    {}

     

    This method can be called in any state and calling it does not change the object state.

     

    setOnErrorListener

     

    any

     

    {}

     

    This method can be called in any state and calling it does not change the object state.

     

    setOnPreparedListener

     

    any

     

    {}

     

    This method can be called in any state and calling it does not change the object state.

     

    setOnSeekCompleteListener

     

    any

     

    {}

     

    This method can be called in any state and calling it does not change the object state.

     

    setScreenOnWhilePlayingany

     

    {}

     

    This method can be called in any state and calling it does not change the object state.

     

    setVolume

     

    {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted}

     

    {Error}

     

    Successful invoke of this method does not change the state.
    setWakeMode

     

    any

     

    {}

     

    This method can be called in any state and calling it does not change the object state.

     

    start

     

    {Prepared, Started, Paused, PlaybackCompleted}

     

    {Idle, Initialized, Stopped, Error}

     

    Successful invoke of this method in a valid state transfers the object to the Started state. Calling this method in an invalid state transfers the object to the Error state.

     

    stop

     

    {Prepared, Started, Stopped, Paused, PlaybackCompleted}

     

    {Idle, Initialized, Error}

     

    Successful invoke of this method in a valid state transfers the object to the Stopped state. Calling this method in an invalid state transfers the object to the Error state.

     

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值