我有两项活动:
我正在通过加载ArrayList
中名为SongsList
的所有歌曲网址并逐个播放来播放在线音频文件,或者用户可以从MediaPlayerPlayList_Acitivty
中选择(不同的显示所有歌曲的活动)并播放任何歌曲...... MediaPlayerPlayList_Acitivty
正在传递正确的歌曲索引,我通过对代码进行调试来确认。
问题:它只播放第一首歌曲songIndex(0),无论我从MediaPlayerPlayList_Acitivty
选择哪首歌曲播放第一首歌曲。
一旦我从MediaPlayerPlayList_Acitivty
中选择了一首新歌,我就会收到下面粘贴的错误,并且我已经粘贴了我用于此媒体播放器的核心代码。
我花了很多时间调试代码但没有用。
logcat的:
03-18 07:15:52.338: E/MediaPlayer(998): error (1, -1004)
03-18 07:15:52.338: W/System.err(998): java.io.IOException: Prepare failed.: status=0x1
03-18 07:15:52.338: W/System.err(998): at android.media.MediaPlayer.prepare(Native Method)
03-18 07:15:52.338: W/System.err(998): at net.website.player.MediaPlayer_Activity.playSong(MediaPlayer_Activity.java:341)
03-18 07:15:52.338: W/System.err(998): at net.website.player.MediaPlayer_Activity.onActivityResult(MediaPlayer_Activity.java:325)
03-18 07:15:52.338: W/System.err(998): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
03-18 07:15:52.338: W/System.err(998): at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
03-18 07:15:52.338: W/System.err(998): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
03-18 07:15:52.338: W/System.err(998): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
03-18 07:15:52.338: W/System.err(998): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
03-18 07:15:52.348: W/System.err(998): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 07:15:52.348: W/System.err(998): at android.os.Looper.loop(Looper.java:123)
03-18 07:15:52.348: W/System.err(998): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-18 07:15:52.348: W/System.err(998): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 07:15:52.348: W/System.err(998): at java.lang.reflect.Method.invoke(Method.java:507)
03-18 07:15:52.348: W/System.err(998): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-18 07:15:52.348: W/System.err(998): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-18 07:15:52.348: W/System.err(998): at dalvik.system.NativeStart.main(Native Method)
03-18 07:15:52.348: E/MediaPlayer(998): Attempt to call getDuration without a valid mediaplayer
03-18 07:15:52.348: E/MediaPlayer(998): error (-38, 0)
03-18 07:15:52.458: E/MediaPlayer(998): Error (-38,0)
03-18 07:15:52.458: D/onCompletion(998): 2
03-18 07:15:52.458: D/playSong(998): 5
03-18 07:15:53.058: E/MediaPlayer(998): error (1, -1004)
03-18 07:15:53.058: W/System.err(998): java.io.IOException: Prepare failed.: status=0x1
03-18 07:15:53.058: W/System.err(998): at android.media.MediaPlayer.prepare(Native Method)
03-18 07:15:53.058: W/System.err(998): at net.website.player.MediaPlayer_Activity.playSong(MediaPlayer_Activity.java:341)
03-18 07:15:53.058: W/System.err(998): at net.website.player.MediaPlayer_Activity.onCompletion(MediaPlayer_Activity.java:449)
03-18 07:15:53.058: W/System.err(998): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1344)
03-18 07:15:53.058: W/System.err(998): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 07:15:53.058: W/System.err(998): at android.os.Looper.loop(Looper.java:123)
03-18 07:15:53.058: W/System.err(998): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-18 07:15:53.058: W/System.err(998): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 07:15:53.058: W/System.err(998): at java.lang.reflect.Method.invoke(Method.java:507)
03-18 07:15:53.058: W/System.err(998): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-18 07:15:53.058: W/System.err(998): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-18 07:15:53.068: W/System.err(998): at dalvik.system.NativeStart.main(Native Method)
03-18 07:15:53.218: E/MediaPlayer(998): Attempt to call getDuration without a valid mediaplayer
03-18 07:15:53.218: E/MediaPlayer(998): error (-38, 0)
03-18 07:15:53.218: E/MediaPlayer(998): Error (-38,0)
03-18 07:15:53.218: D/onCompletion(998): 3
03-18 07:15:53.228: D/playSong(998): 5
03-18 07:15:53.828: E/MediaPlayer(998): error (1, -1004)
03-18 07:15:53.828: W/System.err(998): java.io.IOException: Prepare failed.: status=0x1
03-18 07:15:53.828: W/System.err(998): at android.media.MediaPlayer.prepare(Native Method)
03-18 07:15:53.828: W/System.err(998): at net.website.player.MediaPlayer_Activity.playSong(MediaPlayer_Activity.java:341)
03-18 07:15:53.828: W/System.err(998): at net.website.player.MediaPlayer_Activity.onCompletion(MediaPlayer_Activity.java:449)
03-18 07:15:53.828: W/System.err(998): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1344)
03-18 07:15:53.828: W/System.err(998): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 07:15:53.828: W/System.err(998): at android.os.Looper.loop(Looper.java:123)
03-18 07:15:53.828: W/System.err(998): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-18 07:15:53.828: W/System.err(998): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 07:15:53.828: W/System.err(998): at java.lang.reflect.Method.invoke(Method.java:507)
03-18 07:15:53.828: W/System.err(998): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-18 07:15:53.828: W/System.err(998): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-18 07:15:53.828: W/System.err(998): at dalvik.system.NativeStart.main(Native Method)
03-18 07:15:53.908: E/MediaPlayer(998): Attempt to call getDuration without a valid mediaplayer
03-18 07:15:53.908: E/MediaPlayer(998): error (-38, 0)
03-18 07:15:53.918: E/MediaPlayer(998): Error (-38,0)
代码:
public void onCreate(Bundle savedInstanceState) {
// Mediaplayer
mp = new MediaPlayer();
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
Log.d("onPrepared", "mp.start()");
mp.start();
}
});
}
public void onCompletion(MediaPlayer arg0) {
Log.d("onCompletion", ""+currentSongIndex);
// check for repeat is ON or OFF
if(isRepeat){
// repeat is on play same song again
playSong(currentSongIndex);
} else if(isShuffle){
// shuffle is on - play a random song
Random rand = new Random();
currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
playSong(currentSongIndex);
} else{
// no repeat or shuffle ON - play next song
if(currentSongIndex < (songsList.size() - 1)){
playSong(currentSongIndex + 1);
currentSongIndex = currentSongIndex + 1;
}else{
// play first song
playSong(0);
currentSongIndex = 0;
}
}
}
public void playSong(int songIndex){
// Play song
//mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
Log.d("playSong", ""+ songsList.size());
mp.reset();
mp.setDataSource(songsList.get(songIndex).get("audio_url"));
mp.prepare();
//mp.start(); //
// Displaying Song title
String songTitle = songsList.get(songIndex).get("title_a");
songTitleLabel.setText(songTitle);
// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);
// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
从堆栈跟踪看来,由于java.io.IOException
,准备调用失败了。数据源URL是否正确? songsList
列表可能没有正确填充?
答案 1 :(得分:0)
由于调用 mp.getDuration()
,您的应用似乎崩溃了。我的猜测是,当你的updateProgressBar()
方法在不合适的时间执行时会发生这种情况。
由于您在OnCompletionListener
方法中注册了playSong
,因此您应该拨打prepareAsync()
而不是prepare()
。
编辑仔细观察堆栈跟踪,问题似乎不是从updateProgressBar()
内部发生的,而是直接来自prepare()
内playSong()
的调用}。看看切换到prepareAsync()
是否会改变行为。