我正在处理原始文件夹中的所有声音。使用playSound()并在完成后释放声音以及错误释放声音。在使用playSound()播放几声后,我仍然会收到错误(-19,0)。
public static MediaPlayer mediaPlayer;
public static boolean playSound(final Context context, int resId) {
if(releaseSound()){
mediaPlayer = MediaPlayer.create(context, resId);
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d("test", "Media player error :" + what + " extra:" + extra);
if(what == 19)
FireToast
.showToast(context,
"MediaPlayer error (Out of buffer). Please restart your application.");
releaseSound();
mp.release();
mp = null;
return false;
}
});
if (resId == 0) {
FireToast.showToast(context, "File not found");
return false;
}
// mediaPlayer = MediaPlayer.create(context, resId);
try {
if (mediaPlayer != null)
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
releaseSound();
FireToast.showToast(context,"MediaPlayer song completed.");
}
});
mediaPlayer.start();
setPause(false);
return true;
} catch (IllegalStateException e) {
e.printStackTrace();
return false;
}
}
return false;
}
public static boolean releaseSound() {
try {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
}
mediaPlayer = null;
isPause = false;
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}