I am developing a media app.
If a user plays a song with the native android application for playing a song and then opens my application then i want to stop playing that song in background before user opens my application. At present both songs are being played at a time.
You do not "stop playing that song in background before user opens my application". You request the audio focus using AudioManager.
You can have your program run the isPlaying() routine check if something is playing and take action against the media player if needed.
Related
I am currently making an application that uses the proximity sensor to play / pause and skip music. If a hand is near for under a second, it plays and pauses, if it's over a second it will skip the song. I have the "long" and "short" readouts measured properly, but I do not know how to play or pause music playing in another app from my app. (Ex: Say a person is listening to YouTube Music, I need my app to be able to toggle the play state).
What lines of code would let me play or pause or skip songs?
I am not sure about all apps. But this code works for some music app.
//Pause music
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
am.requestAudioFocus(null,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
//Play music
am.abandonAudioFocus(null);
I have built an audio streaming application for iOS and Android using Phonegap. I have used HTML5 Audio API to play audio files. The audio is being streamed from a PHP server.
When i try to play a song, it is playing fine when the application is in foreground and background/locked.
When i try to play a list of songs, the current song is playing till the end but the next song is not playing. This is working fine when the app is in foreground and not working when the app is in background/locked state.
Detailed Scenario:
Select a song from the playlist. The song is being played.
Push the application to background/lock the device. The current song is still playing till it ends.
While the app is in background, once the current song ends the next song is retrieved from the server. But it is not playing.
Now when i bring the app to the foreground, it immediately starts playing without even buffering. This is because that the song is already served from the server and buffered when it was in background.
Platforms and the issue: iOS (background & locked) & Android (background mode only. works fine in locked state)
I have done my research on this problem and all i could find was few solutions for native audio players. Have anyone solved this?
I am making an app in which I've made a Service which plays Music from URLs. The thing is that my music Service is playing music correctly BUT when user plays any song with Native music player then BOTH(Native Player and My Music Service) are playing music simultaneously. I want to stop My Music Service when user started playing music with native player.
Is there any Broadcast Intent which i can register to Detect the
music player is started?
Is it possible to detect Music player Started?
Any Other Solution?
Any suggestions would appreciated.
I'll suggest a different approach, that I believe it's the correct approach.
the issue on your approach is that you're suggesting to check for one specific app. And there're tons of different music players, plus radio players, plus video players, plus games... and all of those should stop your music in case they want to play something.
So how you do it?
It's all explained in the Android Developers website.
You have to register an OnAudioFocusChangeListener, so, whenever a different app request to have the audio focus, your app can stop the music.
Step 1: Detect if the user has opened native music app. For this , you need to know the package name of your native music app.
Then refer to my answer here: Android how to know an app has been started and range apps priority according the starting times
Using that , the list taskinfo will have the list of all running activities, and as explained there, the first element of the list will be the activity in the foreground.
STEP 2: Once you detect native music app being activated using STEP 1 (by polling for it in the background) , then stop your app's service.
NOTE: You should do this in a background (using asynctask) or another service.
NOTE 2: The limitation of this method is that you can't actually stop the music player when the user clicks play in the native music app, since this method will help you detect only if the native music app is opened or not.
I am playing audio from http link using media player and I release the mediaPlayer onStop() of the activity in which the mediaPlayer plays the song. I want the song to continue playing unless user exits from the app ie exits from the home page. how can i stop and release the media player from the home page?
You're going to need a Service that plays the music and then your App will just send message to the service.
Before open my application I played some of the music from default music player. With that background music I opened my application with the mediaplayer (android.media.MediaPlayer) to play the mp3 available in assets.
The default doesn't stop the music and I am getting two music concurrently from the device.
How do I stop or pause music of the default music player whenever my app started playing the music?
I think thats impossible. The default music player is a different app and out of your control. android does not allow controlling other app features from your app.
I had the same problem. I was trying to stop the background music of the default music player (if it is playing) when my VoIP SIP application receives an incoming call. I came up with:
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, true);
This does not stop or pause the music, but it make the music stream silent. Do not forget to unmute it later. This solution works well only if your second sound does not use the same stream.