Rumah > Soal Jawab > teks badan
Apabila saya memainkan fail berulang kali, pelbagai ralat berlaku, termasuk isPlaying stop dan ralat yang berbeza Di bawah ialah kod saya. Terima kasih!
public void playerVoice(String json) {
if (TextUtils.isEmpty(json))
return;
JSONObject jsonObject = JSON.parseObject(json);
if (player == null) {
player = new MediaPlayer();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.reset();
}
});
}
try {
if (player.isPlaying()) {
player.stop();
player.reset();
}
} catch (Exception e) {
player.stop();
player.reset();
}
try {
player.setDataSource(jsonObject.getString("fullPath"));
player.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}