I am streaming music via an Android service. I am trying to stop-resume music when there's an incoming call. I don't have any problems stopping the music, I'm shutting down the service by calling
stopself()
method.
My question is, how can I restart the service again when the phone state is idle again from within the service after closing itself? I have to assume that the activities might not be there since the music is running in the background.
Do not stop the service using stopSelf in this case.
Your service should implement AudioManager.OnAudioFocusChangeListener.
When your application needs to output audio such as music or a notification, you should always request audio focus. When you loose audio focus you can stop the media player or reduce the volume. You can start back or increase the volume when audio focus gain.
Read Handling audio focus in the link
http://developer.android.com/guide/topics/media/mediaplayer.html
Related
I have created a Service to implement a Music Player functionality and i am binding to the service.
Everything is working fine, the only problem is that when I kill my app while the song is playing, it kills the app, the notification also goes away but the song keeps on playing in the background without any notification, and to stop the audio, the app needs to be launched again.
But when i clear all tasks (kill all the apps) while the while a song is playing, it kills the app as well as the Song, which is correct.
I need a solution to stop the Audio when only my App is killed.
I have implemented onTaskRemoved() callback but the control doesn't always come there.
Can anyone help me here ? Thanks in advance.
Seems like the app is not disconnected fully from the service,
Try to call unbindService().
If your client is still bound to a service when your app destroys the client, destruction causes the client to unbind. It is better to practice to unbind the client as soon as it is done interacting with the service. Doing so allows the idle service to shut down.
Try looking at Managing the lifecycle of a bound service
https://developer.android.com/guide/components/bound-services#Lifecycle
I am currently making an app the will control how to start and stop the mediaplayer on Android devices. Only music. I only want my service running when the music player is active in the background or in the front. I want to do this to save on battery life so its not running the whole time.
I have come across this AudioManager.isMusicActive() a few times, but not sure if that is ONLY for if the music is playing in the background.
For some background, my app will start, stop, skip songs, and pause music.
The proper way to control when your service is running is to start it when a song starts and stop it when a song finishes or is paused. This will help you not have to poll to see if music is currently running.
However, if you absolutely have to poll to see if a service is running you can use the following:
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (YourServiceClass.class.getName().equals(service.service.getClassName())) {
service_running = true;
}
}
I have an application that sets a rtsp stream of a song. When I push the "play" button on the application, the music starts playing. Then, when I push the "pause" button, the music is paused.
However, while the music is paused, if I receive a phone call, answer it, and then finish the call, the paused music AUTOMATICALLY restarts to play!! I DON'T want that to happen (because the user left the music paused)! I want the music to remain paused.
How do I fix this? I guess it's an Android problem!
PS: Just as a note, the oppose situation works fine, that is, if I receive a call while a music is being played, the Android does the right thing: it pauses the music for me to answer the call and, after the call is finished, the music starts to play again from where it was.
Your app has an implementation of PhoneStateListener somewhere that pauses the music when a call begins and restarts it when the call ends. You need to save the current state of the music (paused or playing) when a call begins and when the call ends, only start playing the music if it was playing when the call started. It's difficult to be more specific without your code but this question has an example implementation.
Could probably use more info, but just adding your pauseMusic() function in the activity's onPause() event should do the trick
I am writing an app that uses a media player. I want to start the media player using the service so that the music can be played even if the application is closed (onDestroy is run). Should I use BindService (for control the service) and not unBinding that?
the life cycle of the Service would be a bit tricky, start at playing screen created and stopped when either the music is complete in background or music is stopped when the app is finished (onDestroy)
how should i implement my service to best fit the above case i need?
Should I use BindService (for control the service) and not unBinding that?
No. You should call startService() to start it and stopService() to stop it (e.g., when the user presses the Stop button).
I have written a Music player app and it works great but when a flip action happens or when I return the the player view I have to stop the player and restart it at the postion that it was at when the action happened. That all works but it means a breif stop and start.
How can I run the media player in a different thread and still update my seekbar?
Thanks
Try running the media player within a foreground service. It's a little bit of work, but it's how it should be done anyways.
The service will handle running everything in a background thread and can be set up to post updates to your UI through messages or callbacks.
In addition, a Foreground service does not need to be tied to an activity, so it can continue to run even if you leave your player screen. The service must provide an Ongoing notification to the user that will probably display the current song, artist, click to pause, etc.