Android Studio Mediaplayer如何淡入和淡出 [英] Android Studio Mediaplayer how to fade in and out
问题描述
我正在使用android studio中的mediaplayer类.我只是想淡出一种声音而淡入另一种声音,而不是使用setVolume(0,0)和setVolume(1,1).
I am working with the mediaplayer class in android studio. I simply want to fade out one sound and fade in the other sound instead of using setVolume(0,0) and setVolume(1,1).
我为此创建了两个媒体播放器,似乎在该线程中找到了一个解决方案:
I have created two mediaplayers for this and it seemed like I found a solution in this thread: Android: How to create fade-in/fade-out sound effects for any music file that my app plays? but I don't know how to use deltaTime.
还有一些其他解决方案,我几乎无法理解.没有一种简单的方法可以淡入淡出两个媒体播放器,我无法想象还没有人需要它,或者每个人都使用强迫性代码来实现它.以及我应该如何使用deltaTime?
There are also some other solutions to this, which I can barely understand. Isn't there an easy way to cross fade two mediaplayers, I can not imagine no one has needed this yet or everyone uses obsessive code to achieve it. And how should I use deltaTime?
推荐答案
看看链接的示例,您将拥有循环调用fadeIn()/fadeOut(),以在一段时间内增大/减小音量. deltaTime 是循环的每次迭代之间的时间.
Looking at the linked example, you would have to call fadeIn()/fadeOut() in a loop, to increase/decrease the volume over a period of time. deltaTime would be the time between each iteration of the loop.
您必须在与主UI线程不同的线程中执行此操作,因此您不会阻止它并导致应用程序崩溃.您可以通过将此循环放入新的Thread/Runnable/Timer中来实现.
You'd have to do this in a separate thread from your main UI thread, so you don't block it and cause your app to crash. You can do this by either putting this loop inside a new Thread/Runnable/Timer.
这是我的淡入淡出示例(您可以为淡入淡出做类似的事情):
Here is my example for fading in (you can do a similar thing for fading out):
float volume = 0;
private void startFadeIn(){
final int FADE_DURATION = 3000; //The duration of the fade
//The amount of time between volume changes. The smaller this is, the smoother the fade
final int FADE_INTERVAL = 250;
final int MAX_VOLUME = 1; //The volume will increase from 0 to 1
int numberOfSteps = FADE_DURATION/FADE_INTERVAL; //Calculate the number of fade steps
//Calculate by how much the volume changes each step
final float deltaVolume = MAX_VOLUME / (float)numberOfSteps;
//Create a new Timer and Timer task to run the fading outside the main UI thread
final Timer timer = new Timer(true);
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
fadeInStep(deltaVolume); //Do a fade step
//Cancel and Purge the Timer if the desired volume has been reached
if(volume>=1f){
timer.cancel();
timer.purge();
}
}
};
timer.schedule(timerTask,FADE_INTERVAL,FADE_INTERVAL);
}
private void fadeInStep(float deltaVolume){
mediaPlayer.setVolume(volume, volume);
volume += deltaVolume;
}
在您的情况下,我将不使用两个单独的MediaPlayer对象,而是在渐变之间交换轨道. 示例:
Instead of using two separate MediaPlayer objects, I would in your case use just one and swap the track between the fades. Example:
**Audio track #1 is playing but coming to the end**
startFadeOut();
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.setDataSource(context,audiofileUri);
mediaPlayer.prepare();
mediaPlayer.start();
startFadeIn();
**Audio track #2 has faded in and is now playing**
希望这可以解决您的问题.
Hope this solves your problem.
这篇关于Android Studio Mediaplayer如何淡入和淡出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!