Android multimedia-MediaPlayer usage

Source: Internet
Author: User

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.


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.