Search code examples
androidaudiomedia-playertimedelta

Android Studio Mediaplayer how to fade in and out


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.

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?


Solution

  • 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.

    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;
    
    }
    

    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.