Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.
/ flutter_videoplayer Public archive

Experimental Flutter "on-top" native videoplayer for iOS (swift implementation)

Notifications You must be signed in to change notification settings

rxlabz/flutter_videoplayer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flutter iOS Videoplayer

⚠️ this example is not up to date with the last version of flutter platform API. Same principles but somes method names changed.

An example of native ios swift videoplayer on top of a flutter app using the Platform messaging API.

screen

Flutter (Dart)

Using a PlatformMethodChannel, the Flutter app can call methods from the platform "context" (iOS, Android,...)

const PlatformMethodChannel videoChannel =
  const PlatformMethodChannel("video", const JSONMethodCodec());

// ...

String response = await config.videoChannel.invokeMethod('playVideo', kVideoUrl);

Each message will receive an async response back. Here, each message receive a json response with a status = 1 || 0 and an optional duration.

{
  "status":1,
  "info":"0:20"
}

The Flutter app also listen to the native messages sended from iOS via a PlatformMessageChannel.

const PlatformMessageChannel progressChannel = const PlatformMessageChannel(
      kProgressChannelName, const JSONMessageCodec());

// ...

@override
void initState() {
  config.progressChannel.setMessageHandler((message) {
        setState(() {
          _videoProgress = message["data"];
        });
      });
}

iOS (Swift)

On iOS side, you find the corresponding channels with attached methodCall handler.

var playerChannel = FlutterMethodChannel(
    name: name,
    binaryMessenger: controller,
    codec: FlutterJSONMethodCodec.sharedInstance())

// ...

playerChannel?.setMethodCallHandler {
      (call: FlutterMethodCall?, result: FlutterResultReceiver?) -> Void in
      print("Swift-> methodCallHandler \(call!.method)")

      switch (call!.method) {
      case "playVideo":
        let res = try! self.playVideo(url: call!.arguments! as! String)
        result!("{\"status\":\(res),\"info\":\"\(self.timeFormatter.string(from: self.getVideoDuration())!)\"}", nil)
      case "pauseVideo":
        result!("{\"status\":\(self.pauseVideo())}", nil)
      case "stopVideo":
        result!("{\"status\":\(self.stopVideo())}", nil)
      default:
        print("Error !!! Unknown method -> \(call!.method)")
      }
    }

Platform to Dart

The FlutterMessageChannel offers a sendMessage() method to pass values with the "dart part" of the flutter app

var progressChannel = FlutterMessageChannel( name: name, binaryMessenger: controller,
    codec: FlutterJSONMessageCodec())

// ...

self.progressChannel.sendMessage(PlayerMessage(type: 2, data: progress).toMap())

Android, Audio...

For now this example only works on iOS ( ARM64 : iPhone5S+ ), you can find an older, sketchier ObjC/Java example here, with more types of player ( activities and audio ) for Android and iOS.

About

Experimental Flutter "on-top" native videoplayer for iOS (swift implementation)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published