gpt4 book ai didi

java - 音频文件无法使用 mediaController 播放?

转载 作者:行者123 更新时间:2023-12-01 04:23:15 25 4
gpt4 key购买 nike

尝试使用媒体 Controller 通过 url 播放音频文件

但是我收到错误:

09-11 10:28:23.970: E/MediaPlayer(912): Attempt to call getDuration without a valid mediaplayer 09-11 10:28:23.970: E/MediaPlayer(912): error (-38, 0) 09-11 10:28:23.990: E/MediaPlayer(912): Attempt to perform seekTo in wrong state: mPlayer=0x5d425340, mCurrentState=0

我在这里查看了各种答案,但没有解决。我尝试在 onPrepared()

中调用 getDuration()
mMediaPlayer = new MediaPlayer();
mMediaController = new MediaController(this);
mMediaController.setMediaPlayer(TextToSpeechActivity.this);
mMediaController.setAnchorView(findViewById(R.id.audioView));


try {
mMediaPlayer.setDataSource(getSoundUrl());

mMediaPlayer.prepare();
} catch (IOException e) {
Log.e("PlayAudioDemo", "Could not open file for playback.", e);
}

mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mHandler.post(new Runnable() {
public void run() {
//did not help
int duration = mMediaPlayer.getDuration();
mMediaController.show(10000);
mMediaPlayer.start();
}
});
}
});

getSoundUrl() 返回声音文件(wav)的 URL,它可以工作,因为如果我不实现媒体 Controller ,歌曲就会播放。代码很长,所以让我知道我需要添加哪一部分。

最佳答案

我自己解决了这个问题:

我添加了更多调试:

try {
mMediaPlayer.setDataSource(getSoundUrl());
mMediaPlayer.prepare();

} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

然后,我没有使用 setOnPreparedListener() 设置监听器,而是在 Activity 中实现了 OnPreparedListener,如下所示:

public class TextToSpeechActivity  
extends FragmentActivity implements MediaPlayerControl, OnPreparedListener {

然后onPrepared():

public void onPrepared(MediaPlayer mediaPlayer) {
Log.d("mylog", "onPrepared");
mMediaController.setMediaPlayer(this);
mMediaController.setAnchorView(findViewById(R.id.audioView));

mHandler.post(new Runnable() {
public void run() {

mMediaController.setEnabled(true);
mMediaController.show();
}
});
}

瞧!

关于java - 音频文件无法使用 mediaController 播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18745271/

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