Priyank Prajapati
Priyank Prajapati

Reputation: 183

click on back button and MediaPlayer crash Android

For MediaPlayer I have referred http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/.

I have created activity for Media Player and it is having all the functionality like play, pause, next, previous, seekbar and also includes OnCompletionListener. All works excellent. But now I want that all should be managed by service.

I have created MyService Class :

public class MusicPlayerService extends Service {

    MediaPlayer mp;
    String fullStreamUrl;

    @Override
    public IBinder onBind(Intent arg0) {

        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mp = new MediaPlayer();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            fullStreamUrl = intent.getStringExtra("fullStreamUrl");
            // Log.d("url", "Geturl:-  " + fullStreamUrl);
        }

        try {
            mp.reset();
            mp.setDataSource(fullStreamUrl);
            mp.prepareAsync();
            mp.start();
            mp.setLooping(true);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block 
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void onDestroy() {
        mp.release();
        super.onDestroy();
    }
}

How to manage all functionality using service and currentsongIndex. Thanks. Please clear my doubt. I am so confused.

Upvotes: 1

Views: 650

Answers (3)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

To manage MediaPlayer from Activity which is running in background using service. you should create some commands which will broadcast when next, previous , and seekbar action occur in Activity.

If Service is running in same process then use LocalBroadcastManager otherwise BroadcastReceiver for getting events from Activity.

For example:

1. Create Actions for next,previous,... operations in Service:

  public static String STR_NEXT="MusicPlayerService.NEXT";
  public static String STR_PREV="MusicPlayerService.PREV";

2. Register receiver with all Action in onCreate :

@Override
public void onCreate() {
  super.onCreate();
  mp = new MediaPlayer();
  IntentFilter filter = new IntentFilter();
  filter.addAction(STR_NEXT);
  filter.addAction(STR_PREV);   
  LocalBroadcastManager.getInstance(this).
           registerReceiver(mediaAction,filter);
}

private BroadcastReceiver mediaAction = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if(intent.getAction().equals(STR_NEXT)){
              forwardClick();
            }
           // do same for other actions
        }
};

3. Create a method in Service for next operation:

public void forwardClick(){
  int currentPosition = mp.getCurrentPosition();
  if(currentPosition + seekForwardTime <= mp.getDuration()){
   mp.seekTo(currentPosition + seekForwardTime);
  }else{ 
   mp.seekTo(mp.getDuration());
  }
}

create other methods in same way.

4. From Activity on Button click send broadcast with required Action:

btnForward.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
        Intent intent = new Intent(MusicPlayerService.STR_NEXT);
        LocalBroadcastManager.getInstance(
                       view.getContext()).sendBroadcast(intent);
    }
 });

send broadcast in same way for other actions.

Upvotes: 2

Leye Odumuyiwa
Leye Odumuyiwa

Reputation: 674

Try going through your logcat messages or paste them here for us to be able to trace the error.

Upvotes: 0

Nivedh
Nivedh

Reputation: 961

On onDestroy() try

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    mp.stop();
}

Upvotes: 0

Related Questions