vlcj: two videos same volume

30 views Asked by At

I am using the vlcj library: I want to play two files with a different volume, but when I change the volume of one, the other also changes, what can I do to avoid this?

import javax.swing.JFrame;

import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;

public class AudioIssue {
    
    public static void main(String[] args) {
        JFrame frame1 = new JFrame("Frame1");
        JFrame frame2 = new JFrame("Frame2");
        
        frame1.setBounds(100, 100, 600, 400);
        frame2.setBounds(100, 500, 600, 400);

        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        EmbeddedMediaPlayerComponent mediaPlayerComponent1 = new EmbeddedMediaPlayerComponent();
        EmbeddedMediaPlayerComponent mediaPlayerComponent2 = new EmbeddedMediaPlayerComponent();

        frame1.add(mediaPlayerComponent1);
        frame2.add(mediaPlayerComponent2);
        
        frame1.setVisible(true);
        frame2.setVisible(true);

        EmbeddedMediaPlayer mediaPlayer1 = mediaPlayerComponent1.mediaPlayer();
        EmbeddedMediaPlayer mediaPlayer2 = mediaPlayerComponent2.mediaPlayer();

        mediaPlayer1.audio().setVolume(70);
        mediaPlayer2.audio().setVolume(30);

        mediaPlayer1.media().play("archivos/vlcjReproduccion/test.mp4");
        mediaPlayer2.media().play("archivos/vlcjReproduccion/test.mp4");
        
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(mediaPlayer1.audio().volume());
        System.out.println(mediaPlayer2.audio().volume());
    }
}

Output:

  • 30
  • 30

If I execute two separate codes the volume is independent and it works, but if I execute it from the same code it doesn't work. I've tried Threads but it doesn't work either.

1

There are 1 answers

0
caprica On

This behaviour depends on a number of factors.

First, the audio output VLC is using may or may not support independent audio settings.

You can use vlc -H on the command line to list the available audio outputs, this is what is reported for me:

  -A, --aout {any,pulse,alsa,sndio,adummy,amem,afile,none} 
                                 Audio output module

So if the default audio output doesn't support what you need, you can try the others, although obviously some of these outputs like amem, afile, adummy will be of no use here.

To try this with vlcj, specify something like ["--aout", "pulse"] when you create the MediaPlayerFactory.

There is no guarantee this will work, but it's something I would try.

Second, depending on the version of VLC, you can not change volume or accurately query volume before the media playback has started. The best you could do here is to listen for a "media player ready" event to be fired and then do what you need with audio settings. This is not ideal, because you probably won't get this event until after the first few milliseconds of media has already played.

So make sure you try the latest version of VLC you can.

Third, in my experience this situation has improved more generally with VLC 4.x (which is still in development). So you could try a nightly pre-release build of VLC 4.x and the latest snapshot version of vlcj 5.x, but again there's no guarantee this will work.

If none of this is appropriate for you, then really the only other thing you can do is structure your application such that it runs each native media player in its own separate process. This however is not a trivial task, and requires you to create a new JVM for each media player and have some sort of remote procedure call mechanism to control your media players from your main application. This does work but is a serious undertaking.