How to detect if media player is running - android

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;
}
}

Related

How to proceed playing music after app have been killed?

After android appication have been killed, all the inner threads are stoped. How to proceed playing music using service and threads? Should I keep time of music in killing time and re-play from that exact time?
EDIT
I do use service and it plays in backgound. The only thing I want it to play when the app have been killed by user, in other words the music must be contolled only via notification buttons and not be related to application lifecycle
Make your service a Foreground Service. Then it will run even after the app is killed.

AsyncTask stop on background

i'm building app for listening to acc stream. I m using this library:
https://code.google.com/p/aacdecoder-android/
Especially this player.
PlayerCallback clb = new PlayerCallback() { ... };
MultiPlayer aacMp3Player = new MultiPlayer( clb );
aacMp3Player.playAsync( "http://..." ); // URL of MP3 or AAC stream
It's playing in AsyncTask, but for some smarphones when you put app to backround playing just stop.
Only in methond onDestroy i'm stopping AsyncTask, not in onPause or oStop.
Anyone have an idea why player stops?
Short answer: Music streaming apps need to have a Service running to avoid being terminated by Android.
On a cheaper device or when memory resources run low, Android will simply terminate processes as soon as they go into the background or when more memory is needed. This likely explains why it only happens on "some smartphones" as you mentioned. You can also go into the Developer Options on your phone and play with the "Don't Keep Activities" and "Background process limit" settings to simulate the same thing.
You can read more about how Android prioritizes processes and activities for termination when they are not the active app in the foreground at this link here. You'll read that Services are at higher priority for being kept around than background processes (e.g. Activities that the user exited from, but still have a running thread).
On my music streaming app, when music starts, I start a service (same process as the Activity) and add my app's Main Activity to the Notification area. When the user stops the music, I stop the service and remove the app from the notification area. You'll likely notice very similar behavior between the NPR App, Spotify, i-heart-radio, and others.

How to Stop and Restart a Service from within?

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

Control service in GUI

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).

Android Media Player Threading

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.

Categories

Resources