Append items to queue in sample

This commit is contained in:
gianlucaparadise 2022-02-10 06:10:31 +01:00
parent a63e84d5b8
commit 7937df603a
2 changed files with 97 additions and 22 deletions

View file

@ -113,7 +113,7 @@ class _MyAppState extends State<MyApp> {
}
void _onCastVideo() {
final request = getMediaLoadRequestData();
final request = getMediaLoadRequestData(mainVideo);
castFramework.castContext.sessionManager.remoteMediaClient.load(request);
}
@ -139,6 +139,14 @@ class _MyAppState extends State<MyApp> {
);
}
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<MyApp> {
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,
),
),

View file

@ -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<VideoInfo> 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>[mediaTrack];
final mediaInfo = MediaInfo()
..contentId = videoUrl
..contentId = video.url
..streamType = StreamType.buffered
..contentType = videoType
..contentType = video.type
..mediaMetadata = mediaMetadata
..mediaTracks = <MediaTrack>[]
..streamDuration = videoDuration;
..streamDuration = video.duration;
final request = MediaLoadRequestData()
..shouldAutoplay = true
..currentTime = 0
..mediaInfo = mediaInfo;
return request;
return mediaInfo;
}