2014-09-25 283 views
1

我做了一個簡單的音樂播放器,可以在後臺播放一些歌曲。 進入主屏幕並通過通知重新打開應用程序也行。MusicPlayer崩潰點擊後退按鈕

我唯一的問題是,如果我在音樂播放器活動中按下後退按鈕(轉到父活動),我的應用程序崩潰。有兩個類,活動MP3Player.java和服務MP3Service.jave。

我收到以下錯誤:

java.lang.RuntimeException: Unable to destroy activity {package/package.MP3Player}: java.lang.IllegalArgumentException: Service not registered: [email protected]

你知道任何advide? 在此先感謝!

EDIT1: 我束縛我的球員像這樣在我的玩家活動: MP3Player.java(活動)

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

我用this教程,並修改它。

編輯3: 現在我得到這個錯誤:

java.lang.RuntimeException: Unable to stop service [email protected]: 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(); 
} 
+0

你必須先打電話stopService(),然後unbindService()。 – 2014-09-25 21:12:23

+0

好吧,我切換了兩個電話,但仍然是同樣的問題。它在我服務的onDestroy()中? – mbauer 2014-09-25 21:16:18

+0

清理onDestroy()和onUnbind()中的代碼。請發佈所有printStackTrace,我無法理解錯誤在哪裏。 – 2014-09-25 21:21:35

回答

1

Service not registered意味着它沒有義務在期間服務電話。

閱讀有關服務生命週期: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.

+0

我將我的播放器綁定到該服務。看到更新的問題。 – mbauer 2014-09-25 19:32:22

+0

你在哪裏調用了bindService()?請添加更多的代碼.... – 2014-09-25 19:41:59

+0

完成,但得到另一個錯誤 – mbauer 2014-09-25 20:50:37

相關問題