Android的:如何停止音乐服务我的应用程序,如果另一个应用程序播放音乐? [英] Android : How to stop music service of my app, if another app plays music.?
问题描述
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).
要做到这一点,你需要注册一个<一个href="http://developer.android.com/reference/android/media/AudioManager.OnAudioFocusChangeListener.html"相对=nofollow> OnAudioFocusChangeListener
。
To do this, you need to register an OnAudioFocusChangeListener
.
基本上,你必须:
- 请求音频焦点开始播放前。
- 只有开始播放,如果你有效地获取它。
- 在放弃的焦点,当你停止播放。
- 在处理音频焦点损失,要么通过降低音量暂时(回避),或者完全停止播放。
请检查href="http://developer.android.com/training/managing-audio/audio-focus.html" rel="nofollow">音频管理重点文章在Android的
Please check the Managing Audio Focus article in the Android documentation.
这篇关于Android的:如何停止音乐服务我的应用程序,如果另一个应用程序播放音乐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!