Playing music (in-game sound effects)

Source: Internet
Author: User

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

 

 

 

 

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.