[Android Development] MediaPlayer error analysis, androidmediaplayer
Recently, I am working on a media player and use MediaPlayer that comes with Android. I often encounter MediaPlayer errors. I have searched the online media and I feel that it is not very good or relatively scattered. Next, I will summarize the common errors that occur when using MediaPlayer. If you know more, you can also comment on it below.
1. Introduction to Mediaplayer
Mediaplayer can be used to control the playback of audio or video files and streams. (MediaPlayer class can be used to control playback of audio/video files and streams .)
Here is the Android official API Mediaplayer Introduction: http://developers.androidcn.com/reference/android/media/MediaPlayer.html
In the official API, we can see the life cycle diagram of Mediaplayer: In this life cycle diagram, we can see some calls and statuses of Mediaplayer.
2. Common Error Analysis 1. The most common errors ):
A. If the MediaPlayer is initialized, use the create method to set the data source. You cannot write the MediaPlayer. prepare () method. Otherwise, an error is returned.
B. if you use the MediaPlayer constructor to initialize the MediaPlayer and set the data source using the setDataSource method, you need to set the MediaPlayer. the start () method is written in the OnPrepared function of the MediaPlayer (because the audio file is not ready ):
1 mediaPlayer.setOnPreparedListener(new OnPreparedListener() {2 @Override3 public void onPrepared(MediaPlayer mp) {4 mp.start();5 }6 });
C. If it is an apk file, write it. You may also need to "chmod 777" to fix the permission. I am not very familiar with this solution.
2. Other errors:
We can monitor error codes by setting onErrorListener for MediaPlayer. For example, an error (-) occurs in the following form (what, extra). Some errors and corresponding error messages are shown below (found from stackoverflow ).
1 mediaPlayer.setOnErrorListener(new OnErrorListener() { 2 @Override 3 public boolean onError(MediaPlayer mp, int what, int extra) { 4 Log.d(TAG, "OnError - Error code: " + what + " Extra code: " + extra); 5 switch (what) { 6 case -1004: 7 Log.d(TAG, "MEDIA_ERROR_IO"); 8 break; 9 case -1007:10 Log.d(TAG, "MEDIA_ERROR_MALFORMED");11 break;12 case 200:13 Log.d(TAG, "MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK");14 break;15 case 100:16 Log.d(TAG, "MEDIA_ERROR_SERVER_DIED");17 break;18 case -110:19 Log.d(TAG, "MEDIA_ERROR_TIMED_OUT");20 break;21 case 1:22 Log.d(TAG, "MEDIA_ERROR_UNKNOWN");23 break;24 case -1010:25 Log.d(TAG, "MEDIA_ERROR_UNSUPPORTED");26 break;27 }28 switch (extra) {29 case 800:30 Log.d(TAG, "MEDIA_INFO_BAD_INTERLEAVING");31 break;32 case 702:33 Log.d(TAG, "MEDIA_INFO_BUFFERING_END");34 break;35 case 701:36 Log.d(TAG, "MEDIA_INFO_METADATA_UPDATE");37 break;38 case 802:39 Log.d(TAG, "MEDIA_INFO_METADATA_UPDATE");40 break;41 case 801:42 Log.d(TAG, "MEDIA_INFO_NOT_SEEKABLE");43 break;44 case 1:45 Log.d(TAG, "MEDIA_INFO_UNKNOWN");46 break;47 case 3:48 Log.d(TAG, "MEDIA_INFO_VIDEO_RENDERING_START");49 break;50 case 700:51 Log.d(TAG, "MEDIA_INFO_VIDEO_TRACK_LAGGING");52 break;53 }54 return false;55 }56 });
3. Error status:
That is, the start called in state 0, 0 indicates his error status. The following is the MediaPlayer status, which is found in the source code:
1 enum media_player_states { 2 MEDIA_PLAYER_STATE_ERROR = 0, 3 MEDIA_PLAYER_IDLE = 1 << 0, // 1 4 MEDIA_PLAYER_INITIALIZED = 1 << 1, // 2 5 MEDIA_PLAYER_PREPARING = 1 << 2, // 4 6 MEDIA_PLAYER_PREPARED = 1 << 3, // 8 7 MEDIA_PLAYER_STARTED = 1 << 4, // 16 8 MEDIA_PLAYER_PAUSED = 1 << 5, // 32 9 MEDIA_PLAYER_STOPPED = 1 << 6, // 6410 MEDIA_PLAYER_PLAYBACK_COMPLETE = 1 << 7, // 12811 };
You can refer to the error status and MediaPlayer lifecycle () for error analysis.
Iii. Suggestions
1. When using MediaPlayer for preparation, it is best to use the prepareAsync () method instead of the prepare () method, because the previous method is prepared asynchronously and will not hinder the main thread;
2. mediaplayer. it is best to write the start () method to the onPrepared function of setOnPreparedListener for start. Although sometimes the start method is written directly after prepareAsync ()/prepare (), no error is reported, but we need to eliminate this bug !!
3. After you stop playing the MediaPlayer, you must call the MediaPlayer prepareAsync () method again, instead of calling the start () method! As shown in the preceding MediaPlayer lifecycle.
4. you can set the listener and set variables to mark the approximate status (especially Error, End, and Idle) of the MediaPlayer, and check the status before performing operations on related functions, you can avoid most "Operation errors ". If this is not the case, the IllegalStateException exception is caught for each MediaPlayer function operation.
5. In our programming, we can refer to the MediaPlayer lifecycle step by step. This reduces the number of errors.
Thank you for reading this article. There may be some imperfections. If you have some knowledge, please leave a message below. I can improve the Mediaplayer for your reference. If necessary, subscribe to the next blog! My CSDN blog: http://blog.csdn.net/u010049692/article/details/38867449
In android development, how does one determine that Mediaplayer is actually playing? Using MediaplayerisPlaying () is sometimes inaccurate?
Status determination: Set a status when starting playback, and set a status when paused. This status is used to determine whether the video is being played. Generally, the isPlaying method is sufficient.
Error () reported during Android MediaPlayer usage
Add these two sentences before m. start ();, m. stop (); m. prepare ();