我的无声Android应用程序会打断前台其他应用程序的音频焦点。

huangapple go评论85阅读模式
英文:

My soundless Android app keeps interrupting the Audio focus of other apps on forground

问题

我的Android应用没有声音,每次应用程序变为可见(前台)时,它会不断中断后台运行的其他应用(如Spotify和Youtube Music)的音频焦点,从而导致后台运行的声音或视频暂停。

该应用程序是使用Unity制作的。我尝试在onResume()中自己获取音频焦点,然后释放它:

AudioManager am = (AudioManager) currentActivity.getSystemService(AUDIO_SERVICE);
int res = am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (res == AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
   am.abandonAudioFocus(null);

这种方法有效,但不一致。例如,对于Youtube Music,根本不起作用。

我正在寻找一种告诉操作系统“根本不要中断其他应用的音频焦点”的方法。

英文:

My android application that has no sound keeps interrupting the Audio focus of other apps running in the background such as Spotify and Youtube Music, every time the apps become visible (forground). Which then causes the sound or video running in the background to pause.

The app is made using Unity. I have tried to grab the Audio Focus myself onResume(), then release it:

AudioManager am = (AudioManager) currentActivity.getSystemService(AUDIO_SERVICE);
int res = am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (res == AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
   am.abandonAudioFocus(null);

This works, however not consistently. With Youtube Music for example, doesn't work at all.

I am looking for a way to tell the OS, "Do not interrupt the audio focus of other apps at all"

答案1

得分: 1

看起来在Unity的构建设置中,“静音其他音频源”的选项,如果设置为 true,可能是导致这些问题的原因。这一点记录得比较差,我之前以为它只是针对应用内音频的。我选择这个选项的原因是因为在 Unity 2018 与目标 Android SDK 版本 31 及以上会出现问题,如果没有勾选这个选项。

英文:

It seems that the option at build settings in unity "Mute other Audio Sources" is what's causing this issues if it was set to true. This was poorly documented as I though it is for the in app audios only. The reason I check this option is because unity 2018 combined with target android SDK 31 and above will cause issues if this option wasn't checked.

huangapple
  • 本文由 发表于 2023年2月16日 09:15:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定