From 7937df603abe3681c1b4b41edf7221a497e4b3fe Mon Sep 17 00:00:00 2001 From: gianlucaparadise Date: Thu, 10 Feb 2022 06:10:31 +0100 Subject: [PATCH] Append items to queue in sample --- example/lib/main.dart | 20 +++- .../lib/media_load_request_data_helper.dart | 99 +++++++++++++++---- 2 files changed, 97 insertions(+), 22 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index e74c136..b44037f 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -113,7 +113,7 @@ class _MyAppState extends State { } void _onCastVideo() { - final request = getMediaLoadRequestData(); + final request = getMediaLoadRequestData(mainVideo); castFramework.castContext.sessionManager.remoteMediaClient.load(request); } @@ -139,6 +139,14 @@ class _MyAppState extends State { ); } + void _appendToQueue() { + final sessionManager = castFramework.castContext.sessionManager; + for (var video in otherVideos) { + final item = getMediaQueueItem(video); + sessionManager.remoteMediaClient.queueAppendItem(item); + } + } + Widget _buildTitle(String text) { return Padding( padding: const EdgeInsets.all(8.0), @@ -210,10 +218,18 @@ class _MyAppState extends State { onPressed: _hasMedia ? _openExpandedControls : null, ), ), + _buildTitle("Queue"), Padding( padding: const EdgeInsets.all(8.0), child: ElevatedButton( - child: Text('Queue'), + child: Text("Append to Queue"), + onPressed: _hasSession ? _appendToQueue : null, + ), + ), + Padding( + padding: const EdgeInsets.all(8.0), + child: ElevatedButton( + child: Text('Show Queue'), onPressed: _hasSession ? _openQueue : null, ), ), diff --git a/example/lib/media_load_request_data_helper.dart b/example/lib/media_load_request_data_helper.dart index 14bbaa6..c9dfdc6 100644 --- a/example/lib/media_load_request_data_helper.dart +++ b/example/lib/media_load_request_data_helper.dart @@ -1,17 +1,81 @@ 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"; +/// in seconds +const double QUEUE_PRELOAD_TIME = 20; -MediaLoadRequestData getMediaLoadRequestData() { - final img = WebImage()..url = videoThumb; - final bigImg = WebImage()..url = videoPoster; +const mainVideo = const VideoInfo( + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/mp4/BigBuckBunny.mp4", + "mp4", + 596 * 1000, + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/images/480x270/BigBuckBunny.jpg", + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/images/780x1200/BigBuckBunny-780x1200.jpg", +); + +const List otherVideos = [ + const VideoInfo( + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/mp4/ElephantsDream.mp4", + "mp4", + 653 * 1000, + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/images/480x270/ElephantsDream.jpg", + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/images/780x1200/ElephantsDream-780x1200.jpg", + ), + const VideoInfo( + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/mp4/Sintel.mp4", + "mp4", + 888 * 1000, + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/images/480x270/Sintel.jpg", + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/images/780x1200/Sintel-780x1200.jpg", + ), + const VideoInfo( + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/mp4/TearsOfSteel.mp4", + "mp4", + 734 * 1000, + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/images/480x270/TearsOfSteel.jpg", + "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/images/780x1200/TearsOfSteel-780x1200.jpg", + ), +]; + +class VideoInfo { + const VideoInfo( + this.url, + this.type, + this.duration, + this.thumb, + this.poster, + ); + + final String url; + final String type; + final int duration; + final String thumb; + final String poster; +} + +MediaLoadRequestData getMediaLoadRequestData(VideoInfo video) { + final mediaInfo = _getMediaInfo(video); + + final request = MediaLoadRequestData() + ..shouldAutoplay = true + ..currentTime = 0 + ..mediaInfo = mediaInfo; + + return request; +} + +MediaQueueItem getMediaQueueItem(VideoInfo video) { + final mediaInfo = _getMediaInfo(video); + + final request = MediaQueueItem() + ..media = mediaInfo + ..autoplay = false + ..preloadTime = QUEUE_PRELOAD_TIME; + + return request; +} + +MediaInfo _getMediaInfo(VideoInfo video) { + final img = WebImage()..url = video.thumb; + final bigImg = WebImage()..url = video.poster; final mediaMetadata = MediaMetadata() ..mediaType = MediaType.movie ..webImages = [ @@ -29,17 +93,12 @@ MediaLoadRequestData getMediaLoadRequestData() { // final mediaTracks = [mediaTrack]; final mediaInfo = MediaInfo() - ..contentId = videoUrl + ..contentId = video.url ..streamType = StreamType.buffered - ..contentType = videoType + ..contentType = video.type ..mediaMetadata = mediaMetadata ..mediaTracks = [] - ..streamDuration = videoDuration; + ..streamDuration = video.duration; - final request = MediaLoadRequestData() - ..shouldAutoplay = true - ..currentTime = 0 - ..mediaInfo = mediaInfo; - - return request; + return mediaInfo; }