音乐服务会在后退

时间:2017-05-01 12:52:39

标签: android service android-music-player background-music

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

下面我放了一些我从参考文献中创建的代码

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" />

我在“我的活动”中使用的方法。

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

2 个答案:

答案 0 :(得分:0)

试试这一个......

在服务类

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

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

在你的活动中

不要调用stopService(playIntent);

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

答案 1 :(得分:0)

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

onDestroy()

如果您想要避免在按下时Service将被杀死,您还必须从@Override public boolean onUnbind(Intent intent){ return false; } @Override public void onDestroy() { player.stop(); player.release(); stopForeground(true); } 的{​​{1}}中移除方法stopService() onDestroy()被摧毁。所以:

Activity