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.
Related
I was asked this question in an interview. I am not sure if this the right forum to ask this question because it does not involve any code, but an understanding of android concept.
The question is 'Why do we need a service when everything can be done by a background thread in Android?'
Service runs in the main thread, why do we need something that runs in the main thread but in the background?
Examples like music play can be done in the background thread also so why do we need a service.
Please let me know if this should be asked in another forum.
Service:
Service is like Activity. but it doesn't require UI to work with. whereas when the thread created from the activity , thread will run until the activity lives. so, if you play the music in thread it will run. but it will crash when the activity ends, whereas when you implemented the music playing from service it will run entire the life cycle of the service.
See my answer boundservice to communicate between service and activity. when your music player runs you have to maintain the notification on notification panel.
UPDATE
When you are playing the music with background thread, the music will play even the app closed your background thread will become orphanage thread. You cannot control the state of the music player. Whereas when you are working with service ,music will play and it won't become orphanage service when the app exits. When you recreat the app you can communicate with music player whereas background thread cannot.
I know that a process is an executing instance of an a program running in the foreground or background and that background processes run asynchronously(runs outside the main thread).
Would background music in your application be an example of a background process?(doesn't freeze up your UI in the main thread and it runs in its own thread)
Does process imply then that another program is running the music in the background?
I don't think that you need another process for playing music - you just need another thread. I don't think that you want to play music while your app is not in the foreground. For example if your app is a game which only produces sounds when it is active.
Obviously this is not true if your app is a media player which still plays music while in the background letting user interact with it using notifications which let the user play/pause, skip a song or stop playback.
Please see a question like this one: How to put media controller button on notification bar?
Your android application consist of process, services, threads, message queues. It application developer choice when to use what. As good developer, you should always try to make you application user experience smooth without and any hang. Always perform heavy/time consuming activity with service or async threads, and avoid such activity on main thread as it cause UI hangs.
I guess I still need to learn how Android apps flow. The title might not have been clear, so let me explain.
Situation:
I have a game which has a few different activities. For example, MenuActivity, GameActivity, and HowToActivity. The game starts at MenuActivity and plays a song set to loop. To have the same song play during MenuActivity and HowToActivity, uniterrupted, I have the song played from an implemented Application. If I press the home button, get a phone, or whatever, the song will continue to play. To prevent that, I need to stop the song when leaving the app.
Problem:
Currently, in MenuActivity, I have code to stop the song under the protected void onStop() function. This stops the song when leaving the app (Pressing the Home button, get a phone call), but it also stops the song when changing to another activity within the app, such as HowToActivity. So the question is, how can I tell the difference?
Jesse,
You need to have a service that will do the job of playing the song.
You can easily start the service from any of the activity of your application. Also the service can be stopped by any of the activity.
Hence in the activity onCreate(), you can start the song player service, that will play the song even if the activity dies and new activity starts. Once your application is done with the song playing, just call stopService().
I hope this will solve your issue.
~Rajan
Typically what happens is that people read the phone state using a PhoneStateListener:
http://developer.android.com/reference/android/telephony/PhoneStateListener.html
This is why so many apps need the READ_PHONE_STATE permission, they're making sure you aren't answering a call while the app goes off and continues to do something annoying. You can create a listener to check when things like this happen. You shouldn't really change the behavior of the home key (and can't!), but instead, you can always listen for things like onPause() and onStop().
You probably want a background service that actually does the music playing, etc.., and then you want to control this service from your actual app when you get lifecycle events inside activities. This makes your app a bit more modular (i.e., the thing that it's doing semantically is control the sound, download the stream, whatever), because the Activities control the UI, and the Services what happens behind the scenes.
Edit: tutorial for MediaPlayer from a service:
http://marakana.com/forums/android/examples/60.html
You might also want to look into using a wake lock, though it might not be strictly necessary.
Create a receiver to capture the following intent:
Intent.ACTION_CLOSE_SYSTEM_DIALOGS
This will be called when the Home screen of the phone is launched. So you can stop music at that time rather than stopping onStop() of Activity.
But this will not help if the user launch an app by pressing Home key long time.
So try to play different musics on different Screens.
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
working on a podcast app I'm currently thinking how to implement a small player (which includes a progressbar for current time playing, play, pause, rewind and forward button) which will be displayed in several activities through my app.
For playing podcasts in the background I've already implemented a Service which takes care of the MediaPlayer and the currently played podcast.
What is the best method for updating a small player over several activities and be able to pause and move the currently played podcast?
Thanks in advance.
You're on the right track. Every Activity that can control or see the mediaplayer should bind to the running service. If you implement the Observer design pattern in your service. Then you can make direct calls to the service and perform callbacks from the service to your activity.
Please make sure you play your audio/video in the background in a seperate thread because a service and an activity run in the same thread.
For your progressbar i should implement a callback function like progress(int secondsFromStart, int totalTimeSeconds) that will be called immediately after binding the service. Then the UI could update it's progressbar until it reaches totalTimeSeconds or shenever some kind of pauze call was received from the service.
If you want to use the same Player widget in several activities, then you should try Fragments API. That will alow you to compose complex UI. All your Activities will become Fragments with minor changes in code.