我有一个Android应用,我想播放不同的声音。我的代码工作正常,但播放了一些声音后便停止播放更多声音。我的点击侦听器位于ListView中,因此会循环播放不同的声音。我认为问题是由于创建和停止造成的,但我不知道如何解决它?
任何人都可以提供我的代码帮助吗?
MediaPlayer mp;
holder.alphabetSound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mp != null && mp.isPlaying()) {
mp.stop();
}
mp = MediaPlayer.create(activity,sound[index]));
mp.start();
}
});
Logcat:
2019-10-26 20:44:28.737 1319-8893/? E/MP3Extractor: Unable to resync. Signalling end of stream.
2019-10-26 20:44:28.744 1315-1946/? E/OMXNodeInstance: setConfig(52300a9:google.mp3.decoder, ConfigPriority(0x6f800002)) ERROR: Undefined(0x80001001)
2019-10-26 20:44:28.751 1306-9450/? E/AudioFlinger: no more track names available
2019-10-26 20:44:28.751 1306-9450/? E/AudioFlinger: createTrack_l() initCheck failed -12; no control block?
2019-10-26 20:44:28.751 1320-10055/? E/AudioTrack: AudioFlinger could not create track, status: -12
2019-10-26 20:44:28.751 1320-10055/? E/AudioSink: Unable to create audio track
2019-10-26 20:44:28.752 1320-10052/? E/NuPlayer: received error(0xffffffed) from audio decoder, flushing(0), now shutting down
2019-10-26 20:44:28.752 9789-9801/ir.lariha.englishanimals E/MediaPlayer: error (1, -19)
2019-10-26 20:44:28.752 9789-9789/ir.lariha.englishanimals E/MediaPlayer: Error (1,-19)
private void startMP(final Context context){
if(mp == null){
mp = MediaPlayer.create( context, R.raw.ringtone);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
stopMP(context);
}
});
}
mp.start();
mp.setLooping(true);
}
private void stopMP(Context context) {
if(mp != null) {
mp.release();
mp = null;
Toast.makeText(context , "song is stopped " , Toast.LENGTH_SHORT).show();
}
}
您认为,问题出在创建和停止过程中。我认为,如果您这样做,将会有所帮助。