Android-mediaplayer playback

Source: Internet
Author: User

Understanding the medaiplayer oddities:

1. Once you set the data source of a mediaplayer, you cannot easily change it to another

One-you'll have to create a new mediaplayer or call the reset () method to reinitialize the state of the player.

2. after you call prepare (), you can call getcurrentposition (), getduration (), and isplaying () to get the current state of the player. you can also call the setlooping () and setvolume () methods after the call to prepare ().

3. After you call start (), you can call pause (), stop (), and seekto ().

4. Every mediaplayer creates a new thread, so be sure to call the release () method when

You are done with the media player, the videoview takes care of this in the case of video playback, but you'll have to do it manually if you decide to use mediaplayer instead of videoview

 

// Method 1: Use mediaplayer to play the audio files in the local raw/directory.
Private void playlocalaudio () throws exception {
Mediaplayer player = mediaplayer. Create (context, R. Raw. music_file );
Player. Start ();
}
 
// Method 2: Use mediaplayer to play the audio file (including setdatasource () of the local raw/file ())
Private void playlocalaudio_usingdescriptor () Throw exception {
Assetfiledescriptor filedesc = This. getresources ()
. Openrawresourcefd (R. Raw. music_file );
Mediaplayer player = NULL;
If (filedesc! = NULL ){
Player = new mediaplayer ();
Player. setdatasource (filedesc, filedesc. getstartoffset (),
Filedesc. getlength ());
Filedesc. Close ();
Player. Prepare ();
Player. Start ();
}
}

Related Article

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.