我们正在使用Android中的MediaPlayer在无限循环中播放小型音频剪辑,但是在声音的循环之间有很小的间隔(200毫秒),这非常令人讨厌,因为它具有声音的断续性。
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();
}
循环之间暂停
答案 0 :(得分: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);
不是使用媒体播放器,而是使用SoundPool来播放连续的声音文件。