how to know when audio is started playing in android

拥有回忆 提交于 2019-12-02 00:27:17

问题


In my application I want to get start time of music player when user started and end time when it stops.I don't want to start any music player in my app. I just want to track user activity in device. So i want my application to get any notification when user started the music .

Do I get any intent for music player started and it stopped.Or do I get any intent for when user opens music files.

Is there any other method other than intent to capture the start time and and time when user starts music player.


回答1:


Well you can implement the interface 'AudioManager.OnAudioFocusChangeListener' in which there is a method 'onAudioFocusChange' which lets you know if audio focus has been changed, and it can also tell if focus was gained or lost.

See this link, it explains the Audio Focus in detail. An application must gain audio focus through a request, and you can implement the 'AudioManager.OnAudioFocusChangeListener' and if there is focus gain, you can detect if the media player is running, (because some other application such as you tube may gain focus to play its audio), see this to see how to detect which application or service is currently running. You can find if audio has started to play, and if it was the media player or not.

Hope this helps. It was interesting question and I have learned some new things while searching for the answer!



来源:https://stackoverflow.com/questions/10783031/how-to-know-when-audio-is-started-playing-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!