Original address: http://www.cnblogs.com/wynet/p/5526905.html
Here are two ways to play a resource file:
The first kind,
The Assets class resource is placed in the assets subdirectory of the project root, which holds some original files that can be organized in any way. These files will eventually be packaged in the APK file in the original way. If we want to access these files in the program, we need to specify the file name to access. Add the music files to the
Add the Play () method.
protected voidPlay () {Assetmanager am=getassets (); Try{Mpmediaplayer.setdatasource (AM.OPENFD ("First.mp3"). Getfiledescriptor ()); Mpmediaplayer.prepare (); Mpmediaplayer.start (); } Catch(IllegalArgumentException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IllegalStateException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }
The second kind,
Add the Raw folder under the Res folder, add music files under the raw folder, continue adding music files, add Paly method
protected void Play () { mpmediaplayer=mediaplayer.create (this, r.raw.first); Try { mpmediaplayer.prepare (); Catch (IllegalStateException e) { e.printstacktrace (); Catch (IOException e) { e.printstacktrace (); } Mpmediaplayer.start (); }
The resources under the Res/raw directory, like the assets class resources, are packaged in the APK file, but they are given a resource ID so that we can access them through the ID in the program.
To achieve repeated playback of music files:
// Play Music repeatedly Mpmediaplayer.setoncompletionlistener (new Mediaplayer.oncompletionlistener () { @Override publicvoid oncompletion (MediaPlayer arg0) { mpmediaplayer.start (); Mpmediaplayer.setlooping (true); }
Android Learning Note (1)----Playing music files