嘿伙计们我正在为Android制作一个音乐播放器,当我选择一首歌来播放应用程序崩溃并产生错误时:
09-16 13:10:08.671: I/System.out(22932): Reset worked
09-16 13:10:08.671: I/System.out(22932): com.example.taptwisttunes.Song@42d97240
09-16 13:10:08.671: I/System.out(22932): about to load song
09-16 13:10:08.671: I/System.out(22932): content://media/external/audio/media/23657
09-16 13:10:08.671: I/System.out(22932): content://media/external/audio/media/23657
09-16 13:10:08.671: D/AndroidRuntime(22932): Shutting down VM
09-16 13:10:08.671: W/dalvikvm(22932): threadid=1: thread exiting with uncaught exception (group=0x41fee700)
09-16 13:10:08.676: E/AndroidRuntime(22932): FATAL EXCEPTION: main
09-16 13:10:08.676: E/AndroidRuntime(22932): java.lang.IllegalStateException: Could not execute method of the activity
这是我的playSong方法,任何人都知道我做错了什么:
public void playSong(){ //this method plays the song by retrieving ID and modeling it as uri
//player.reset(); //resets media player
System.out.println("Reset worked");
Song playSong = songs.get(sPos); // retrieves song to be played
System.out.println(playSong);
long currSong = playSong.getId();
System.out.println("about to load song");
Uri trackUri = ContentUris.withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, currSong);
System.out.println(trackUri);
System.out.println(trackUri.toString());
//try block not sure if uri will work as a data source
try{
player.setDataSource(this.getApplicationContext(), trackUri);
}
catch(Exception e){
Log.e("Music Service", "There has been an error setting the data source", e);
}
player.prepareAsync();
}
答案 0 :(得分:0)
MediaPlayer#prepareAsync
抛出IllegalStateException
,您需要处理该异常或向方法声明添加throws,因此该方法的调用者将处理该异常。是你做的吗?
类似的东西:
try{
player.prepareAsync();
}
catch(IllegalStateException ise){
//do what you want when this exception is thrown
}
答案 1 :(得分:0)
有了错误和代码,我相信你之前已经调用了player
实例播放了一首歌,而你正试图第二次播放这首歌。
此外,您已注释掉player.reset().
确保您的player
实例重置(),准备好了()并设置为正确播放文件。
following diagram from the android developers应该可以帮助您更好地理解这个概念。