Developing multimedia software MediaPlayer on Android

Source: Internet
Author: User

In the previous article, we introduced how to build an Eclipse Android development environment under Ubuntu. Now we will introduce how to develop the MediaPlayer multimedia software on the Android platform. MediaPlayer can be used to play videos, audio, and streaming media. The lifecycle of MediaPlayer is as follows:

 

From the MediaPlayer lifecycle diagram, we can see that using MediaPlayer is not very complex.

The simplest process is initialization-> resrt ()-> setDataSource ()-> prepare ()-> start ()-> ...... You only need a few simple steps to play the audio file.

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

1: private Handler handler = new Handler (){
   2:  
   3:         public void handleMessage(Message msg) {
4: // update the 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 the data source
  22:             mediaPlayer.setDataSource(filePath);
23: // prepare for playing
  24:             mediaPlayer.prepare();
25: // obtain the playback duration
  26:             length = mediaPlayer.getDuration();
  27:             progressBar.setMax(length / 1000);
28: // play
  29:             mediaPlayer.start();
  30:             new Thread(new ProcessBarRefresh()).start();
31: // set the listener after playing
  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. To develop a player, you must pay attention to a lot of details in addition to the UI.

In Android, music playback is a background process. When the above Code calls onStop ()-> onCreate (), the playlist will be repeatedly loaded, resulting in duplicate playlists. This requires onSaveIntanceState (). In this way, when initializing the playlist in onCreate (), you can first determine whether the Bundle is empty and whether the playlist can be obtained. If not, you can re-initialize the playlist.

In addition, Android also provides a Provider property object such as MediaStore for our development and utilization.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.