Android AudioFocus onAudioFocusChange不监听更改

时间:2019-04-01 06:39:10

标签: java audio android-audiomanager implements changelistener

我一直在构建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的

在每个switch案例中实现了Toa​​st,以通知我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);
}

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

1 个答案:

答案 0 :(得分:0)

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

var str = '7868HGCD';

console.log(str.match(/[a-zA-Z]/g).length == str.match(/[0-9]/g).length);

requestAudioFocus()声明不正确。

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

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

MainActivity的实现也正确编写了:

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