这是我的第一个问题。它在media.release()行中给出了NullPointerException。确切的输出是NullPointerException(@ ProgressDetailRecyclerViewAdapter $ 5:onCompletion:308){main}
public void play(String fileURL) {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(fileURL);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.release(); //line 308
mediaPlayer = null;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
试试这个
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release(); //line 308
mp= null;
}
});
答案 1 :(得分:0)
传递给onCompletion
方法的变量名为mp
,而不是mediaPlayer
。您的mediaPlayer
变量可能已被初始化,从而导致错误。将代码更改为:
@Override
public void onCompletion(MediaPlayer mp) {
mp.release(); //line 308
mp = null;
}
答案 2 :(得分:0)
@Override
public void onCompletion(MediaPlayer **mp**) {
**mediaPlayer**.release(); //line 308
mediaPlayer = null;
}
在此方法中使用正确的对象mp而不是mediaPlayer