我想要一首歌即将结束并开始播放时的交叉渐入渐出效果。我在下面的代码中使用了音频之间的crossFading渐进渐进法,它在大多数设备上都能正常工作,但在某些运行在Oreo和一台Oreo上的三星设备上却无法正常工作加6.当第二个媒体播放器开始播放下一首歌曲时,它们的停顿很小。预先感谢。
private void crossFade() {
fadeOut(musicPlayer, CROSSFADE_DURATION);
fadeIn(musicPlayer2, CROSSFADE_DURATION);
}
public void fadeOut(final MediaPlayer _player, final int duration) {
final float deviceVolume = getDeviceVolume();
final Handler h = new Handler();
h.postDelayed(new Runnable() {
private float time = duration;
private float volume = 0.0f;
@Override
public void run() {
// can call h again after work!
time -= 100;
volume = (deviceVolume * time) / duration;
_player.setVolume(volume, volume);
if (time > 0)
h.postDelayed(this, 100);
else {
_player.stop();
_player.release();
}
}
}, 100); // delay (takes millis)
}
public void fadeIn(final MediaPlayer _player, final int duration) {
final float deviceVolume = getDeviceVolume();
final Handler h = new Handler();
h.postDelayed(new Runnable() {
private float time = 0.0f;
private float volume = 0.0f;
@Override
public void run() {
if (!_player.isPlaying())
_player.start();
// can call h again after work!
time += 100;
volume = (deviceVolume * time) / duration;
_player.setVolume(volume, volume);
if (time < duration)
h.postDelayed(this, 100);
}
}, 100); // delay (takes millis)
}
答案 0 :(得分:0)
最后,我自己解决了这个问题。实际上,MediaPlayer存在此问题,很久以前就已向Google报告了该问题,但仍未解决(https://issuetracker.google.com/issues/36931073),因此我们对此无能为力。因此,我使用Exoplayer播放音频,并且运行非常流畅,没有任何暂停。