0

I'm trying to make an audio player app, all running smoothly by utilizing activity and services. But there is a constraint when you have selected an audio and play it. Currently when the audio is playing, while selecting the same audio, I make it start from the beginning.

The question: How to detect that the selected audio is being played. If the selected audio is different from what is being played it will be played from scratch but if it is the same it will not do anything.

In my activity :

//connect to the service
  private ServiceConnection serviceConnection = new ServiceConnection(){

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Services.MediaBinder binder = (Services.MediaBinder) service;
            //get service
            services = binder.getService();
            mediaBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mediaBound = false;
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        if(playIntent==null){
            playIntent = new Intent(this, Services.class);
            playIntent.putExtra("url", String.valueOf(urlRaw));
            bindService(playIntent, serviceConnection, Context.BIND_AUTO_CREATE);
            startService(playIntent);
        }
    }

In services : Get variable using onBind:

@Override
public IBinder onBind(Intent intent) {
    urlMedia = intent.getStringExtra("url");
    return mediaBinder; // private final IBinder mediaBinder
}

I use in onStartCommand to play the selected audio.

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            //An audio file is passed to the service through putExtra();

            if(isPng()){
                mPlayer.release();
            }

            mPlayer = MediaPlayer.create(getApplicationContext(), getResources().getIdentifier(String.valueOf(urlMedia),"raw",getPackageName()));
            mPlayer.start();
        } catch (NullPointerException e) {
            stopSelf();
        }

        if (mPlayer != null)
            initMusicPlayer();

        return START_STICKY;
    }
Ugy Astro
  • 357
  • 3
  • 6
  • 16

2 Answers2

0

Use an another variable to keep the newly passed url in the onBind() method. Then check whether the new url is different from the old one.

// On Bind
@Override
public IBinder onBind(Intent intent) {
    newUrlMedia = intent.getStringExtra("url");
    return mediaBinder; 
}

// On Start Command
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        if (newUrlMedia.equals(urlMedia)) {
            // Same url is passes
            return START_STICKY;
        } else {
            // Different url 
            urlMedia = newUrlMedia;
            if(isPng()){
                mPlayer.release();
            }

            mPlayer = MediaPlayer.create(getApplicationContext(), getResources().getIdentifier(String.valueOf(urlMedia),"raw",getPackageName()));
            mPlayer.start();
        }
    } catch (NullPointerException e) {
        stopSelf();
    }

    if (mPlayer != null)
        initMusicPlayer();

    return START_STICKY;
}
UdeshUK
  • 972
  • 1
  • 11
  • 19
0

You are using bound service so you can create a method in service like

private String currentUrl;

public void playAudio(Sting url){
  if(currentUrl.equals(url)) return;
  this.currentUrl = url;
  //Do other stuff.
}

And when service connected you can use this public method.

@Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Services.MediaBinder binder = (Services.MediaBinder) service;
            //get service
            services = binder.getService();
            services.playAudio("url");
            mediaBound = true;
        }
toffor
  • 1,219
  • 1
  • 12
  • 21