how to know when audio is started playing in andro

2019-02-27 13:35发布

问题:

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!