Android AudioFocus onAudioFocusChange 没有监听变化
Android AudioFocus onAudioFocusChange not listening for changes
我一直在构建一个 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;
}
}
我也尝试过 class 声明之外的实现并指定
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
我一直在构建一个 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;
}
}
我也尝试过 class 声明之外的实现并指定
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