class android.media.MediaPlayer

2011-05-17  来源:本站原创  分类:Mobile  人气:170 

class android.media.MediaPlayer

MediaPlayer class can be used to control audio / video files or streaming playback. On how to use the methods of this class can also read VideoView class documentation.

1. State diagram

For playing audio / video files and streams is controlled by a state machine to manage. The following figure shows a MediaPlayer object is to support the playback control operation and status-driven life cycle. Elliptical objects may reside on behalf of the state of MediaPlayer. MediaPlayer arc that drive migration between states in the various playback control operation. There are two types of arcs. Arrow to start the arc by a representative of synchronous method calls, and the double arrow at the beginning of the curve on behalf of the representatives of the asynchronous method call.

Through this picture, we can know that a MediaPlayer object has the following states:

1) When a MediaPlayer object is just created with the new operator or call the reset () method, it is in Idle state. When you call the release () method, it is in End state. MediaPlayer between these two states is the object life cycle.

1.1) Construction of a new MediaPlayer object and a call reset () method MediaPlayer objects have a small but very important difference. In the Idle state is calling getCurrentPosition (), getDuration (), getVideoHeight (), getVideoWidth (), setAudioStreamType (int), setLooping (boolean), setVolume (float, float), pause (), start (), stop ( ), seekTo (int), prepare () or prepareAsync () methods are programming errors. When a MediaPlayer object has just been built when the playback engine and the internal state of the object are not changed, more than at this time call those methods, the framework will be unable to register client callback OnErrorListener.onError () method; if The MediaPlayer object called reset () method, then call those methods above, the playback engine will call back within the client program register OnErrorListener.onError () method, and will be passed in the wrong state.

1.2) We propose that once a MediaPlayer object is no longer being used, should immediately call the release () method to release the internal playback engine associated with the MediaPlayer object resources. Resources may include components such as hardware acceleration components of a single state, if not call the release () method may lead to the MediaPlayer object instance after that one state can not use the hardware resources to run back to the software or failure. Once the MediaPlayer object into the End state, it can no longer be used, there is no way to migrate to other states.

1.3) In addition, the use of new operator to create a MediaPlayer object is in Idle state, and those by overloading of the create () convenience method to create the MediaPlayer object is not in the Idle state. In fact, if a successful call to the overloaded create () method, then the object is already Prepare state of the.

2) Under normal circumstances, for various reasons some players control operation may fail if not supported audio / video formats, the lack of interlaced audio / video, high resolution, flow-out and other reasons, and so on. Therefore, the error reporting and recovery in this case is very important. Sometimes, due to programming errors, in the case of an invalid state is called a playback control operation may occur. In all of these error conditions, the internal playback engine will call a client programmer to provide the OnErrorListener.onError () method. The client programmer can call MediaPlayer.setOnErrorListener (android.media.MediaPlayer.OnErrorListener) method to register OnErrorListener.

2.1) in the event of an error, MediaPlayer object into the Error state.

2.2) In order to reuse an object in the Error state MediaPlayer, you can call reset () method to restore this object into Idle state.

2.3) up to learn a OnErrorListener errors that occur within the playback engine is good programming practice.

2.4) in illegal state called a number of ways, such as the prepare (), prepareAsync () and setDataSource () method throws an IllegalStateException.

3) call setDataSource (FileDescriptor) method, or setDataSource (String) method, or setDataSource (Context, Uri) method, or setDataSource (FileDescriptor, long, long) method is in Idle state of the object will migrate to the Initialized state.

3.1) If when the MediaPlayer in the other state, call the setDataSource () method throws an IllegalStateException.

3.2), good programming practice is to not neglect the call setDataSource () method when an exception may be thrown IllegalArgumentException and IOException exceptions.

4) before playback begins, MediaPlayer object must enter the Prepared state.

4.1) There are two methods (synchronous and asynchronous) can make the MediaPlayer object into Prepared state: either call prepare () method (synchronous), this method returns to that of the MediaPlayer object has entered the Prepared state; or call prepareAsync () method ( asynchronous), this method will make the MediaPlayer object into the Preparing state and return, and the internal playback engine to finish his preparations. When the synchronous or asynchronous version returns fully completed version of the preparatory work for the client will be called when the programmer provides OnPreparedListener.onPrepared () listener method. Can call MediaPlayer.setOnPreparedListener (android.media.MediaPlayer.OnPreparedListener) method to register OnPreparedListener.

4.2) Preparing an intermediate state, in this state to call any way with the result of side effects are unknown!

4.3) is called in inappropriate state prepare () and prepareAsync () method throws an IllegalStateException. Prepared state when the MediaPlayer object is the time to adjust the audio / video properties, such as volume, play time is always on screen, loop and so on.

5) To start playing, you must call the start () method. When this method returns successfully, MediaPlayer object is in Started state. isPlaying () method can be called to test whether an object is in the Started state MediaPlayer.

5.1) When in the Started state, the internal playback engine will call the client programmer's OnBufferingUpdateListener.onBufferingUpdate () callback method, this callback allows an application to track the current state of play of the buffer.

5.2) is already in a state of the MediaPlayer object Started calling start () method has no effect.

6) Play can be pause, stop, and adjust the current playback position. When you call the pause () method and returns, will MediaPlayer object into the Paused state. Note Started with the Paused state conversion in the internal playback engine is asynchronous. It may take a little time in isPlaying () method to update the state, if the streaming content, this time there may be a few seconds.

6.1) calls start () method in the Paused state would make a MediaPlayer object suspended from the place before the resumption of play. When you call start () method returns the time, MediaPlayer object's status has become a Started state.

6.2) is already in a state of the MediaPlayer object Paused pause () method has no effect.

7) call stop () method will stop playing, and will make a in Started, Paused, Prepared or PlaybackCompleted state MediaPlayer into Stopped state.

7.1) is already in a state of the MediaPlayer object Stopped stop () method has no effect.

8) Call seekTo () method can adjust the playback position.

8.1) seekTo (int) method is executed asynchronously, so it can return immediately, but the actual operation of the positioning of players may take some time to complete, especially in the form of play streaming audio / video. When the actual positioning Play operation is completed, the internal playback engine will call the client programmer's OnSeekComplete.onSeekComplete () callback method. By setOnSeekCompleteListener (OnSeekCompleteListener) method to register.

8.2) Note, seekTo (int) method can also be invoked in other states, such as Prepared, Paused and PlaybackCompleted state. In addition, the current playback position, the actual can call getCurrentPosition () method available, it can help, such as music player application that plays continuously updated progress

9) When playing to the end of the stream, the player is complete.

9.1) If you call the setLooping (boolean) method to open a circular pattern, then the MediaPlayer object will re-enter the Started state.

9.2) if there is no open loop mode, the playback engine will call within the client programmer's OnCompletion.onCompletion () callback method. By calling MediaPlayer.setOnCompletionListener (OnCompletionListener) method to set. Once inside the playback engine called OnCompletion.onCompletion () callback method to prove that the MediaPlayer object into the PlaybackCompleted state.

9.3) When in PlaybackCompleted state, they can then call the start () method to let this MediaPlayer object re-enter the Started state.

相关文章
  • class android.media.MediaPlayer 2011-05-17

    MediaPlayer class can be used to control audio / video files or streaming playback. On how to use the methods of this class can also read VideoView class documentation. 1. State diagram For playing audio / video files and streams is controlled by a s

  • Android的MediaPlayer 2015-01-12

    MediaPlayer类可用于控制音频/视频文件或流的播放.关于如何使用这个类的方法还可以阅读VideoView类的文档. 1.状态图 对播放音频/视频文件和流的控制是通过一个状态机来管理的.下图显示一个MediaPlayer对象被支持的播放控制操作驱动的生命周期和状态.椭圆代表MediaPlayer对象可能驻留的状态.弧线表示驱动MediaPlayer在各个状态之间迁移的播放控制操作. 这里有两种类型的弧线.由一个箭头开始的弧代表同步的方法调用,而以双箭头开头的代表的弧线代表异步方法调用. 通

  • Android Media Scanner Mechanism Analyze 2010-08-19

    Architecture Figure 2-1 As Figure 2-1. MediaScannerReciver start up at anytime where receive intent ACTION_BOOT_COMPLETED, ACTION_MEDIA_MOUNTED or ACTION_MEDIA_SCANNER_SCAN_FILE. Cause on that spend long time to process the media metadata, so that Me

  • android Media process 2010-12-20

    Android Media Scanner Process [First written by Steve Guo, please keep the mark if forwarding.] Here is the overall picture. MediaScannerReceiver is started when any ACTION_BOOT_COMPLETED, ACTION_MEDIA_MOUNTED or ACTION_MEDIA_SCANNER_SCAN_FILE intent

  • 浅析Android 的 MediaPlayer类 2013-12-10

    本文主要介绍了Android的mediaplayer类作用和用法,并附上了关键代码,有需要的朋友可以参考下 在Android中通常用MediaPlayer来播放一些媒体文件,对于音频文件来说只需直接使用MeidaPlayer结合几句代码即可,但是对于视频文件来说稍微复杂一些.单独的MediaPlayer只能播放音频文件,要想播放视频还需要SurfaceView来配合显示画面.而对于SurfacView而言,在Android中采用了显示与控制分离机制,即SurfaceView只是负责显示画面,而不

  • Android Multimedia scanning process (Android Media Scanner Process) 2010-05-28

    The following is the system diagram MediaScannerReceiver in any ACTION_BOOT_COMPLETED, ACTION_MEDIA_MOUNTED or ACTION_MEDIA_SCANNER_SCAN_FILE intent (intent), when given starts. Because the analysis of the media file metadata may take a long time, so

  • android media files inform the media database updates 2010-11-25

    sendBroadcast (new Intent (Intent.ACTION_MEDIA_MOUNTED, Uri.parse ("file: / /" + Environment . GetExternalStorageDirectory ())));

  • Android MediaPlayer life cycle 2010-06-23

    MediaPlayer's state transition diagram is also characterized its life cycle, understand this map can help us consider the case when using MediaPlayer more comprehensive, write the code more robust. This state transition diagram a clear description of

  • The core principle of Android MediaPlayer 2010-06-25

    In this paper, is a very important Android is also the most complex media player (MediaPlayer) part of the structure. For a complete Android and relatively complex system, a function of the realization of MediaPlayer is not their specific function, b

  • Android MediaPlayer music 2010-07-04

    This example demonstrates the use MediaPlayer Play sdcard in the music, first in / sdcard / directory, adding music files (mp3), <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.c

  • Improve the first of the Android MediaPlayer 2010-12-19

    Written in front of fourteen articles on the interface of the entry, we all read and follow the exercises, the commonly used Layout and View will have a certain understanding, the next article describes the interface is no longer stressed, but rather

  • Android提高之MediaPlayer音视频播放 2013-12-15

    这篇文章主要介绍了Android提高之MediaPlayer音视频播放,很实用的功能,需要的朋友可以参考下 前面文章已经详细介绍了Android界面的入门技术,相信大家在看完和跟着练习之后,会对于常用的Layout和View都会有一定的了解了,接下来就不再强调介绍界面了,而是针对具体的常见功能而展开. 本文将介绍MediaPlayer的使用.MediaPlayer可以播放音频和视频,另外也可以通过VideoView来播放视频,虽然VideoView比MediaPlayer简单易用,但定制性不如用

  • Android提高之MediaPlayer播放网络音频的实现方法 2014-04-16

    这篇文章主要介绍了Android的MediaPlayer播放网络音频的实现方法,很实用的功能,需要的朋友可以参考下 前面有文章曾经地介绍过MediaPlayer的基本用法,这里就更加深入地讲解MediaPlayer的在线播放功能.本文主要实现MediaPlayer在线播放音频的功能,由于在线视频播放比在线音频播放复杂,因此先介绍在线音频播放的实现,这样可以帮助大家逐步深入了解MediaPlayer的在线播放功能. 先来看看本文程序运行的结果如下图所示: main.xml的源码如下: <?xml

  • Android提高之MediaPlayer播放网络视频的实现方法 2014-10-22

    这篇文章主要介绍了Android的MediaPlayer播放网络视频的实现方法,是一个非常实用的功能,需要的朋友可以参考下 前面讲解了MediaPlayer播放网络音频,主要介绍了MediaPlayer关于网络音频的缓冲和进度条控制的方法,本文再来讲解一下MediaPlayer播放网络视频的方法.播放网络视频比播放网络音频需要多一个SurfaceView而已,在已经熟悉了MediaPlayer播放网络音频之后,相信大家对本文所述的播放网络视频也能很快地掌握. 先来看看本文程序运行截图,如下所示:

  • android MediaPlayer API 2011-08-12

    MediaPlayer类可用于控制音频/视频文件或流的播放.关于如何使用这个类的方法还可以阅读VideoView类的文档. 1.状态图 对播放音频/视频文件和流的控制是通过一个状态机来管理的.下图显示一个MediaPlayer对象被支持的播放控制操作驱动的生命周期和状态.椭圆代表MediaPlayer对象可能驻留的状态.弧线表示驱动MediaPlayer在各个状态之间迁移的播放控制操作.这里有两种类型的弧线.由一个箭头开始的弧代表同步的方法调用,而以双箭头开头的代表的弧线代表异步方法调用. 通过

  • [Android游戏开发之八]游戏中添加音频-详解MediaPlayer与SoundPoo!并讲解两者的区别和游戏中的用途! 2012-03-19

    李华明Himi 原创,转载务必在明显处注明: 转载自 [黑米GameDev街区] 原文链接: http://www.himigame.com/android-game/312.html 很多童鞋说我的代码运行后,点击home或者back后会程序异常,如果你也这样遇到过,那么你肯定没有仔细读完Himi的博文,第十九篇Himi专门写了关于这些错误的原因和解决方法,这里我在博客都补充说明下,省的童鞋们总疑惑这一块:请点击下面联系进入阅读: [Android游戏开发十九](必看篇)SurfaceView

  • Android MediaPlayer状态机 2012-03-28

    翻译Android Reference Manual的MediaPlayer的状态机 对播放音频/视频文件和流的控制是通过一个状态机来管理的.下图显示一个MediaPlayer对象被支持的播放控制操作驱动的生命周期和状态.椭圆代表MediaPlayer对象可能驻留的状态.弧线表示驱动MediaPlayer在各个状态之间迁移的播放控制操作.这里有两种类型的弧线.由一个箭头开始的弧代表同步的方法调用,而以双箭头开头的代表的弧线代表异步方法调用. 通过这张图,我们可以知道一个MediaPlayer对象

  • Android MediaPlayer的生命周期 2014-03-09

    这张状态转换图清晰的描述了MediaPlayer的各个状态,也列举了主要的方法的调用时序,每种方法只能在一些特定的状态下使用,如果使用时MediaPlayer的状态不正确则会引发IllegalStateException异常. 1: Idle 状态:当使用new MediaPlayer()创建一个MediaPlayer对象或者调用了其reset()时,该MediaPlayer对象处于idle状态.这两种方法的一个重要差别就是:如果在这个状态下调用了getDuration()等方法(相当于调用时机

  • MP3 player with an application to Android experience in the Service 2010-03-29

    Cell phones are indispensable in people's lives a useful tool. We are using the phone, often multi-threaded, and is suddenly interrupted. For instance, we will side browse the web while listening to MP3. Or is text messaging, when a sudden incoming c

  • MP3 player of small applications (and writing to learn Android 1) 2010-03-29

    "In the information age, the objective obstacles no longer exists, the so-called barriers are subjective. If you want to develop any new technology, you do not need millions of dollars, you only need to put in the refrigerator full of pizza and Coke,