Get the App
SLTechnology News&Howtos  ›  Development  › 

How to develop multimedia software MediaPlayer under Android platform

Shulou Source: shulou.com Published: 2022-06-02 16:01:25 10月04日 Update

How to develop multimedia software MediaPlayer under the Android platform, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Now we will introduce how to develop the multimedia software MediaPlayer under the Android platform. MediaPlayer can be used to play video and audio, streaming media. The life cycle of MediaPlayer is shown below:

From the life cycle diagram of MediaPlayer, you can see that using MediaPlayer is not very complicated.

The simplest process: initialize-> resrt ()-> setDataSource ()-> prepare ()-> start ()->... With a few simple steps, you can play the audio file.

Let's take a look at an example of playing audio, look at the code and comments.

1: private Handler handler = new Handler () {2: 3: public void handleMessage (Message msg) {4: / / Update progress bar 5: progressBar.setProgress (progressState); 6:}; 7:} 8: 9: / * * 10: * play 11: * 12: * @ param filePath 13: * File path 14: * / 15: private void playMusic (String filePath) {16: try {17: progressState = 0; 18: progressBar.setProgress (0) 19: / reset 20: mediaPlayer.reset (); 21: / set data source 22: mediaPlayer.setDataSource (filePath); 23: / ready to play 24: mediaPlayer.prepare (); 25: / get playback duration 26: length = mediaPlayer.getDuration () 27: progressBar.setMax (length / 1000); 28: / / playback 29: mediaPlayer.start (); 30: new Thread (new ProcessBarRefresh ()) .start () 31: / set to listen after playback 32: mediaPlayer.setOnCompletionListener (new OnCompletionListener () {33: @ Override 34: public void onCompletion (MediaPlayer mp) {35: nextMusic (); 36:} 37:}) 38: 39:} catch (Exception e) {40: displayToast (e.getMessage ()) 41:} 42:} 43: 44: / * 45: * Update progress bar 46: * @ author Zhang Hanguo 47: * / 48: class ProcessBarRefresh implements Runnable {49: 50: @ Override 51: public void run () {52: while (isPlaying) {53: ProgressState = mediaPlayer.getCurrentPosition () / 1000 54: handler.sendMessage (handler.obtainMessage ()); 55:} 56:} 57: 58:}

The above is just a simple example. In addition to UI, you need to pay attention to a lot of details to develop a player.

Music playback in Android is a background process. When the above code calls onStop ()-> onCreate (), the playlist will be loaded repeatedly, causing the list to repeat. This requires the use of onSaveIntanceState (), so that when initializing the playlist in onCreate (), determine whether the Bundle is empty, whether you can get the playlist, and reinitialize if you can't get it.

In addition, Android itself also provides a MediaStore such a Provider attribute object for us to develop and use.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Development playlist audio multimedia platform software code cycle file time life progress help update complexity clarity thing alto example content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux vpn Apple Shulou Tech Info NVidia