Android AudioFocus onAudioFocusChange不监听更改

2020年1月7日 18点热度 0条评论

我一直在构建MediaPlayer应用程序,一直在尝试让我的Mediaplayer应用程序处理AudioFocusChange事件。但是,无论我如何构造代码,我都不会监听AudioFocusChange事件,而是继续在其他应用程序之上播放。一切正常,除了我单击应用程序中的“播放”按钮时,它只是在其他音乐应用程序之上播放音乐而不会暂停它。我试过了

public void play() {
Intent("com.android.music.musicservicecommand");
        i.putExtra("command", "pause");
        MainActivity.this.sendBroadcast(i);
        int requestAudioFocus = AM.requestAudioFocus(afChangeListener, AudioManager.AUDIOFOCUS_GAIN, AudioManager.STREAM_MUSIC);
        if (requestAudioFocus == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            Toast.makeText(this, "Focus Granted", Toast.LENGTH_SHORT).show();
            if (!mediaPlayer.isPlaying() || mediaPlayer == null) {
                mediaPlayer.start();
                handler.removeCallbacks(moveSeekBarThread);
                handler.postDelayed(moveSeekBarThread, 300);
            }
        } else {
            Toast.makeText(this, "Focus Not Granted", Toast.LENGTH_LONG).show();
            return;
        }
    }

在我play()方法的顶部,但是它仍然在该顶部播放其他应用程序的音乐。

到目前为止,我有:

public class MainActivity extends AppCompatActivity implements             
AudioManager.OnAudioFocusChangeListener {

...

}

其次是监听器块:

@Override
    public void onAudioFocusChange(int focusChange) {
        switch (focusChange) {
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                Toast.makeText(this, "FOCUS LOSS TRANSIENT", Toast.LENGTH_SHORT).show();
                pause();
                break;
            case AudioManager.AUDIOFOCUS_LOSS:
                pause();
                Toast.makeText(this, "FOCUS LOST", Toast.LENGTH_SHORT).show();
                break;
            case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                Intent i = new Intent("com.android.music.musicservicecommand");
                i.putExtra("command", "pause");
                MainActivity.this.sendBroadcast(i);
                AM.setStreamVolume(AudioManager.STREAM_MUSIC, AM.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
                play();
            case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
                stop();
                break;
            case AudioManager.AUDIOFOCUS_REQUEST_GRANTED:
                Intent iARG = new Intent("com.android.music.musicservicecommand");
                iARG.putExtra("command", "pause");
                MainActivity.this.sendBroadcast(iARG);
                AM.setStreamVolume(AudioManager.STREAM_MUSIC, AM.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
                play();
                break;
        }
    }

我还在类声明之外尝试了实现,并指定了

AudioManager.OnAudioFocusChangeListener = new onAudioFocusChangeListener(){
public void onAudioFocusChange(int focusChange){
...
}

但是,它也没有监听focusChange的

在每个切换案例中都实现了敬酒,以通知我Focus状态何时发生了变化,但是我从没有看到任何通知。我也尝试放入方法:

int requestAudioFocus = AM.requestAudioFocus(null, AudioManager.AUDIOFOCUS_GAIN, AudioManager.STREAM_MUSIC);
        if (requestAudioFocus == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            Intent i = new Intent("com.android.music.musicservicecommand");
            i.putExtra("command", "pause");
            MainActivity.this.sendBroadcast(i);
}

但是,它只会停止另一个应用程序中的音乐播放一次。我有种感觉,我已经忽略了一些简单的事情,只是无法使它正常工作。

解决方案如下:

这是一个简单的错误,而不是为侦听器实例化“this”(实例),而是使用我先前定义但忘记删除的替代侦听器。由于侦听器是在MainActivity中实现的:

public class MainActivity extends AppCompatActivity implements AudioManager.OnAudioFocusChangeListener {

AM.requestAudioFocus(afChangeListener <-- should have been 'this',
                // Use the music stream.
                AudioManager.STREAM_MUSIC,
                // Request permanent focus.
                AudioManager.AUDIOFOCUS_GAIN);

requestAudioFocus()声明不正确。

焦点更改的开关盒已正确编写:

@Override
    public void onAudioFocusChange(int focusChange) {
        switch (focusChange) {
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                Toast.makeText(this, "FOCUS LOSS TRANSIENT", Toast.LENGTH_SHORT).show();
                pause();
                break;
            case AudioManager.AUDIOFOCUS_LOSS:
                pause();
                Toast.makeText(this, "FOCUS LOST", Toast.LENGTH_SHORT).show();
                break;
            case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                Intent i = new Intent("com.android.music.musicservicecommand");
                i.putExtra("command", "pause");
                MainActivity.this.sendBroadcast(i);
                AM.setStreamVolume(AudioManager.STREAM_MUSIC, AM.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
                play();
            case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
                stop();
                break;
            case AudioManager.AUDIOFOCUS_REQUEST_GRANTED:
                Intent iARG = new Intent("com.android.music.musicservicecommand");
                iARG.putExtra("command", "pause");
                MainActivity.this.sendBroadcast(iARG);
                AM.setStreamVolume(AudioManager.STREAM_MUSIC, AM.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
                play();
                break;
        }
    }

正确编写MainActivity的实现后:

public class MainActivity extends AppCompatActivity implements AudioManager.OnAudioFocusChangeListener