gpt4 book ai didi

java - AUDIOFOCUS_LOSS 在 android 中打完电话后调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:09:22 24 4
gpt4 key购买 nike

我正在尝试在电话响起时暂停媒体播放器。我使用来自 android 站点的示例代码。是这样的;

public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
// resume playback
if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
mMediaPlayer.start();
mMediaPlayer.setVolume(1.0f, 1.0f);
}

break;

case AudioManager.AUDIOFOCUS_LOSS:
// Lost focus for an unbounded amount of time: stop playback and
// release media player
stopMediaPlayer();
break;

case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
// Lost focus for a short time, but we have to stop
// playback. We don't release the media player because playback
// is likely to resume
if (mMediaPlayer.isPlaying())
mMediaPlayer.pause();
break;

case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
// Lost focus for a short time, but it's ok to keep playing
// at an attenuated level
if (mMediaPlayer.isPlaying())
mMediaPlayer.setVolume(0.1f, 0.1f);
break;
}
}

当电话响起时发送AUDIOFOCUS_LOSS_TRANSIENT;没关系。当通话结束时,发送 AUDIOFOCUS_GAIN 并且播放器继续播放;这也可以。但是在发送 AUDIOFOCUS_GAIN 之后,AUDIOFOCUS_LOSS 就被发送了。知道为什么它会失去音频焦点吗?提前致谢。

最佳答案

要检查两件事:1)例如,您是否有一个播放按钮,单击该按钮后,请求焦点并开始播放?我有一次遇到问题,他们播放“滴答”声以提供点击反馈。发生这种情况时,他们会将音频焦点从我的应用程序中移开。在您的 XML 中,为该 Activity 中的所有按钮将此值设置为 false:

android:soundEffectsEnabled = 'false'

2) 检查是否安装了其他同样请求焦点的媒体播放器应用程序。他们是否在失去焦点后立即请求关注?如果是这样,除了警告您的用户不要安装此应用程序之外,您无能为力。

关于java - AUDIOFOCUS_LOSS 在 android 中打完电话后调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12334110/

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