Android uses MediaPlayer to play mp3 in the raw directory

When using the MediaPlayer that comes with android to play mp3, you need to pay attention to a few points:

1. Use:

——>Initialization:

MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.example_song);

——>Play:

mediaPlayer.start();

--> release:

mediaPlayer.stop();
mediaPlayer.release();

2. Add playback monitoring:

——> Error monitoring:

mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {     @Override     public boolean onError(MediaPlayer mp, int what, int extra) {         // This method is called when an error occurs in MediaPlayer         // You can do proper error handling here, for example Pop-up error prompt box, etc.         return false;     } });






tip: When an error occurs in MediaPlayer, the onError method will be called. Appropriate error handling can be performed here, such as popping up an error prompt box, etc. A return value of false means that the error will not be handled by MediaPlayer, but by the caller itself. If true is returned, it means MediaPlayer will handle the error and continue

--> Ready:

mediaPlayer.setOnPreparedListener(new MediaPlayer.onPreparedListener(){ 
        @Override 
        public void onPrepared(MediaPlayer mp) { LogUtils.e("MP3Player————onPrepared");  
             mp.start(); 
        } 
});

——>Play completed:

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {     @Override     public void onCompletion(MediaPlayer mp) {         // This method is called when the MediaPlayer finishes playing         // You can perform some related operations here, such as updating the UI interface, etc.     } }) ;





3. About release

In the setOnCompletionListener listener, there is no need to manually call the mediaPlayer.stop(), mediaPlayer.release() methods.

The setOnCompletionListener method is automatically called after the MediaPlayer finishes playing, and calling the stop() and release() methods in this method may cause unpredictable problems.

When the MediaPlayer finishes playing, it stops automatically and is in the stopped state. If you still need to use MediaPlayer, you can choose to call the MediaPlayer.create() method again to create a new MediaPlayer object to play other audio files. If you no longer need to use MediaPlayer, you can call the release() method at an appropriate time to release the resources it occupies.

4. About initialization

The difference between MediaPlayer.create() and new MediaPlayer() is that they create MediaPlayer objects in different ways.

MediaPlayer.create() is a static method, which can directly create a MediaPlayer object from a resource file or file path, and automatically complete some initialization work of MediaPlayer, such as setting the data source, buffer size, etc. Specifically, the MediaPlayer.create() method accepts two parameters: context and resource ID or file path, it will automatically call the constructor of the MediaPlayer class and set parameters such as data source, and finally return a prepared MediaPlayer object.

However, using new MediaPlayer() requires manual setting of MediaPlayer's data source, buffer size and other parameters, which requires additional code to complete. For example, you need to call the setDataSource() method to set the data source, call the prepare() method to prepare the MediaPlayer, and so on. It should be noted that if you choose to use the new MediaPlayer() method to create a MediaPlayer object, you need to call the release() method at an appropriate time to release the resources it occupies.

Therefore, if you need to quickly and easily create a MediaPlayer object and play an audio file, you can use the MediaPlayer.create() method. But if you need more control and customization, such as dealing with buffering during playback, setting a specific playback mode, etc., you need to use the new MediaPlayer() method to create a MediaPlayer object and manually configure parameters.

5. Repeat initialization:

When calling the MediaPlayer.create() method, the system will create a new MediaPlayer object for each call, and automatically release the resources related to the object after the call is completed. Therefore, calling the MediaPlayer.create() method multiple times does not cause performance problems.

However, it should be noted that each MediaPlayer object will occupy a certain amount of memory resources, so if you frequently create and release MediaPlayer objects in your application, it may have a certain impact on the memory resources of the system, causing the application performance issues. If you need to play different audio files frequently, you can consider using a single MediaPlayer object, and call the setDataSource() method to set different data sources when you need to play different files.

In addition, it should be noted that if multiple MediaPlayer objects exist and play audio files at the same time, multiple audio files may be played at the same time, resulting in confusion and poor user experience. Therefore, in the application, it is recommended to use a single MediaPlayer object to play the audio file, and when the audio file needs to be switched, first call the reset() method to clear the previous state, and then call the setDataSource() method to set a new data source. And re-call the prepare() method to prepare the MediaPlayer object.

6. Regarding incomplete playback:

It is said on the Internet that the playback is incomplete because the MediaPlayer object may be recycled during the playback process, and the object needs to be declared as a member variable. After testing, the incomplete playback rate can be reduced to a considerable extent, but it cannot be completely avoided.

If you know, please leave a message

Guess you like

Origin blog.csdn.net/set_one_name/article/details/130624586