java - Android AudioFocus onAudioFocusChange不会监听更改

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

在每个开关盒中都实现了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);
}

然而,它只会阻止其他应用中的音乐播放一次。 我有一种感觉,我忽略了一些简单的事情,我无法让它发挥作用。

这是一个简单的错误,而不是为侦听器实例化'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

转载请注明来自askonline.tech,本文标题:java - Android AudioFocus onAudioFocusChange不会监听更改


 Top