Wednesday 15 May 2013

pause main background music on different activity Android -


itemprop = "text">

I'm developing an Android game. The background music of the game is in my main activity or intro activity. Overall, this is the main background music of the whole game. That's why I'm playing music:

  Private Zero startBgSound () {// TODO auto generated method stub // int sound file = R.raw.backgroundmusic; // property file descriptor afd = getResources (). OpenRawResourceFd (soundfile); Bgsound = new media player (); Bgsound.reset (); Bgsound = MediaPlayer.create (this, RRRbackground music); Bgsound.setLooping (true); Bgsound.setVolume (100, 100); Bgsound.start (); }   

By setting a loop to correct when the user should click on the play button (still on main activity), the next activity is at the top of the main activity but my second activity In, I have placed a punctuation button, so when the user clicks on it, the background music should also be stopped.

Do you have any clever ideas about doing this? I am stuck with this issue so I really appreciate it if you can help thank you.

In my game, I handle music with a stable class. Just reference it when you start playing music and you can stop music anywhere in your code.

Just to reset the class, make sure that when the player leaves the game or opens the game, then the stable classes, though the game is closed, still survive.

Here's an example:

  import android.content.Context; Import android Media. media Player; Public square SoundHandler {Private Stable Media Player background music; Private static reference references; Private steady boolean = liberation; Public set zero set set (reference reference) {reference = cont; } Public static zero playmaking (int resource) {if (backgroundMusic! = Null) backgroundMusic.reset (); Try {backgroundMusic = MediaPlayer.create (reference, resource); BackgroundMusic.setLooping (right); BackgroundMusic.setVolume (100, 100); If (! Has been provided) {backgroundMusic.start (); }} Hold (NullPointerException E) {// Media Player failed to create. It happens randomly without any obvious reasons. E.printStackTrace (); }} Public Stable Zero Setmoot (Boolean Mute) {if (backgroundMusic! = Null) {if (muted) {if (backgroundMusic.isPlaying ()) {backgroundMusic.stop (); IsMuted = true; }} And {if (BackgroundMusic.Playing ()) {backgroundMusic.start (); IsMuted = false; }}}} Public static zero left () {if (backgroundMusic! = Null) {backgroundMusic.release ();   

Remember to set the context in your on-Crete () and to run the session () function in your onDesto ().

No comments:

Post a Comment