Imran Balouch
Imran Balouch

Reputation: 2170

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.

Upvotes: 2

Views: 483

Answers (7)

Robin Dijkhof
Robin Dijkhof

Reputation: 19278

I would definitely use a service. You can bind to a service and call methods from it.

Intent it = new Intent(MainActivity.this, MyService.class);
bindService(it, mConnection, Context.BIND_AUTO_CREATE);

Bind to the service in each activity so you can do whatever you want with it. Don't forget to unbind in your activity its' onDestroy.

if(mBound)unbindService(mConnection);

And simply stop playing by stopping the service.

stopService(new Intent(this, MyService.class));

This is the serviceConnection I use.

private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;            
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
}; 

Place this in your service to return itself to the activity:

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

public class LocalBinder extends Binder {
    MyService getService() {
        // Return this instance of LocalService so clients can call public methods
        return MyService.this;
    }
}

Finally, this will be called after starting your service startService(intent); Note that it returns Service.START_STICKY. This will prevent your service from being killed, unless the OS has very little memory.

public int onStartCommand(Intent intent, int flags, int startid) {      
    return Service.START_STICKY;
}

I hope my answere will help you and good luck with it.

Upvotes: 2

piotrpo
piotrpo

Reputation: 12636

All you have to do is to implement Service component: http://developer.android.com/reference/android/app/Service.html

You also should to mark it as foreground service in order to give him a bit more chance to survive in case lack of memory

http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)

Then I suppose you will need to communicate with the server according to send them some orders like start/stop playing etc. Most convinient way to do it in this particular case will be local broadcasting:

http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

you just need to call registerReceiver(...) in your service and use sendBroadcast(...) whenever you want to send some command.

Just in case - since Android 4.0 we usually do not use so much activities - consider using Fragment instead.

Upvotes: 0

Ganesh Bhat
Ganesh Bhat

Reputation: 243

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

Upvotes: 1

user1868091
user1868091

Reputation:

Activity 1 :

call service //start Service(new Intent(this, your Service.class))

Activity 2 :

stop service //stop Service(new Intent(this, your Service.class))

Service :

create media player //Media Player.create(this, file name.mp3); //media player.start();

Upvotes: 0

Kanwar_Singh
Kanwar_Singh

Reputation: 133

You need to use Services in your app,services accomplishes background tasks,they have no user interface,it will surely help you.

Upvotes: 0

Leonidos
Leonidos

Reputation: 10518

You can use Service as @Yjay suggested, but also you can create MediaPlayer instance using application context and share it between activities via app object/singletone/static fields.

Upvotes: 0

Yjay
Yjay

Reputation: 2737

You will want to use a Service containing a MediaPlayer, which your various Activities can interact with while it plays media in the background. Refer to the following link for a lot of info regarding setting up media playback in Android, specifically the Running asynchronously section:

Media Playback | Android Developers

Upvotes: 0

Related Questions