Phonegap - continuous audio playback not working when the app is in background mode - android

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?

Related

Android Auto Music app no sound,MediaPlayer callback shows play success

I developed a music app to support Android Auto, MediaPlayer call back shows already playing, but no sound.
Then I found that if I choose Google Music to play a song first, then I switch to my app, everything runs perfectly.
Is there anything I did wrong or it is Android Auto's bug???
problem solved , request audio focus first

How to check if external media player is playing in android?

I want to stop my media player if external media player is playing.
I've implemented following code but it is called even when my app media player is playing.
if (audioManager.isMusicActive()) {
return;
}
How to distinguish between in-app media player and external media player?
Any help would be appreciated.
Two or more Android apps can play audio to the same output stream
simultaneously. The system mixes everything together. While this is
technically impressive, it can be very aggravating to a user. To avoid
every music app playing at the same time, Android introduces the idea
of audio focus. Only one app can hold audio focus at a time.
Further,
A well-behaved audio app should manage audio focus according to these
general guidelines:
Call requestAudioFocus() immediately before starting to play and
verify that the call returns AUDIOFOCUS_REQUEST_GRANTED. If you design
your app as we describe in this guide, the call to requestAudioFocus()
should be made in the onPlay() callback of your media session. When
another app gains audio focus, stop or pause playing, or duck the
volume down. When playback stops, abandon audio focus. Audio focus is
handled differently depending on the the version of Android that is
running:
You can check the more detail from developer link.

how to detect when Music is played using Native Music Player?

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.

Android media player not producing sound even playing

I have created a media player application using Android MediaPlayer API. The app is working fine unless a phone call is received. I am handling phone call events in my app and doing pause and play of media player.
The problem is that After pause and play, when phone call arrives, the song is playing but with no sound from speakers.
One more thing is This is happening when I am using bluetooth headset only.
If anybody faced this problem, please reply suggestions to my question.
I solved this problem by stopping bluetooth's SCO before playing a music track if it is ON.

how to stop the native android service playing music

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.

Categories

Resources