In the previous article, we learned how to play music in a specified sdcard, but how to implement the sound effects in the game?
The game must be packaged and released with the program. Such music is not stored in sdcard, and is usually stored in "Res/raw" under the application directory. How can we play music?
Actually, it is very simple. I just changed the setdatasource () method to the CREATE () method to specify the mediaplayer object of the Resource Index.
Package xiaosi.mp3; import android. app. activity; import android. media. mediaplayer; import android. media. mediaplayer. oncompletionlistener; import android. OS. bundle; import android. view. keyevent; import android. view. view; import android. view. view. onclicklistener; import android. widget. imagebutton; import android. widget. textview; public class mp32activity extends activity {/** called when the activity is first c Reated. */private textview text = NULL; private imagebutton start = NULL; private imagebutton pause = NULL; private imagebutton stop = NULL; private mediaplayer = NULL; private Boolean isplaying = false; private Boolean ispaused = false; private Boolean isreleased = false; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main ); // Construct the mediaplayer object mediaplayer = new mediaplayer (); text = (textview) findviewbyid (R. id. text); Start = (imagebutton) findviewbyid (R. id. start); start. setonclicklistener (New startlistener (); pause = (imagebutton) findviewbyid (R. id. pause); pause. setonclicklistener (New pauselistener (); stop = (imagebutton) findviewbyid (R. id. stop); stop. setonclicklistener (New stoplistener ());} // ++ ++ ++ // Start playing private class startlistener implements onclicklistener {public void onclick (view V) {If (! Isplaying) {mediaplayer = mediaplayer. create (mp32activity. this, R. raw. f4); mediaplayer. setlooping (true); mediaplayer. start (); text. settext ("the music is playing ...... "); isplaying = true; isreleased = false; ispaused = false; mediaplayer. setoncompletionlistener (New oncompletionlistener () {public void oncompletion (mediaplayer MP) {mediaplayer. release ();}});}}} // ++ ++ ++/ /Pause private class pauselistener implements onclicklistener {public void onclick (view V) {If (mediaplayer! = NULL) {If (! Isreleased) {If (! Ispaused) {mediaplayer. pause (); ispaused = true; isplaying = true; text. settext ("playing paused ...... ");} else {mediaplayer. start (); ispaused = false; isplaying = true; text. settext ("play again ...... ");}}}}} // ++ ++ ++ // stop the private class stoplistener implements onclicklistener {public void onclick (view V) {If (isplaying) {If (! Isreleased) {mediaplayer. stop (); mediaplayer. release (); isreleased = true;} isplaying = false; text. settext ("Stop playing ...... ");}}} // ++ ++ ++ @ overridepublic Boolean onkeydown (INT keycode, keyevent) {If (keycode = keyevent. keycode_back) {If (! Isreleased) {mediaplayer. stop (); mediaplayer. release (); isreleased = true;} This. finish (); Return true;} return Super. onkeydown (keycode, event );}}
Source code download: Click the open link