flutterflutter-getxlibvlc

Set user agent when changing video URL in Flutter VLC player


I am able to change the user agent when opening initial URL but unable to set the user agent when changing to new URL.

I have code for playing video with flutter vlc player and getx, this code :

in controller :

class VlcController extends GetxController {
  late VlcPlayerController videoPlayerController;

  @override
  void onInit() {
    super.onInit();
    initializePlayer();
  }

  @override
  void onClose() {
    super.onClose();
    videoPlayerController.stopRendererScanning();
    videoPlayerController.dispose();
  }

  void initializePlayer() {
    videoPlayerController = VlcPlayerController.network(
      'https://example.com/video1',
      hwAcc: HwAcc.full,
      autoPlay: true,
      options: VlcPlayerOptions(
        http: VlcHttpOptions([':http-user-agent=Example']),
      ),
    );
  }

  void changeVideoUrl(String newUrl) async {
    videoPlayerController.setMediaFromNetwork(newUrl);
  }
}

in view :

GetBuilder<VlcController>(
      builder: (vlcController) { 
        return VlcPlayer(
          controller: vlcController.videoPlayerController,
          aspectRatio: 16 / 9,
          placeholder: const Center(child: CircularProgressIndicator()),
        );
      },
    );

list video ontap :

GestureDetector(
 onTap: () {
  Get.find<VlcController>().changeVideoUrl('https://example.com/video2');
});

When I call initializePlayer(), user agent is sent as expected but when I call changeVideoUrl(), user agent is not sent.

  1. I have tried calling setMediaFromNetwork() when VLC Player is playing. It works but user agent is not sent.

  2. I have also tried calling:

    await videoPlayerController.stop();
    await videoPlayerController.dispose();
    videoPlayerController = VlcPlayerController.network(
      newUrl,
      hwAcc: HwAcc.full,
      autoPlay: true,
      options: VlcPlayerOptions(
        http: VlcHttpOptions([':http-user-agent=Example']),
      ),
    );

But there's an exception:

Unhandled Exception: LateInitializationError: Field '_viewId@1186035241' has not been initialized.

Solution

  • The problem is because in iOS, VLC's options is not reused by setMediaFromNetwork().

    I made changes in VLCViewBuilder.swift, and it worked. The user agent is now sent in iOS.

    public class VLCViewBuilder: NSObject, VlcPlayerApi{
    ...
    private var options: [String]
    
    init(registrar: FlutterPluginRegistrar) {
    ...
     options = []
    ...
    }
    
     public func create(_ input: CreateMessage, error: AutoreleasingUnsafeMutablePointer<FlutterError?>) {
     ...
            options = input.options as? [String] ?? []
            player?.setMediaPlayerUrl(
                uri: mediaUrl,
                isAssetUrl: isAssetUrl,
                autoPlay: input.autoPlay?.boolValue ?? true,
                hwAcc: input.hwAcc?.intValue ?? HWAccellerationType.HW_ACCELERATION_AUTOMATIC.rawValue,
                options: options
            )
     }
    
     public func setStreamUrl(_ input: SetMediaMessage, error: AutoreleasingUnsafeMutablePointer<FlutterError?>) {
     ...
     player?.setMediaPlayerUrl(
                uri: mediaUrl,
                isAssetUrl: isAssetUrl,
                autoPlay: input.autoPlay?.boolValue ?? true,
                hwAcc: input.hwAcc?.intValue ?? HWAccellerationType.HW_ACCELERATION_AUTOMATIC.rawValue,
                options: options
            )
    
     }
    }
    

    don't forget to run flutter clean