gpt4 book ai didi

java - Android AudioFocus onAudioFocusChange 不监听更改

转载 作者:行者123 更新时间:2023-12-03 01:33:39 32 4
gpt4 key购买 nike

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

...

}

其次是监听器 block :
@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 的

Toasts 在每个 switch case 中实现,以通知我焦点状态何时更改,但我从未看到它们的任何通知。我也尝试输入方法:
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

关于java - Android AudioFocus onAudioFocusChange 不监听更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55449304/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com