Class MediaDevicesApi

java.lang.Object
com.oorian.html.js.JavaScriptApi
com.oorian.html.js.mediadevices.MediaDevicesApi

public class MediaDevicesApi extends JavaScriptApi
Provides access to media devices (cameras, microphones) from server-side Java code.

This API wraps the browser's MediaDevices API, allowing you to enumerate available media devices and request access to camera and microphone.

Usage:


 public class MyPage extends HtmlPage implements MediaDevicesListener {

     @Override
     protected void createBody(Body body) {
         body.registerListener(this, MediaDevicesEvent.class);
         body.registerListener(this, MediaDevicesErrorEvent.class);

         // Enumerate available devices
         MediaDevicesApi.enumerateDevices(body);
     }

     @Override
     public void onEvent(MediaDevicesEvent event) {
         for (MediaDevice device : event.getDevices()) {
             System.out.println(device.getKind() + ": " + device.getLabel());
         }
     }

     @Override
     public void onEvent(MediaDevicesErrorEvent event) {
         showMessage("Error: " + event.getMessage());
     }
 }
 
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • MediaDevicesApi

      public MediaDevicesApi()
  • Method Details

    • enumerateDevices

      public static void enumerateDevices(Element element)
      Enumerates available media devices using the current page context.

      Returns a list of available cameras, microphones, and audio output devices. Note that device labels may be empty until the user grants permission.

      Parameters:
      element - The element that will receive the device list event.
    • enumerateDevices

      public static void enumerateDevices(HtmlPage page, Element element)
      Enumerates available media devices.

      Returns a list of available cameras, microphones, and audio output devices. Note that device labels may be empty until the user grants permission.

      Parameters:
      page - The page context.
      element - The element that will receive the device list event.
    • getUserMedia

      public static void getUserMedia(Element element, boolean audio, boolean video)
      Requests access to user media (camera and/or microphone) using the current page context.

      This will prompt the user to grant permission. After permission is granted, a MediaDevicesEvent will be fired with the updated device list including labels.

      Parameters:
      element - The element that will receive media events.
      audio - True to request audio access.
      video - True to request video access.
    • getUserMedia

      public static void getUserMedia(HtmlPage page, Element element, boolean audio, boolean video)
      Requests access to user media (camera and/or microphone).

      This will prompt the user to grant permission. After permission is granted, a MediaDevicesEvent will be fired with the updated device list including labels.

      Parameters:
      page - The page context.
      element - The element that will receive media events.
      audio - True to request audio access.
      video - True to request video access.
    • requestCameraAccess

      public static void requestCameraAccess(Element element)
      Requests access to the camera only using the current page context.
      Parameters:
      element - The element that will receive media events.
    • requestCameraAccess

      public static void requestCameraAccess(HtmlPage page, Element element)
      Requests access to the camera only.
      Parameters:
      page - The page context.
      element - The element that will receive media events.
    • requestMicrophoneAccess

      public static void requestMicrophoneAccess(Element element)
      Requests access to the microphone only using the current page context.
      Parameters:
      element - The element that will receive media events.
    • requestMicrophoneAccess

      public static void requestMicrophoneAccess(HtmlPage page, Element element)
      Requests access to the microphone only.
      Parameters:
      page - The page context.
      element - The element that will receive media events.
    • requestFullAccess

      public static void requestFullAccess(Element element)
      Requests access to both camera and microphone using the current page context.
      Parameters:
      element - The element that will receive media events.
    • requestFullAccess

      public static void requestFullAccess(HtmlPage page, Element element)
      Requests access to both camera and microphone.
      Parameters:
      page - The page context.
      element - The element that will receive media events.