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 ();
}
}