Search code examples
androidservicebackgroundrecording

How to accomplish task of playing recording in background and work on different activities?


I am bit new to android development, I have came across a scenario and wanted to have valuable feedback from experts.

In my application user is on Activity1, from where user selects a client and is navigated to Activity2. On Activity2 user shall be able to play a recording file, while the recording is being played, user can navigate to Activity3, Activity4 or Activity5, listens to audio and make the entries. On Activity3, Activity4 and Activity5 user will have controls to pause or stop the audio. And if user navigates back from Activity2 to Activity1, the audio will stop automatically.

I am confused that either it can be done through service or background task.

Any valuable suggestions/code sample is highly appreciated.


Solution

  • I have created a working sample for you to understand the concept fully. I created a service as you wanted to play the media inside service.

    Otherwise, service is not needed in this case, rather we can directly use the MediaPlayer api to play/pause and stop songs, by keeping a singleton object.

    Also, the player controls need not to be on each activity screen, rather they can be part of your action bar.

    Let me know if you are interested in seeing an example where player controls are in action bar and singleton used for media play instead of having seperate service as in my example, I will create a project and post the same.

    ( Tested in Note2 and Nexus emulator )

    URL of the project : Posted in Git Hub.

    https://github.com/gansBhat/AndroidProjects/tree/master

    Also posted in Google drive

    https://drive.google.com/folderview?id=0BxHClVwHSqq5dVRvT1Qyd0hYN0k&usp=sharing