淡入淡出成功返回接口类
public interface MusicDoneCallBack {
void onComplete();
}
淡入淡出工具类
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.media.MediaPlayer;
import android.view.animation.LinearInterpolator;
public class FadeIn {
public static void volumeGradient(final MediaPlayer mediaPlayer,
final float from, final float to,
MusicDoneCallBack doneCallBack) {
ValueAnimator animator = ValueAnimator.ofFloat(from, to);
animator.setDuration(6000);
animator.setInterpolator(new LinearInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator it) {
float volume = (float) it.getAnimatedValue();
try {
mediaPlayer.setVolume(volume, volume);
} catch (Exception e) {
it.cancel();
}
}
});
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
if (doneCallBack != null) {
doneCallBack.onComplete();
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animator.start();
}
}
调用示例
FadeIn.volumeGradient(mediaPlayer, 1f, 0f, new MusicDoneCallBack() {
@Override
public void onComplete() {
}
});
|