gpt4 book ai didi

android - 单击后退按钮时音乐播放器崩溃

转载 作者:行者123 更新时间:2023-11-30 02:39:01 25 4
gpt4 key购买 nike

我制作了一个简单的音乐播放器,可以在后台播放一些歌曲。转到主屏幕并通过通知重新打开应用程序也可以。

我遇到的唯一问题是,如果我在音乐播放器 Activity 中按下后退按钮(转到父 Activity ),我的应用程序就会崩溃。有两个类, Activity MP3Player.java 和服务 MP3Service.jave。

我收到以下错误:

java.lang.RuntimeException: Unable to destroy activity {package/package.MP3Player}: java.lang.IllegalArgumentException: Service not registered: package.MP3Player$1@b135b300

你知道什么建议吗?提前致谢!

编辑 1:我在我的播放器 Activity 中像这样绑定(bind)我的播放器:MP3Player.java( Activity )

playIntent = new Intent(this, MP3Service.class);
bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
startService(playIntent);

我用了this教程并对其进行了修改。

编辑 3:现在我得到这个错误:

java.lang.RuntimeException: Unable to stop service package.MP3Service@b13a6f80: java.lang.IllegalStateException

MP3Service.java

public void onCreate() {
super.onCreate();
songPosn = 0;
player = new MediaPlayer();
initMusicPlayer();
}

public void initMusicPlayer() {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
}

...

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}

public void setList(ArrayList<Song> theSongs) {
songs = theSongs;
}

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

@Override
public IBinder onBind(Intent intent) {
return musicBind;
}

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

public void playSong() {
player.reset();

Song playSong = songs.get(songPosn);

try {
player.setDataSource(getApplicationContext(),
Uri.parse(playSong.getPath()));
} catch (Exception e) {
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
player.prepareAsync();
}

public void pauseMusic() {
if (player.isPlaying()) {
player.pause();
length = player.getCurrentPosition();
}
}

public void resumeMusic() {
if (player.isPlaying() == false) {
player.seekTo(this.length);
player.start();
} else {
Toast.makeText(getApplicationContext(),
"Please select a song to play", Toast.LENGTH_LONG).show();
}
}

public void stopMusic() {
player.stop();
player.release();
player = null;
}

// set the song
public void setSong(int songIndex) {
songPosn = songIndex;

}

@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}

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

MP3Player.java

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mp3_player);
getActionBar().setDisplayHomeAsUpEnabled(true);

songList = getSongList();

listAdapter = new PlayListAdapter(this, songList);
listMusic.setAdapter(listAdapter);

listMusic.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long arg3) {
currentSongPos = pos;
musicSrv.setSong(currentSongPos);
musicSrv.playSong();
}
});

}

private ServiceConnection musicConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MusicBinder binder = (MusicBinder) service;
musicSrv = binder.getService();
musicSrv.setList(songList);
musicBound = true;
}

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

@Override
protected void onStart() {
super.onStart();
if (playIntent == null) {
playIntent = new Intent(this, MP3Service.class);
bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
startService(playIntent);
}
}


...

protected void onDestroy() {
if (musicBound) {
stopService(playIntent);
unbindService(musicConnection);
musicSrv = null;
}
super.onDestroy();
}

最佳答案

Service not registered 表示它在 unbindService() 调用期间未绑定(bind)服务。

阅读有关服务生命周期的信息:API Guide: Services

编辑:

尝试添加:

protected void onDestroy() {
if(musicBound){
stopService(playIntent);
unbindService(musicConnection);
musicSrv=null;
}
super.onDestroy();
}

编辑 2:

对不起我的错,你需要先调用stopService()然后调用unbindService()

stopService() 的 Android 文档指出:

Note that if a stopped service still has ServiceConnection objects bound to it with the BIND_AUTO_CREATE set, it will not be destroyed until all of these bindings are removed. See the Service documentation for more details on a service's lifecycle.

关于android - 单击后退按钮时音乐播放器崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26046335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com