Android communicating events from service to activity - android

I am building a music player in android for which I am using bound services to create mediaplayer and do all the background operations like playing media and pause etc like this.
All the UI components in mediaplayer are implemented in activity and I need to communicate events like mediaplayer is ready, media is playing, paused etc from service to activity to make changes in UI accordingly. What would be best way to do that?
Broadcasts from service to activity for various states is one way to implement it.

Related

How to extend life cycle of an activity object

I initialize a MediaPlayer object in my main activity to play music. I would like my app to keep playing music when the user closes the app. I guess when the user closes the app, the MediaPlayer object goes out of scope. How can I keep keep the music playing after the app is closed?
How extend life cycle of an activity object
You can't.
If you want to run code when your app is in the background, you have to use a Service
You're looking at this wrong. Extending the life cycle means opening the door to leaks. There's also a considerable amount of things to keep track of.
The recommended way is via a Foreground Service. Your activity ideally binds to your service and controls the music playback via the provided Binder interface.
You can also allow the service to be controlled directly through your service's Ongoing Notification.
An example of the the above is Google Play Music.
Good luck.

Service OR IntentService playing audio after recording

I want to achieve following goals:
Record Audio
Send Audio to Server
Play Audio
I know that First 2 tasks are possible by Using Service as I've done that in one of the previous apps but when it comes to playing an audio file, it needs to be triggered from an Activity.
Because accessing Activity from Service can be achieved by using BroadcastReceiver but what about accessing method of a Service from an Activity?
So, now comes the question: Whether to go for Service itself for Playing the audio also(triggered from activity) OR to use IntentService.
Documentation says,
No easy or direct way to interact with user interface directly from IntentService.
and
Any tasks started using IntentService cannot be interrupted
I may want to stop recording ant time and play it any time.
Which would suit t he requirement best --> Service OR IntentService ??
Any suggestions will be highly appreciated.
I would use a Service rather than an IntentService for what you need.
In particular, use a bound Service which allows two-way communication between the Activity which binds to it and from the Service to the Activity.
The IntentService class is designed for one-shot operations using its own worker thread, once the work on the thread is complete, the thread terminates and the IntentService calls stopSelf() to terminate itself. This means any user interaction between the user (via an Activity) and an IntentService is problematic.
A bound Service on the other hand will exist until it is either explicitly stopped or untill the last bound component unbinds. This allows for longer term interaction.
For playback, the fact a Service runs on the UI thread isn't an issue if you use something like MediaPlayerwhich handles its own thread for playback purposes.
Further to this, if you use a bound Service, MediaPlayer and MediaController, you can control play, stop, pause, seek etc from the Activity.
EDIT: For further information see... Bound Services

Why use service for background music in android

Why should service be used for playing background music in Android? Why not I just create another media player object loop the player to play the music?
Thanks!
Keyword is background. If you tie your Media Player object to an activity, you don't have the guarantee that your object will be around if your activity goes to the background. The GC will destroy it when resources become low.
Therefore using a Service is safer. And services are designed for running in the background unlike activities.

Player widget over several activities

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.

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

Categories

Resources