My music application constantly plays music in the background, however I'd like to be able to detect when another application starts playing audio (such as the YouTube app) so I can pause/mute/stop the audio in my application.
This will allow a user to continue browsing the web whilst listening to music, but then if they wish to watch a video at any point, they can do so without audio conflict.
One solution might be to listen for a broadcast which states when an application begins using the AudioManager. Does such an Intent Action exist?
Edit: As in the answer provided below, there appears to be a method of detecting the loss of audio focus in 2.2 with AudioManager.OnAudioFocusChangeListener.
Great, but is there a solution for the more common versions of Android? Ideally 1.5+.
http://developer.android.com/reference/android/media/AudioManager.OnAudioFocusChangeListener.html
this thread also has additional information that might get you heading in the right direction.
http://groups.google.com/group/android-developers/browse_thread/thread/db6822d84feaac6/219d8cba07795c61?hl=en&lnk=gst&q=OnAudioFocusChangeListener#219d8cba07795c61
Related
I need to develop an Android Service (API level > 21), which should be able to detect video playback on a third party Video player, so that it can further disable the Notification pop-ups during a video playback.
On searching, I found several posts talking about detecting the play-states of videos in a VideoView, such as the following:
How to detect when VideoView starts playing (Android)?
But, I couldn't find anything that can help detecting video playback on a video player, wherein, the video player is installed as a third party app and is not a part of the Android application/ service.
So, the challenges are:
1) The android service needs to detect a video playback
2) Registering package names for video player apps with the Service is out of question, since the Service should be able to detect the playback even when a new video player is installed from the Playstore.
3) Need to extend this idea for online video streaming as well.
Any help in this regard would be really appreciated.
Thanks in advance.
There is no way to implement 100% reliable video playback detection :(
Just and idea: try listening to the audio focus changes... this might help with detection of video playback, assuming the target player respects the audio output stream usage policy, recommended by Google. You might also want to get notified about system UI visibility changes (to detect fullscreen playback).
Audio focus:
https://developer.android.com/guide/topics/media-apps/volume-and-earphones.html
System UI visibility changes:
https://developer.android.com/reference/android/view/View.OnSystemUiVisibilityChangeListener.html
Also, you are correct saying that package name based detector is not an option since detecting active/running package is no longer possible on android 7.
I'm looking to make an Android app that can process music being played by another app. More specifically I want to analyze the raw audio. I've done some research and looked around, but I'm beginning to think this isn't possible, and maybe for good reason.
Does anyone have any ideas on how I might be able to achieve this capability?
as your requirement is to analyse all aps audio and want to process raw audio too.only one possibility is there which is to start recorder in background service and analyse audio all the time like many of the call recorder app on play store is doing to record a call.
it does have some drawbacks too
background noise
you will not able to know when music starts and when stops.
I was wondering how to exactly stop all other music that may be playing when my app is opened whenever there is music being played in my application. I have a couple instances where I just have sound effects, so is there anyway I can stop music that may be playing already whenever these sound effects pop up? I know I'm not giving any code, however, I have no idea how to approach this and haven't found many other questions asking similar things.Thanks!
Look at the AudioManager.requestAudioFocus API. Remember to release the focus when you're done with it, or other apps may not be able to play sounds correctly.
My app plays an audio file. I want my app to pause the audio playing if another apps starts to play audio. I don't want the two audio sounds mess up each other. My app will resume the audio playing after the other app finishes playing its audio. How can I make it? Thanks.
Depending on how your application is running ie. while app is running in foreground (users is looking at your screen) or if your running a "service" which is currently in the background and you wish music to continue playing (songza, slacker radio etc.) how you handle these things will be different. Android uses internal system states to determine which apps should be out-putting audio to the speaker. All this is handled by the audio manager requestAudioFocus() releaseAudioFocus() will be the system calls you will want to look at the most. For a more detailed explanation see android developer api where you this is explained and documented quite well http://developer.android.com/guide/topics/media/mediaplayer.html
I've decided to try to make a little Android application that streams live audio from my server. I'm using a HTML5 audio tag for the audio. But, on some devices when the user goes to the browser or their messages the music stops playing. Would I need to create an ongoing notification so the audio would continue to play? Or, would something else be better? I'm using PhoneGap too, I don't know if there's a plug-in out there that I've missed that could solve this. I'm trying to learn all of this on my own, but I'm just confused at this point.
I think it ended up being a problem with the app manager just closing the app.