javaandroid-studioandroid-mediaplayer

mediaplayer not decreasing volume or stopping


I'm trying to create a method that crossfades between two mediaplayers. One should fade in while the other fades out, as I've done in both of the threads.

void startAudio(String path) throws IOException {
        ready = false;
        mp.setLooping(true);
        mp.setVolume(0.0f,0.0f);
        mpVolume = 0.0f;
        mp.setAudioAttributes(new AudioAttributes.Builder()
                .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
                .setLegacyStreamType(AudioManager.STREAM_MUSIC)
                .setUsage(AudioAttributes.USAGE_ALARM)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build());
        mp.setDataSource(this, Uri.parse(path));
        mp.prepare();
        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mp.start();
            }
        });
        Handler handler = new Handler();
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    while(!ready) {
                        sleep(50);
                        if(mpVolume < 1){
                            mpVolume+=0.01f;
                            if(mp2Volume > 0){
                                mp2Volume-=0.01f;
                            }
                            mp.setVolume(mpVolume, mpVolume);
                            mp2.setVolume(mp2Volume, mp2Volume);
                        }else{
                            mp2.stop();
                            currentMP = 2;
                            ready = true;
                        }
                        handler.post(this);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    }
    void startNewAudio(String path) throws IOException {
        ready = false;
        mp2.setLooping(true);
        mp2.setVolume(0.0f,0.0f);
        mp2Volume = 0.0f;
        mp2.setAudioAttributes(new AudioAttributes.Builder()
                .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
                .setLegacyStreamType(AudioManager.STREAM_MUSIC)
                .setUsage(AudioAttributes.USAGE_ALARM)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build());
        mp2.setDataSource(this, Uri.parse(path));
        mp2.prepare();
        mp2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mp2.start();
            }
        });
        Handler handler = new Handler();
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    while(!ready) {
                        sleep(50);
                        if(mp2Volume < 1){
                            mpVolume-=0.01f;
                            mp2Volume+=0.01f;
                            mp.setVolume(mpVolume, mpVolume);
                            mp2.setVolume(mp2Volume, mp2Volume);
                        }else{
                            mp.stop();
                            currentMP = 1;
                            ready = true;
                        }
                        handler.post(this);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    }

The players both fade in smoothly, but refuse to stop playing or fade back out again once the other is activated.

Any help is very much appreciated because this is actually driving me insane. Thanks!


Solution

  • I found the issue. It was that I was initializing the mediaplayers in the OnCreate method, which made the .stop and .setvolume methods apply to a version of them that didn't exist. I fixed it when I initialized the players in the same method that I was using them in.

    void startAudio(String path) throws IOException {
        ready = false;
        mp = new MediaPlayer();
        mp.setLooping(true);
        mp.setVolume(0.0f,0.0f);
        mpVolume = 0.0f;
        mp.setAudioAttributes(new AudioAttributes.Builder()
                .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
                .setLegacyStreamType(AudioManager.STREAM_MUSIC)
                .setUsage(AudioAttributes.USAGE_ALARM)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build());
        mp.setDataSource(this, Uri.parse(path));
        mp.prepare();
        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mp.start();
            }
        });
        Handler handler = new Handler();
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    while(!ready) {
                        sleep(50);
                        if(mpVolume < 1){
                            mpVolume+=0.005f;
                            if(mp2Volume > 0){
                                mp2Volume-=0.005f;
                            }
                            mp.setVolume(mpVolume, mpVolume);
                            if(mp2 != null){
                                mp2.setVolume(mp2Volume, mp2Volume);
                            }
                        }else{
                            if(mp2 != null){
                                mp2.release();
                            }
                            currentMP = 2;
                            ready = true;
                        }
                        handler.post(this);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    }
    void startNewAudio(String path) throws IOException {
        ready = false;
        mp2 = new MediaPlayer();
        mp2.setLooping(true);
        mp2.setVolume(0.0f,0.0f);
        mp2Volume = 0.0f;
        mp2.setAudioAttributes(new AudioAttributes.Builder()
                .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
                .setLegacyStreamType(AudioManager.STREAM_MUSIC)
                .setUsage(AudioAttributes.USAGE_ALARM)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build());
        mp2.setDataSource(this, Uri.parse(path));
        mp2.prepare();
        mp2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mp2.start();
            }
        });
        Handler handler = new Handler();
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    while(!ready) {
                        sleep(50);
                        if(mp2Volume < 1){
                            mpVolume-=0.005f;
                            mp2Volume+=0.005f;
                            mp.setVolume(mpVolume, mpVolume);
                            mp2.setVolume(mp2Volume, mp2Volume);
                        }else{
                            mp.release();
                            currentMP = 1;
                            ready = true;
                        }
                        handler.post(this);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    }