问题
We are playing the small audio clip in infinity loop using MediaPlayer in android but there is very small around (200 ms) pause between a loop in the sound which is very annoying because of its break continuity of sound.
MediaPlayer mp = new MediaPlayer();
try {
AssetFileDescriptor descriptor = getAssets().openFd(file);
mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
mp.prepare();
mp.setLooping(true);
mp.start();
} catch (IOException e) {
e.printStackTrace();
}
Pause between loop
回答1:
SoundPool soundPool;
int soundID;
boolean plays = false, loaded = false;
float actVolume, maxVolume, volume;
AudioManager audioManager;
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
actVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
volume = actVolume / maxVolume;
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
}
});
soundID = soundPool.load(this, R.raw.audiofile, 1);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
soundPool.play(soundID, volume, volume, 1, -1, 1f);
}
},1000);
Instead of media player use SoundPool to play continue sound file.
来源:https://stackoverflow.com/questions/57122329/while-playing-sound-using-media-player-in-loop-there-is-some-pause-in-the-loop-w