Android:如何检测Android Studio中正在播放所选音频

时间:2018-03-20 04:47:22

标签: android audio android-mediaplayer android-service-binding

我正在努力创建一个音频播放器应用程序,所有这些都通过利用活动和服务顺利运行。但是,当您选择音频并播放音频时会有一个限制。 目前正在播放音频时,在选择相同的音频时,我会从头开始。

问题: 如何检测正在播放所选音频。如果所选音频与正在播放的音频不同,它将从头开始播放,但如果它是相同的则不会做任何事情。

在我的活动中:

//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);
        }
    }

在服务中 使用onBind获取变量:

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

我在onStartCommand中使用播放所选的音频。

@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;
    }

2 个答案:

答案 0 :(得分:0)

使用另一个变量来保留url方法中新传递的onBind()。然后检查新网址是否与旧网址不同。

// 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;
}

答案 1 :(得分:0)

您正在使用绑定服务,因此您可以在服务中创建一个方法,如

private String currentUrl;

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

当连接服务时,您可以使用此公共方法。

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