这是我播放和更新曲目的代码,按下按钮我调用update()
功能,但曲目没有正确更改。
第一首曲目播放得很好,但是当我尝试切换到下一首时,它表现不正确,我错了。
MediaPlayer mPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPlayer=new MediaPlayer();
mPlayer.setOnPreparedListener(this);
}
public void prepareMediaPlayer(FileDescriptor fd)
{
try {
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
Log.d("player", "setAudiotStreamType");
mPlayer.setDataSource(fd);
Log.d("player", "setDataSource");
mPlayer.prepareAsync();
Log.d("player", "prepareAsync");
}
catch (IOException e) {}
catch (IllegalArgumentException e) {}
catch (IllegalStateException e) {}
}
@Override
public void onPrepared(MediaPlayer arg0) {
}
public void updateTrack()
{
File f=(File) imageFilexxSm.values().toArray()[index];
FileInputStream inputStream = new FileInputStream(f);
prepareMediaPlayer(inputStream.getFD());
inputStream.close();
if(mPlayer.isPlaying())
{
mPlayer.stop();
mPlayer.reset();
mPlayer.reset();
}
else
{
mPlayer.start();
}
}
日志:
05-11 13:17:42.782: E/MediaPlayer(14969): start called in state 4
05-11 13:17:42.782: E/MediaPlayer(14969): error (-38, 0)
05-11 13:17:42.821: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.372MB for 4194320-byte allocation
05-11 13:17:43.040: I/System.out(14969): index value is 0/1024/0
05-11 13:17:43.087: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.457MB for 2261008-byte allocation
05-11 13:17:43.189: E/MediaPlayer(14969): attachNewPlayer called in state 8
05-11 13:17:43.220: I/Choreographer(14969): Skipped 35 frames! The application may be doing too much work on its main thread.
05-11 13:17:43.275: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.452MB for 4194320-byte allocation
05-11 13:17:43.392: I/MediaPlayer(14969): Don't send intent. msg.arg1 = 0, msg.arg2 = 0
05-11 13:17:43.392: E/MediaPlayer(14969): Error (-38,0)
05-11 13:17:57.642: I/System.out(14969): action down
05-11 13:17:57.642: I/System.out(14969): points 1.6014493?21
05-11 13:17:57.642: I/System.out(14969): show one page
05-11 13:17:57.642: I/System.out(14969): start curl right
05-11 13:17:57.650: I/System.out(14969): index value is 1/1024/1
05-11 13:17:57.681: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.401MB for 2261008-byte allocation
05-11 13:17:57.775: E/MediaPlayer(14969): attachNewPlayer called in state 128
05-11 13:17:57.821: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.401MB for 4194320-byte allocation
05-11 13:18:04.704: I/System.out(14969): action down
05-11 13:18:04.704: I/System.out(14969): points 1.692029?21
05-11 13:18:04.704: I/System.out(14969): show one page
05-11 13:18:04.704: I/System.out(14969): start curl right
05-11 13:18:04.704: I/System.out(14969): index value is 2/1024/2
05-11 13:18:04.736: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.315MB for 2261008-byte allocation
05-11 13:18:04.829: E/MediaPlayer(14969): attachNewPlayer called in state 128
05-11 13:18:04.876: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.316MB for 4194320-byte allocation
05-11 13:18:08.845: I/System.out(14969): action down
05-11 13:18:08.845: I/System.out(14969): points 1.5471015?21
05-11 13:18:08.845: I/System.out(14969): show one page
05-11 13:18:08.845: I/System.out(14969): start curl right
05-11 13:18:08.845: I/System.out(14969): index value is 3/1024/3
05-11 13:18:08.876: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.316MB for 2261008-byte allocation
05-11 13:18:08.970: E/MediaPlayer(14969): attachNewPlayer called in state 16
05-11 13:18:09.025: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.316MB for 4194320-byte allocation
05-11 13:18:12.892: I/System.out(14969): action down
05-11 13:18:12.892: I/System.out(14969): points 1.5688405?21
05-11 13:18:12.892: I/System.out(14969): show one page
05-11 13:18:12.892: I/System.out(14969): start curl right
05-11 13:18:12.892: I/System.out(14969): index value is 4/1024/4
05-11 13:18:12.923: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.316MB for 2261008-byte allocation
05-11 13:18:13.025: E/MediaPlayer(14969): start called in state 4
05-11 13:18:13.025: E/MediaPlayer(14969): error (-38, 0)
05-11 13:18:13.064: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.316MB for 4194320-byte allocation
05-11 13:18:13.126: E/MediaPlayer(14969): Error (-38,0)
05-11 13:18:13.126: I/MediaPlayer(14969): Don't send intent. msg.arg1 = 0, msg.arg2 = 0
05-11 13:18:18.228: I/System.out(14969): action down
05-11 13:18:18.236: I/System.out(14969): points -1.5289855?21
05-11 13:18:18.236: I/System.out(14969): show one page
05-11 13:18:18.236: I/System.out(14969): index value is 3/1024/3
05-11 13:18:18.259: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.316MB for 2261008-byte allocation
05-11 13:18:18.353: E/MediaPlayer(14969): attachNewPlayer called in state 8
05-11 13:18:18.400: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.316MB for 4194320-byte allocation
答案 0 :(得分:6)
有两件事情发生了:
您使用的是prepareAsync
,异步。也就是说,在执行任何依赖于准备工作的事情之前(例如调用onPrepared
),您应该等待start
回调。这就是您收到“start called in state 4
”错误消息的原因(状态4为MEDIA_PLAYER_PREPARING
)。
如果在正在播放期间调用updateTrack
,则在准备新歌之前,似乎不会停止MediaPlayer
。在调用stop
之后,您已拨打prepareMediaPlayer
,但这对您没有多大帮助。为了能够重新准备播放器,您必须首先stop
播放,如果您还想更改数据源,则必须在停止播放后reset
播放器。
有关有效状态转换的集合,请参阅MediaPlayer documentation's state diagram。