Android的:如何停止音乐服务我的应用程序,如果另一个应用程序播放音乐?应用程序、我的、音乐、Android

2023-09-07 12:17:37 作者:愚人自演i

1)在Android项目中,我写了播放音乐的后台服务。问题是,当我的应用程序正在播放音乐的背景和其他应用程序(音乐播放器)播放音乐,无论是音频同时播放。我想停止播放音乐在我的应用程序,如果有其他的应用程序播放音乐。我该如何处理这个?

1) In an android project, I have written a service that plays music at the background. The problem is when my application is playing music at the background and another application(music player) plays music, both the audios play simultaneously. I want to stop playing the music in my application, if any other app plays the music. How do I deal with this.?

推荐答案

这个概念被称为音频焦点在Android中。

This concept is called audio focus in Android.

在广义上讲,这意味着只有一个应用程序可以在一个时间点有音频焦点,如果一个电话呼叫时,或其他应用程序要播放音乐,你应该放弃,如果它要求(例如,及放; C)

In broad terms, it means that only one app can have audio focus at one point in time, and that you should relinquish if it asked to (for example if a phone call arrives, or another app wants to play music, &c).

要做到这一点,你需要注册一个 OnAudioFocusChangeListener

To do this, you need to register an OnAudioFocusChangeListener.

基本上,你必须:

请求音频焦点开始播放前。 只有开始播放,如果你有效地获取它。 在放弃的焦点,当你停止播放。 在处理音频焦点损失,要么通过降低音量暂时(回避),或者完全停止播放。

请检查href="http://developer.android.com/training/managing-audio/audio-focus.html" rel="nofollow">音频管理重点文章在Android的