gpt4 book ai didi

android - 尝试在 android 媒体播放器中没有有效媒体播放器的情况下调用 getDuration

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:32 25 4
gpt4 key购买 nike

我正在使用搜索栏播放 SD 卡中的歌曲。在获取歌曲的持续时间时,我得到了Attempt to call getDuration without a valid mediaplayer and illegal state exception。文件名和路径是有效的。我试了很长时间都找不到解决方案。以及如何在播放歌曲时移动搜索栏。请帮助我。我的代码:

Button play,pause,stop;
SeekBar seek;
MediaPlayer mediaPlayer;
SurfaceView sv;
boolean isPlaying = false;

play.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
playsong(filename);
}
});


private void playsong(String filename2) {
try{
Log.e("filename2",filename2);
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filename2);
seek.setMax(mediaPlayer.getDuration());
mediaPlayer.prepare();
mediaPlayer.start();
myHandler.post(runn);
isPlaying = true;
mediaPlayer.setOnCompletionListener(this);
}
catch(Exception ex){
Log.e("sdcard-err2:",""+ex);
}

}

private Handler myHandler = new Handler();
final Runnable runn = new Runnable()
{
public void run()
{
if (mediaPlayer != null)
{
if (isPlaying )
{
try
{
int currentPosition = mediaPlayer.getCurrentPosition();

seek.setProgress(currentPosition);

} catch (Exception e) {
e.printStackTrace();
}

sv.postDelayed(runn, 150);
}
}
}

};

我的日志猫错误:

05-17 09:46:43.578: ERROR/filename2(1670): /sdcard/The Dance Theme.mp3
05-17 09:46:43.594: ERROR/MediaPlayer(1670): Attempt to call getDuration without a valid mediaplayer
05-17 09:46:43.594: ERROR/MediaPlayer(1670): error (-38, 0)
05-17 09:46:43.594: ERROR/MediaPlayer(1670): prepareAsync called in state 0
05-17 09:46:43.594: ERROR/sdcard-err2:(1670): java.lang.IllegalStateException
05-17 09:46:43.594: ERROR/MediaPlayer(1670): Error (-38,0)

最佳答案

您可能在文件完全加载之前调用了 getDuration。看能不能解决this question适合你。

关于android - 尝试在 android 媒体播放器中没有有效媒体播放器的情况下调用 getDuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6026288/

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