Read more about how Android MediaPlayer is used

Source: Internet
Author: User

MediaPlayer has a very powerful function, the audio and video playback are provided support, in order to ensure that the system during playback normal operation, you need to set the "Android.permission.WAKE_LOCK" permission.

In MediaPlayer, according to the data source for the metadata, audio files, audio streaming different situations, has a corresponding processing process. The basic process is as follows:

1) Play meta-data

If the metadata is located under the "Res/raw" folder in Android engineering.     The process for playing meta data is: MediaPlayer mp=mediaplayer.create (context, r.raw.sound_file_1); Mp.start ();

2) Play audio file

If the path of the audio file is Path_to_file, the process for playing the audio file is: MediaPlayer mp=new MediaPlayer (); Mp.setdatasource (Path_to_file);     Set data source Mp.prepare (); Mp.start ();

3) Play audio stream

If the URL address of the audio file is url_adress, the process of playing the audio stream is: MediaPlayer mp=new MediaPlayer (); Mp.setdatasource (url_adress);     Set data source Mp.prepare (); Mp.start ();

Here is an example of Android that describes the process of playing, pausing, replying, stopping, and so on for Android MediaPlayer player audio files. It is important to note that in order to achieve a better user experience, the silent mode and the single-play mode must be processed when pausing and resuming playback.

Code Mute mode and single-mode processing

public class Audiomanagerstub extends Activity                    {                      private final int Mp3_to_play=r.raw.testmp3;                private MediaPlayer Mmediaplayer;                private Audiomanager Maudiomanager;                private Static Ctsresult Mctsresult; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP              @Override                 protected void OnCreate (Bundle savedinstancestate)     &NBSP;&NBSP;&NBSP;&NBsp;           {                             super.oncreate (savedinstancestate);                   // Get Audio Services                    maudiomanager= (Audiomanager) Getsystemservice (Context.audio_service);                             mmediaplayer=mediaplayer.create (this, MP3_TO_PLAY); Set Audio source                    mmediaplayer.setaudiostreamtype (audiomanager.stream_music);//Set Flow type                    mmediaplayer.setlooping (FALSE); Set whether to loop play                    mmediaplayer.start (); Start playing                }                      @ Override                protected Void OnPause ()                     {                          super.onpause ();                           try                             {                             for (int i=0; i < Audiosystem.getnumstreamtypes (); i++)                                     {                                                   maudiomanager.setstreammute (I, false); Unmute Other audio Apps &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;                  Maudiomanager.setstreamsolo (I, false); //set to not exclusively release mode                             }                             }                           catch (Exception e)                              {                                 mctsresult.Setresult (Ctsresult.result_fail);                                 finish ();                         }                    }                @ Override                protected Void Onresume ()                  {                     Super.onresume ();                    try                       {                        for (int i=0; i< audiosystem.getnumstreamtypes (); i++)                            {                               Maudiomanager.setstreammute (I, true); Set other audio apps mute                           maudiomanager.setstreamsolo (I, true); Set to single-Lay mode                       }                          }                         catch (Exception e)                            {                            mctsresult.setresult (CTSResult.RESULT_FAIL);                             finish ();                             return;                           }                         intent intent=new Intent ();                         intent.setclass (this, audiomanagerstubhelper.class);                         intent.setflags (Intent.flag_activity_new_task);                         startactivityforresult (Intent, 1);                  }      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp;    @Override                 protected void Onactivityresult (int requestcode, int resultcode, Intent data)       & nbsp;           {                    super.onactivityresult (RequestCode, ResultCode, data);                     Mmediaplayer.stop (); Stop playing                 Mmediaplayer.release (); Release Resources                 Mctsresult.setresult (CTSRESULT.RESULT_OK);                     Finish ();     &NBSP;&Nbsp;           }              }

Interpreting Android MediaPlayer detailed usage

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.