Android Music service in background

455 views Asked by At

I have a problem here, I have a music track works in the background of an app that is never stop and when I add OnDestroy() or Pause() in my main activity class gives me an error when running the application not in the class and I don't know where to put them to control the music in the main !!

here are the codes that gives me an error:

mServ.pauseMusic();  
mServ.resumeMusic();
mServ.stopMusic();

The Main Activity:

private boolean mIsBound = false;
private MusicService mServ;

private ServiceConnection Scon = new ServiceConnection(){
    public void onServiceConnected(ComponentName name, IBinder  binder) {
            mServ = ((MusicService.ServiceBinder)binder).getService();
            }

    public void onServiceDisconnected(ComponentName name) {
        mServ = null;
    }

    };
    void doBindService(){
        bindService(new Intent(this,MusicService.class),
                Scon,Context.BIND_AUTO_CREATE);
        mIsBound = true;
    }

    void doUnbindService()
    {
        if(mIsBound)
        {
            unbindService(Scon);
            mIsBound = false;
        }
    }

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent music = new Intent();
    music.setClass(this,MusicService.class);
    startService(music);

And The Service Activity:

public class MusicService extends Service implements MediaPlayer.OnErrorListener {

private final IBinder mBinder = new ServiceBinder();
MediaPlayer mPlayer;
private int length = 0;

public MusicService(){}

public class ServiceBinder extends Binder {
    public MusicService getService()
 {
    return MusicService.this;
 }

}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return mBinder;
}

public void onCreate(){
    super.onCreate();

    mPlayer = MediaPlayer.create(this, R.raw.la);
    mPlayer.setOnErrorListener(this);

    if(mPlayer != null){

        mPlayer.setLooping(true);
        mPlayer.setVolume(100, 100);
    }

    mPlayer.setOnErrorListener(new OnErrorListener(){

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {

            onError(mPlayer, what, extra);
            return true;
        }

    });
}

public int onStartCommand (Intent intent , int flags, int startId)
{
     mPlayer.start();
     return START_STICKY;
}

public void pauseMusic()
{
    if(mPlayer.isPlaying())
    {
        mPlayer.pause();
        length=mPlayer.getCurrentPosition();
        Toast.makeText(this, "Music is Paused", Toast.LENGTH_LONG).show();

    }
}

public void resumeMusic()
{
    if(mPlayer.isPlaying()==false)
    {
        mPlayer.seekTo(length);
        Toast.makeText(this, "Music is started", Toast.LENGTH_LONG).show();
        mPlayer.start();
    }
}

public void stopMusic()
{
    mPlayer.stop();
    Toast.makeText(this, "Music is stoped", Toast.LENGTH_LONG).show();
    mPlayer.release();
    mPlayer = null;
}

@Override
public void onDestroy ()
{
    super.onDestroy();
    if(mPlayer != null)
    {
    try{
     mPlayer.stop();
     mPlayer.release();
        }finally {
            mPlayer = null;
        }
    }
}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    // TODO Auto-generated method stub

    Toast.makeText(this, "Music player failed", Toast.LENGTH_SHORT).show();
    if(mPlayer != null)
    {
        try{
            mPlayer.stop();
            mPlayer.release();
        }finally {
            mPlayer = null;
        }



    }

    return false;

}

}

The code is based on this link tutorial

0

There are 0 answers