音乐服务在按下后自动停止

music service stops automatically on backpressed

我创建了音乐播放器服务,它会在按下后退时自动停止。 我想连续设置它直到歌曲可用或用户手动关闭通知 window。 任何帮助表示赞赏。

下面我放了一些我根据参考

创建的代码
private MediaPlayer player;
    private final IBinder musicBind = new MusicBinder();
    public void onCreate(){
        super.onCreate();
        player = new MediaPlayer();
        initMusicPlayer();
    }

    public void initMusicPlayer(){
        //set player properties
        player.setWakeMode(getApplicationContext(), 
                PowerManager.PARTIAL_WAKE_LOCK);
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        //set listeners
        player.setOnPreparedListener(this);
        player.setOnCompletionListener(this);
        player.setOnErrorListener(this);
    }

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

    //activity will bind to service
    @Override
    public IBinder onBind(Intent intent) {
        return musicBind;
    }

    //release resources when unbind
    @Override
    public boolean onUnbind(Intent intent){
        //player.stop();
        //player.release();
        return false;
    }
@Override
    public void onDestroy() {
        //stopForeground(true);
    }

下面是我如何从 onStart() 调用服务的代码

bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
            startService(playIntent);

我也试过传入清单。

<service android:name=".MusicService" android:enabled="true" android:process=":remote" />

我在 Activity.

中使用的方法
@Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    //start and bind the service when the activity starts
    @Override
    protected void onCreate() {
        super.onCreate();
        if (playIntent == null) {
            playIntent = new Intent(this, MusicService.class);
            playIntent.setAction(Constant.ACTION_PLAYER);
            bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
            startService(playIntent);
        }
    }
@Override
    protected void onStop() {
        super.onStop();
        playing = false;
        if (musicConnection != null) {
           // unbindService(musicConnection);
        }
    }

    @Override
    protected void onDestroy() {
        //stopService(playIntent);
        musicSrv = null;
        //unregisterReceiver(broadcastReceiver);
        super.onDestroy();
    }

试试这个……

服役中class

@Override
public boolean onUnbind(Intent intent){
    //player.stop();
    //player.release();
    return false;
}

@Override
public void onDestroy() {
    player.stop();
    player.release();
    stopForeground(true);
}

在你的Activity

不要调用 stopService(playIntent);

  @Override
   protected void onDestroy() {
    //stopService(playIntent);
    //musicSrv = null;
    //unregisterReceiver(broadcastReceiver);
    super.onDestroy();
  }

您必须将 MediaPlayer 的方法 release()stop() 移到 onUnbind() 之外。当您按下后退按钮时,客户端 (Activity) 调用它自己的 onDestroy() 方法并取消绑定 Service。如果只在服务停止时需要停止MediaPlayer,则必须将代码移动到ServiceonDestroy():

@Override
public boolean onUnbind(Intent intent){
    return false;
}

@Override
public void onDestroy() {
    player.stop();
    player.release();
    stopForeground(true);
}

如果您想避免 Service 在您按下后退时被杀死,您还必须从 ActivityonDestroy() 中删除方法 stopService() Activity 被销毁。所以:

@Override
protected void onDestroy() {
    musicSrv = null;
    //unregisterReceiver(broadcastReceiver);
    super.onDestroy();
}