为什么在Android应用程序中放弃Audio Focus不起作用?

时间:2013-07-06 19:15:10

标签: android audio

我正在尝试在Android上编写某种修改过的Timer。按下按钮后,新的线程启动,它会计算时间,在某些情况下它应该播放声音。 在声音播放期间,我得到了Audio Focus - 在后台播放的MP3播放器,将其状态更改为“暂停”。但后来我有问题 - 我称之为放弃焦点方法,但MP3播放器仍然暂停。我在这种情况下找不到任何建议,只有我应该打电话给 abandonAudioFocus()的信息。

我的代码如下。我已经更改了一些部件以缩短它但仍然很长。我已经展示了这一切,因为我想介绍该计划的整个组织。这是我的第一个Android应用程序,所以错误可能是愚蠢的:)

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);
                }
            }   
    }
}

}

1 个答案:

答案 0 :(得分:10)

而不是使用AUDIOFOCUS_GAIN使用AUDIOFOCUS_GAIN_TRANSIENT。

这是因为audiofocus_gain表示所有应用都会停止播放,因为您不知道有多长时间会有焦点,而瞬态意味着应用应暂停,直到您放弃对焦为止。

了解详情:http://developer.android.com/reference/android/media/AudioManager.html#AUDIOFOCUS_GAIN_TRANSIENT