user2556754
user2556754

Reputation: 23

Why abandonment of Audio Focus in Android application does not work?

I'm trying to write some kind of modified Timer on Android. After pressing a button a new thread is started, it calculates time and in some situations it should play sound. During the sound playing I get the Audio Focus - MP3 Player which is playing in background, changes his state to "pause". But later I have problem - I call abandon focus method but MP3 Player is still paused. I can't find any advise in such situation, only information that I should call abandonAudioFocus() which I've done.

My code is below. I have changed some parts to make it shorter but it is still long. I've shown it all because I want to present whole organisation of the program. This is my first Android application so the error can be stupid :)

public class MainActivity extends Activity{

Button p1;
Thread thread;
MyRunnable rn;
private boolean mPaused = false;
MediaPlayer mp = null;  
AudioManager audioMeg;
int time = 0;
int interval = 0;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //set text and similar things
    PressStart startList = new PressStart();
    p1.setOnClickListener(startList);

    mp = MediaPlayer.create(this, R.raw.alarm2);
    audioMeg = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    //something more here
}

class PressStart implements OnClickListener{
    @Override
    public void onClick(View arg0) {
        //calculate time and interval
        if(mPaused == false){
            rn = new MyRunnable();
            thread = new Thread(rn);
            thread.start(); 
        }
        else{
            rn.onResume();
        }
    }
}

class AFChangeListener implements OnAudioFocusChangeListener{
    @Override
    public void onAudioFocusChange(int focusChange) {
        if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){
            mp.pause();
        } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
            mp.start();
        } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
            mp.stop();
        }   
    }
}

class MyRunnable implements Runnable {
    private Object mPauseLock;

    public MyRunnable() {
        mPauseLock = new Object();
        mPaused = false;
    }

    @Override
    public void run() {
            int focus;
            AFChangeListener afChangeListener = new AFChangeListener();
            for(int i = 0; i < time + 1; i++){
                synchronized (mPauseLock) {
                    while (mPaused) {
                            mPauseLock.wait();
                    }
                }                   
                if(!mPaused){                   
                    if((i % interval == 0)){

                        //================================

                        focus = audioMeg.requestAudioFocus(afChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
                        if(focus == AudioManager.AUDIOFOCUS_REQUEST_GRANTED){
                            mp.start();
                        }
                        //while(mp.isPlaying()); when it is uncommented abandon focus still doesn't work
                        audioMeg.abandonAudioFocus(afChangeListener);

                        //=========================================
                    }
                    //something more here   
                    Thread.sleep(1000);
                }
            }   
    }
}

}

Upvotes: 2

Views: 4662

Answers (1)

Ryan S
Ryan S

Reputation: 4567

Instead of using AUDIOFOCUS_GAIN use AUDIOFOCUS_GAIN_TRANSIENT.

This is because audiofocus_gain signifies to all apps to stop their playback because you have no idea how long you will have focus while transient means that apps should pause until you abandon focus.

Read more: http://developer.android.com/reference/android/media/AudioManager.html#AUDIOFOCUS_GAIN_TRANSIENT

Upvotes: 11

Related Questions