views:

701

answers:

7

Hello! First time to ask a question here at stackoverflow. Exciting! Haha.

We're developing an Android game and we play some background music for our intro (we have an Intro Activity) but we want it to continue playing to the next Activity, and perhaps be able to stop or play the music again from anywhere within the application.

What we're doing at the moment is play the bgm using MediaPlayer at our Intro Activity. However, we stop the music as soon as the user leaves that Activity. Do we have to use something like Services for this? Or is MediaPlayer/SoundPool enough? If anyone knows the answer, we'd gladly appreciate your sharing it with us. Thanks!

+1  A: 

Do we have to use something like Services for this?

I would recommend it.

CommonsWare
Agreed, services are the way to go here.
r1k0
Even in the Fundamentals article on the Android website when the Service class is explained they use a music player as example.http://developer.android.com/guide/topics/fundamentals.html#appcomp
Daniel Velkov
Any idea why there isn't a music playing service as part of android? Or is there one?
Phil
Well, the Music app uses a service. However, like most of the Android apps, the Music app is not part of the SDK, so there is no documented means to tell it to go play stuff.
CommonsWare
+1  A: 

In my experience using Services for this is not a very good idea.

Background Music without using Services:

http://www.rbgrn.net/content/307-light-racer-20-days-61-64-completion

vladikoff
It's been a while since I posted this question. I should have updated it when we found the solution. The use of a static music manager rather than a Service seemed to be the best option for this. In fact the link that you posted is exactly the one that we based it from. God Bless Robert Green.
jhie
A: 

If I understand your situation correctly, then I have confronted the same problem a few times. I am using a different thread to play music in my applications. This implementation passes a static reference to a Context that I know will be alive for the time that the music will be playing.

public class AudioPlayer extends Thread {
private Context c;
private Thread blinker;
private File file;

public AudioPlayer (Context c, File file) {
    this.c = c;
    this.file = file;
}

public void go () {
    blinker = this;
    if(!blinker.isAlive()) {
        blinker.start();
    }
}

public void end () {
    Thread waiter = blinker;
    blinker = null;
    if (waiter != null)
        waiter.interrupt ();
}

public void run () {
    MediaPlayer ap = MediaPlayer.create(c, Uri.fromFile(file));
    int duration = ap.getDuration();
    long startTime = System.currentTimeMillis();
    ap.start();
    try {
        Thread thisThread = Thread.currentThread();
        while (this.blinker == thisThread && System.currentTimeMillis() - startTime < duration) {           
            Thread.sleep (500);  // interval between checks (in ms)
        }
        ap.stop ();
        ap.release ();
        ap = null;
    } catch (InterruptedException e) {
        Log.d("AUDIO-PLAYER", "INTERRUPTED EXCEPTION");
        ap.stop ();
        ap.release();
        ap = null;
    }
    }
}
Shade
A: 

You can try using the AsyncPlayer but you should keep a reference to the class in order to stop the sound from playing.

You can create a Uri to a local resource using Uri uri = Uri.parse("android.resource://com.stackoverlof.android/raw/ beep.mp3");.

Hope this helps.

Bloodsplatter
A: 

You can also create a service which play music using mediaplayer as below.

Intent svc=new Intent(this, BackgroundSoundService.class);
startService(svc); //OR stopService(svc); 

public class BackgroundSoundService extends Service {
    private static final String TAG = null;
    MediaPlayer player;
    public IBinder onBind(Intent arg0) {

        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();


        player = MediaPlayer.create(this, R.raw.idil);
        player.setLooping(true); // Set looping
        player.setVolume(100,100);

    }
    public int onStartCommand(Intent intent, int flags, int startId) {


        player.start();

        return 1;
    }

    public void onStart(Intent intent, int startId) {
        // TODO



    }
    public IBinder onUnBind(Intent arg0) {
        // TODO Auto-generated method stub

        return null;
    }

    public void onStop() {

    }
    public void onPause() {

    }
    @Override
    public void onDestroy() {

        player.stop();
        player.release();
    }

    @Override
    public void onLowMemory() {

    }
}
Maneesh
+1  A: 

Create a static SoundManager using either SoundPools or MediaPlayers.

Create a static flag called keepMusicGoing.

Start the music when the first activty is created.

When switching actvities set keepMusicGoing to true.

On the onStop event of your activities check if keepMusicGoing is true,if so leave the music on, then set keepMusicGoing to false.

If they press the home button the keepMusicGoing flag will be false so the music will stop when the activity loses focus.

Email me and I can send you a couple SoundManagers that I wrote one uses MediaPlayers and the other SoundPools

Chad

Chad
Yes, this was also how we did it. vladikoff posted the link to where we got the idea of using a static manager for the music.
jhie
A: 

I'm having the same problem.

I used a variant on the static SoundManager listed at the link above, but I can't figure out how to use the boolean variable to make it turn off the sound when my app goes to the background.

I want the same music to play all the time - on startup all the way up through when they quit or hit the home screen (but even if they quit or press home, the music keeps playing...)

Tojo