Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop multimedia software MediaPlayer under Android platform

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report