Let's just talk about the code.
Public class SimpleMediaPlayerActivity extends Activity {// private static final Uri mMusicUri = Uri. fromFile (new File ("/sdcard/sound_file_1.mp3"); private static final Uri mMusicUri = Uri. parse ("http://czanxi.azone.artron.net/users_info/88/czanxi/2009121322260351292.mp3"); private MediaPlayer mMediaPlayer = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanc EState); playMusic1 ();}/*** Method 1: create */private void playMusic1 () {stopCurrentMediaPlayer (); mMediaPlayer = MediaPlayer. create (this, mMusicUri); // preparedmMediaPlayer. start (); // no need to call prepare (); create () does that for you}/*** method 2, which is created through new MediaPlayer, use prepare */private void playMusic2 () {stopCurrentMediaPlayer (); mMediaPlayer = new MediaPlayer (); // idlemMediaPlayer. setAudioS TreamType (AudioManager. STREAM_MUSIC); try {mMediaPlayer. setDataSource (getApplicationContext (), mMusicUri); mMediaPlayer. prepare ();} catch (IllegalArgumentException e) {e. printStackTrace ();} catch (SecurityException e) {e. printStackTrace ();} catch (IllegalStateException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} mMediaPlayer. start ();}/*** method 3, which is created through new MediaPlayer () and prep AreAsync */private void playMusic3 () {mMediaPlayer = new MediaPlayer (); mMediaPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC); mMediaPlayer. setOnPreparedListener (new OnPreparedListener () {@ Overridepublic void onPrepared (MediaPlayer mediaPlayer) {mediaPlayer. start () ;}}); try {mMediaPlayer. setDataSource (getApplicationContext (), mMusicUri); mMediaPlayer. prepareAsync (); // asynchronous loading} catch (IllegalArgum EntException e) {e. printStackTrace ();} catch (SecurityException e) {e. printStackTrace ();} catch (IllegalStateException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}@ Overrideprotected void onDestroy () {stopCurrentMediaPlayer (); super. onDestroy ();} private void stopCurrentMediaPlayer () {if (mMediaPlayer! = Null) {mMediaPlayer. stop (); mMediaPlayer. release (); mMediaPlayer = null ;}}}
The first method differs from the second and third methods:
New is the idle state, and create is the prepared state. This is why we do not need to manually call the prepare () method to change the status after calling create.
The differences between the first, second, and third types are:
The first two Synchronization Methods are involved. IO time-consuming operations such as identification and decoding may easily cause congestion in the main thread. The third method is asynchronous mode, which does not cause this problem. We recommend this method.