Question

My app is a game designed for children ages 1 to 3 (I have two of my own in this range!)

I'm trying to respect the development life cycle, follow the guidelines for app design and make the experience do , start-to-finish, exactly what the users (parents and little tots) expect it to do.

When the game begins, I start (peaceful) background music through a service which implements AudioManager.OnAudioFocusChangeListener (to start, change the volume level, shut off, etc. depending on focus) that lasts through game play without interruption when switching between activities but with an option to toggle on and off as a small button on each game screen.

All works as I would expect it to, except when I push the home button to exit the app in Android fashion (ie. no exit button/option). The music keeps playing indefinitely. Is there something I can change in my switch (see code below)? I can't tell the service to stop on pause as that ruins the seamless background music between activities.

Is this a case to go against typical app design where it would be okay to create an exit option (a prompt when home is pressed with a cute little animal that says "Say bye for now?" and Quit and Cancel buttons)? Is there another option?

As always, thanks for guiding me as I stumble along! =)

@Override
public void onAudioFocusChange(int focusChange) {
    // TODO Auto-generated method stub 
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_GAIN:
        Log.v(TAG, "AUDIOFOCUS_GAIN");
        // resume playback
        if (mp == null)  {mp = MediaPlayer.create(this, R.raw.backgroundmusic); mp.start(); mp.setLooping(true);}
        else if (!mp.isPlaying()) mp.start();
        mp.setVolume(1.0f, 1.0f);
        break;

    case AudioManager.AUDIOFOCUS_LOSS:
        Log.v(TAG, "AUDIOFOCUS_LOSS");
        // Lost focus for an unbounded amount of time: stop playback and release media player
        if (mp.isPlaying())mp.stop();
        mp.release();
        mp = null;
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        Log.v(TAG, "AUDIOFOCUS_LOSS_TRANSIENT");
        // Lost focus for a short time, but we have to stop
        // playback. We don't release the media player because playback
        // is likely to resume
        if (mp.isPlaying()) mp.pause();
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        Log.v(TAG, "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
        // Lost focus for a short time, but it's ok to keep playing
        // at an attenuated level
        if (mp.isPlaying()) mp.setVolume(0.1f, 0.1f);
        break;
    }
}
Était-ce utile?

La solution

Yep, this is a very common issue in Android. Your application can go to the background from any activity at any time and your app does not get any kind of callback when this happens. Hopefully they can address this in future releases.

That being said, there are ways to accomplish what you want, but it's going to be a lot more complicated than you would think. Here's a good answer for exactly what you're looking for.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top