Search code examples
javalibvlcvlcj

How to control volume in vlcj?


I am creating a project for my college using vlcj. this is my code

public class MediaPlayerUI extends JFrame {
/**
 * 
 */
private static final long serialVersionUID = 1L;
public static EmbeddedMediaPlayer mediaPlayer;
public static FullScreenStrategy fullScreenStrategy;
public MediaPlayerUI(String title) {
    super(title);
    // finding native library of vlc player
    new NativeDiscovery().discover();

    //setting a layout
    setLayout(new BorderLayout());

    //setting up mediaplayer components
    String[] libvlcArgs = {};
    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(libvlcArgs);

    mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer(fullScreenStrategy);

    //creating swing Components
    Canvas canvas = new Canvas();

    //setting up the video surface
    CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas);
    mediaPlayer.setVideoSurface(videoSurface);

    //adding swing components to contant pane
    Container UIContainer = getContentPane();
    UIContainer.add(canvas,BorderLayout.CENTER);

}

}

and this

     package andromedia;

import javax.swing.JFrame;

import uk.co.caprica.vlcj.player.embedded.DefaultFullScreenStrategy;

public class MainPlayer {

public static void main(String args[])
{
    JFrame mainFrame = new MediaPlayerUI("AndroMedia");
    mainFrame.setVisible(true);
    mainFrame.setSize(500,500);
    mainFrame.setDefaultCloseOperation(3);
    MediaPlayerUI.fullScreenStrategy = new     DefaultFullScreenStrategy(mainFrame);  
    MediaPlayerUI.mediaPlayer.playMedia("E:\\Media\\flash.mp4","Volume=0");
    MediaPlayerUI.mediaPlayer.setVolume(0);
    System.out.println(MediaPlayerUI.mediaPlayer.getVolume());
}


}

I cant control the volume when I print the current volume I get the value "-1"

and am getting the following runtime errors

 -1
 [000000000d670bd0] main vout display error: Failed to set on top
 [000000000d670bd0] main vout display error: Failed to resize display

any help would be greatly appreciated

thank you .


Solution

  • Volume is a bit problematic with recent versions of VLC. In older versions of VLC there was not a problem.

    In 2.1, it is simply not possible to set/get the volume before media playback has actually begun. As you have described, you get a value of "-1".

    So you have to find some other way, like using a MediaPlayerEventListener and waiting for a media player "playing" event and even then you may need to wait for a little bit longer using e.g. by sleeping for 500ms or so before setting/getting the volume.

    Clearly that's a pretty bad solution, but unfortunately that's just the way it is.

    With VLC 2.2+, the situation is a little bit better. You can now set/get the volume before playback has actually begun, but if you stop the media player (pause is OK, but not stop) then you can no longer set/get the volume until you play media again.

    There is a thread at the VLC forums here with the same conclusions:

    https://forum.videolan.org/viewtopic.php?f=32&t=104345

    So because of this issue in LibVLC, the same issue appears in vlcj with no perfectly satisfactory solution (at the moment).

    In your code, you do this:

    mediaPlayer.playMedia(...);
    mediaPlayer.setVolume(0);
    mediaPlayer.getVolume();
    

    You need to be aware that playMedia actually starts media player asynchronously, so at the point that function returns, and you try to set the volume immediately after, the media is absolutely not guaranteed to have actually started yet and because of this issue you can't set/get the volume yet.

    To summarise, there is no ideal solution available to this right now, and it will likely require a change to LibVLC unless you are prepared to live with sub-optimal workarounds.

    By the way, those "main vout display error" messages are irrelevant.