From cc5e5f969673b4d605ddf910361960ab4ad6abcd Mon Sep 17 00:00:00 2001 From: gianlucaparadise Date: Tue, 16 Nov 2021 09:00:29 +0100 Subject: [PATCH] RemoteMediaClient implemented in example app - Fix android implementation --- .../media/MediaLoadRequestDataHelper.kt | 30 ++++++------- example/lib/main.dart | 16 +++++-- .../lib/media_load_request_data_helper.dart | 45 +++++++++++++++++++ 3 files changed, 73 insertions(+), 18 deletions(-) create mode 100644 example/lib/media_load_request_data_helper.dart diff --git a/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/media/MediaLoadRequestDataHelper.kt b/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/media/MediaLoadRequestDataHelper.kt index fea8261..cac9837 100644 --- a/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/media/MediaLoadRequestDataHelper.kt +++ b/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/media/MediaLoadRequestDataHelper.kt @@ -9,7 +9,7 @@ import com.google.android.gms.cast.MediaTrack import com.google.android.gms.common.images.WebImage import org.json.JSONObject -fun getMediaLoadRequestData(request: PlatformBridgeApis.MediaLoadRequestData) : MediaLoadRequestData { +fun getMediaLoadRequestData(request: PlatformBridgeApis.MediaLoadRequestData): MediaLoadRequestData { val mediaInfo = getMediaInfo(request.mediaInfo) return MediaLoadRequestData.Builder() @@ -19,13 +19,13 @@ fun getMediaLoadRequestData(request: PlatformBridgeApis.MediaLoadRequestData) : .build() } -fun getMediaInfo(mediaInfo: PlatformBridgeApis.MediaInfo?) : MediaInfo? { +fun getMediaInfo(mediaInfo: PlatformBridgeApis.MediaInfo?): MediaInfo? { if (mediaInfo == null) return null val streamType = getStreamType(mediaInfo.streamType) val metadata = getMediaMetadata(mediaInfo.mediaMetadata) val mediaTracks = mediaInfo.mediaTracks.map { getMediaTrack(it) } - val customData = JSONObject(mediaInfo.customDataAsJson) + val customData = JSONObject(mediaInfo.customDataAsJson ?: "{}") return MediaInfo.Builder(mediaInfo.contentId) .setStreamType(streamType) @@ -37,7 +37,7 @@ fun getMediaInfo(mediaInfo: PlatformBridgeApis.MediaInfo?) : MediaInfo? { .build() } -fun getStreamType(streamType: PlatformBridgeApis.StreamType) : Int { +fun getStreamType(streamType: PlatformBridgeApis.StreamType): Int { return when (streamType) { PlatformBridgeApis.StreamType.invalid -> -1 PlatformBridgeApis.StreamType.none -> 0 @@ -46,7 +46,7 @@ fun getStreamType(streamType: PlatformBridgeApis.StreamType) : Int { } } -fun getMediaMetadata(mediaMetadata: PlatformBridgeApis.MediaMetadata) : MediaMetadata { +fun getMediaMetadata(mediaMetadata: PlatformBridgeApis.MediaMetadata): MediaMetadata { val mediaType = getMediaType(mediaMetadata.mediaType) val result = MediaMetadata(mediaType) @@ -64,7 +64,7 @@ fun getMediaMetadata(mediaMetadata: PlatformBridgeApis.MediaMetadata) : MediaMet return result } -fun getMediaType(mediaType: PlatformBridgeApis.MediaType) : Int { +fun getMediaType(mediaType: PlatformBridgeApis.MediaType): Int { return when (mediaType) { PlatformBridgeApis.MediaType.generic -> 0 PlatformBridgeApis.MediaType.movie -> 1 @@ -109,7 +109,7 @@ fun getMediaType(mediaType: PlatformBridgeApis.MediaType) : Int { // } //} -fun getMediaTrack(mediaTrack: PlatformBridgeApis.MediaTrack) : MediaTrack { +fun getMediaTrack(mediaTrack: PlatformBridgeApis.MediaTrack): MediaTrack { val trackType = getTrackType(mediaTrack.trackType) val trackSubtype = getTrackSubtype(mediaTrack.trackSubtype) @@ -120,7 +120,7 @@ fun getMediaTrack(mediaTrack: PlatformBridgeApis.MediaTrack) : MediaTrack { .build() } -fun getTrackType(trackType: PlatformBridgeApis.TrackType) : Int { +fun getTrackType(trackType: PlatformBridgeApis.TrackType): Int { return when (trackType) { PlatformBridgeApis.TrackType.unknown -> 0 PlatformBridgeApis.TrackType.text -> 1 @@ -129,14 +129,14 @@ fun getTrackType(trackType: PlatformBridgeApis.TrackType) : Int { } } -fun getTrackSubtype(trackSubtype: PlatformBridgeApis.TrackSubtype) : Int { +fun getTrackSubtype(trackSubtype: PlatformBridgeApis.TrackSubtype): Int { return when (trackSubtype) { PlatformBridgeApis.TrackSubtype.unknown -> -1 - PlatformBridgeApis.TrackSubtype.none -> 0 - PlatformBridgeApis.TrackSubtype.subtitles -> 1 - PlatformBridgeApis.TrackSubtype.captions -> 2 - PlatformBridgeApis.TrackSubtype.descriptions -> 3 - PlatformBridgeApis.TrackSubtype.chapters -> 4 - PlatformBridgeApis.TrackSubtype.metadata -> 5 + PlatformBridgeApis.TrackSubtype.none -> 0 + PlatformBridgeApis.TrackSubtype.subtitles -> 1 + PlatformBridgeApis.TrackSubtype.captions -> 2 + PlatformBridgeApis.TrackSubtype.descriptions -> 3 + PlatformBridgeApis.TrackSubtype.chapters -> 4 + PlatformBridgeApis.TrackSubtype.metadata -> 5 } } diff --git a/example/lib/main.dart b/example/lib/main.dart index d49014e..7d7ea34 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -2,6 +2,8 @@ import 'package:flutter/material.dart'; import 'package:flutter_cast_framework/cast.dart'; import 'package:flutter_cast_framework/widgets.dart'; +import 'media_load_request_data_helper.dart'; + void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @@ -18,7 +20,7 @@ class _MyAppState extends State { final textMessageController = TextEditingController(); - final String castNamespace = 'urn:x-cast:cast-your-instructions'; + final String castNamespace = 'urn:x-cast:flutter-cast-framework-demo'; @override void initState() { @@ -48,8 +50,7 @@ class _MyAppState extends State { void _onSessionStateChanged() { debugPrint("Session state changed from example"); setState(() { - _sessionState = - castFramework.castContext.sessionManager.state.value; + _sessionState = castFramework.castContext.sessionManager.state.value; }); } @@ -66,6 +67,11 @@ class _MyAppState extends State { .sendMessage(castNamespace, message); } + void _onCastVideo() { + final request = getMediaLoadRequestData(); + castFramework.castContext.sessionManager.remoteMediaClient.load(request); + } + @override Widget build(BuildContext context) { return MaterialApp( @@ -104,6 +110,10 @@ class _MyAppState extends State { ], ), Text('Received Message: $_message'), + ElevatedButton( + child: Text('Cast video'), + onPressed: _onCastVideo, + ) ], ), ), diff --git a/example/lib/media_load_request_data_helper.dart b/example/lib/media_load_request_data_helper.dart new file mode 100644 index 0000000..14bbaa6 --- /dev/null +++ b/example/lib/media_load_request_data_helper.dart @@ -0,0 +1,45 @@ +import 'package:flutter_cast_framework/cast.dart'; + +const videoUrl = + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/mp4/BigBuckBunny.mp4"; +const videoType = "mp4"; +const videoDuration = 596 * 1000; +const videoThumb = + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/images/480x270/BigBuckBunny.jpg"; +const videoPoster = + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/images/780x1200/BigBuckBunny-780x1200.jpg"; + +MediaLoadRequestData getMediaLoadRequestData() { + final img = WebImage()..url = videoThumb; + final bigImg = WebImage()..url = videoPoster; + final mediaMetadata = MediaMetadata() + ..mediaType = MediaType.movie + ..webImages = [ + img, + bigImg, + ]; + + // final mediaTrack = MediaTrack() + // ..contentId = "" + // ..id = 0 + // ..language = "" + // ..name = "" + // ..trackType = TrackType.unknown + // ..trackSubtype = TrackSubtype.unknown; + // final mediaTracks = [mediaTrack]; + + final mediaInfo = MediaInfo() + ..contentId = videoUrl + ..streamType = StreamType.buffered + ..contentType = videoType + ..mediaMetadata = mediaMetadata + ..mediaTracks = [] + ..streamDuration = videoDuration; + + final request = MediaLoadRequestData() + ..shouldAutoplay = true + ..currentTime = 0 + ..mediaInfo = mediaInfo; + + return request; +}