gpt4 book ai didi

android-mediaplayer - Mediaplayer 最终确定但未发布 (JNI)

转载 作者:行者123 更新时间:2023-12-02 01:54:10 27 4
gpt4 key购买 nike

我在 logcat 中收到一条警告消息

W/MediaPlayer-JNI(16795): MediaPlayer finalized without being released

请告诉我我错在哪里以及如何解决问题。我想在第二次调用 playAudio 时停止之前的 mMediaPlayer 音频播放。

public class WordAudio  {

MediaPlayer mMediaPlayer;
String url;
private static WordAudio wordAudio;
private WordAudio() {
}

public static WordAudio getInstance() {
if (wordAudio == null) {
wordAudio = new WordAudio();
}
return wordAudio;

}

public void playAudio(final Context context, String id) {

try {

if (mMediaPlayer != null) {

if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
}
}

mMediaPlayer = new MediaPlayer();
url = AUDIO_STREEM_URL+"id=" + id+ "&x=mp3";
mMediaPlayer = MediaPlayer.create(context, Uri.parse(url));
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.start();
}
});
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mMediaPlayer) {
//mMediaPlayer.release();
}
});


mMediaPlayer.setOnInfoListener(new OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mMediaPlayer, int what, int extra) {
if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
} else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
mMediaPlayer.start();
}
return false;
}
});
}
catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}}

谢谢

最佳答案

这样做

mMediaPlayer = new MediaPlayer();
url = AUDIO_STREEM_URL;
mMediaPlayer = MediaPlayer.create(context, Uri.parse(url));
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.start();
}
});
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mMediaPlayer) {
mMediaPlayer.release();
}
});

关于android-mediaplayer - Mediaplayer 最终确定但未发布 (JNI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21129586/

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