Caprica Software

vlcj 4.x Tutorial

Dialogs

Some API methods may cause a native dialog to be requested by VLC.

For example if there is an error playing media, or if VLC needs to request credentials from the user if playing a protected stream.

By default, native dialog requests are not enabled.


Enabling Dialogs

Native dialogs are enabled via the MediaPlayerFactory:

MediaPlayerFactory factory = new MediaPlayerFactory();
Dialogs dialogs = factory.dialogs().newDialogs();
factory.dialogs().enable(dialogs);

After creating the factory, this example uses that factory to create a Dialogs component. This dialogs component is a bridge between the Java application and the dialogs requested by the native library.

Once a dialogs component has been created, it is enabled via the dialogs API on the factory.

Now, any request to display a native dialog will get routed via this dialogs component.


Adding Dialog Handlers

This example shows how to add a handler for an error dialog request:

dialogs.addDialogHandler(new DialogHandler() {
@Override
public void displayError(Long userData, String title, String text) {
System.out.printf("display error: %s - %s - %s%n", userData, title, text);
}
});

This example shows how to add a handler for a login credentials dialog request, note here the use of the dismiss method to close/cancel the dialog:

dialogs.addDialogHandler(new DialogHandler() {
private int loginCount = 0;
@Override
public void displayLogin(Long userData, DialogId id, String title, String text, String defaultUsername, boolean askStore) {
System.out.printf("display login: %s - %s - %s - %s - %s - %s%n", userData, id, title, text, defaultUsername, askStore);
// In a real application these would be requested in an actual dialog box,
// or read from some configuration somewhere
String username = "deckard";
String password = "nexus6";
if (loginCount < 3) {
loginCount++;
factory.dialogs().postLogin(id, username, password, false);
} else {
factory.dialogs().dismiss(id);
}
}
});

This is just an example to show the concept, obviously a real application would provide something more robust.

Specifically for the login dialog, it is necessary to specify a keystore when creating the media player factory, for example:

MediaPlayerFactory factory = new MediaPlayerFactory("--keystore=secret");

Without this keystore setting the application will most likely hang when requesting the dialog.


General Notes

Any number of dialog handlers can be added, or implementations for multiple dialogs can be provided in a single handler.

As these examples have demonstrated, there is no requirement to display an actual dialog box to handle these dialog requests.