2

我正在尝试在 Android 上编写某种修改后的 Timer。按下按钮后会启动一个新线程,它会计算时间,并且在某些情况下它应该播放声音。在声音播放期间,我得到了音频焦点 - 在后台播放的 MP3 播放器,将他的状态更改为“暂停”。但后来我遇到了问题 - 我调用了放弃焦点方法,但 MP3 播放器仍然暂停。在这种情况下,我找不到任何建议,只有我应该调用的信息,放弃AudioFocus()我已经完成了。

我的代码如下。我已经更改了一些部分以使其更短,但它仍然很长。我已经展示了这一切,因为我想展示该程序的整个组织。这是我的第一个 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);
                }
            }   
    }
}

}

4

1 回答 1

11

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

于 2013-07-06T19:24:17.667 回答