Android的:如何创建淡入/淡出音效任何音乐文件,我的应用程序的戏剧?我的、音效、应用程序、音乐文件

2023-09-12 23:38:30 作者:他唇毁她纯

这是我的工作就播放音乐文件的应用程序。如果定时器超时我想要的音乐淡出。我怎么做。我使用的MediaPlayer播放音乐和音乐文件present在我的应用程序的原始文件夹。

The application that I am working on plays music files. If a timer expires I want the music to fade out. How do I do that. I am using MediaPlayer to play music and music files are present in raw folder of my application.

推荐答案

做到这一点的方法之一是使用MediaPlayer.setVolume(左,右),并让这些价值递减后,每iteration..here是一个粗略的想法

One way to do it is to use MediaPlayer.setVolume(right, left) and have these values decrement after every iteration..here is a rough idea

float volume = 1;
float speed = 0.05f;

public void FadeOut(float deltaTime)
{
    MediaPlayer.setVolume(volume, volume);
    volume -= speed* deltaTime

}
public void FadeIn(float deltaTime)
{
    MediaPlayer.setVolume(volume, volume);
    volume += speed* deltaTime

}

的淡入或淡出应称为一旦你的这个定时器已经期满,则该方法犯规需要采取deltaTime但其良好的做法,因为它会降低音量在所有设备相同的速率

The FadeIn or FadeOut should be called once this timer of yours has expired, the method doesnt need to take the deltaTime but its good practise as it will lower the volume at the same rate across all devices