-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcasting.dart
188 lines (169 loc) · 5.53 KB
/
casting.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import 'dart:async';
import 'package:bitmovin_player/bitmovin_player.dart';
import 'package:bitmovin_player_example/controls.dart';
import 'package:bitmovin_player_example/env/env.dart';
import 'package:bitmovin_player_example/platform.dart';
import 'package:bitmovin_player_example/player_info.dart';
import 'package:bitmovin_player_example/player_view_container.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
class Casting extends StatefulWidget {
const Casting({super.key});
static String routeName = 'Casting';
@override
State<Casting> createState() => _CastingState();
}
class _PlayerState {
_PlayerState(this.player, this.castManager);
final Player player;
final BitmovinCastManager castManager;
}
const artOfMotionDash =
'https://cdn.bitmovin.com/content/assets/MI201109210084/mpds/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.mpd';
const artOfMotionHls =
'https://cdn.bitmovin.com/content/assets/MI201109210084/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8';
final SourceConfig _sourceConfig = isIOS
? const SourceConfig(url: artOfMotionHls, type: SourceType.hls)
: const SourceConfig(url: artOfMotionDash, type: SourceType.dash);
class _CastingState extends State<Casting> {
_CastingState() {
_playerState = createPlayerState(
_sourceConfig,
(Event event) {
_onEvent(logger, event);
},
);
}
final logger = Logger();
late final Future<_PlayerState> _playerState;
final _playerInfoKey = GlobalKey<PlayerInfoState>();
Future<_PlayerState> createPlayerState(
SourceConfig sourceConfig,
void Function(Event event) eventListener,
) async {
final castManager = await BitmovinCastManager.initialize();
final player = Player(
config: const PlayerConfig(
key: Env.bitmovinPlayerLicenseKey,
remoteControlConfig: RemoteControlConfig(
customReceiverConfig: {'key': 'value'},
),
),
)
..onCastAvailable = eventListener
..onCastWaitingForDevice = eventListener
..onCastStart = eventListener
..onCastStarted = eventListener
..onCastStopped = eventListener
..onCastTimeUpdated = eventListener;
// Configure playing DASH source on Chromecast, even if on iOS the local
// played back asset is HLS. This is to demonstrate how a different source
// can be used for remote playback than for local playback.
final source = Source(
sourceConfig: sourceConfig,
remoteControl: const SourceRemoteControlConfig(
castSourceConfig: SourceConfig(
url: artOfMotionDash,
type: SourceType.dash,
),
),
);
await player.loadSource(source);
return _PlayerState(player, castManager);
}
void _onEvent(
Logger logger,
Event event,
) {
_playerState.then(
(state) => _playerInfoKey.currentState?.updatePlayerInfo(
state.player,
event,
),
);
final eventName = '${event.runtimeType}';
final eventData = '$eventName ${event.toJson()}';
logger.d(eventData);
}
@override
void dispose() {
_playerState.then((state) => state.player.dispose());
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Casting'),
),
body: FutureBuilder(future: _playerState, builder: buildPlayer),
);
}
Widget buildPlayer(BuildContext _, AsyncSnapshot<_PlayerState> snapshot) {
final data = snapshot.data;
if (data != null) {
return buildWithPlayer(data.player, data.castManager);
}
final error = snapshot.error;
if (error != null) {
return Text('Platform error when creating Player: $error');
}
return const Text('Loading player...');
}
Widget buildWithPlayer(Player player, BitmovinCastManager castManager) {
return Column(
children: [
PlayerViewContainer(player: player),
Container(
margin: const EdgeInsets.only(top: 5),
child: Controls(
onLoadPressed: () => player.loadSourceConfig(_sourceConfig),
onPlayPressed: player.play,
onPausePressed: player.pause,
onMutePressed: player.mute,
onUnmutePressed: player.unmute,
onSkipForwardPressed: () async =>
player.seek(await player.currentTime + 10),
onSkipBackwardPressed: () async =>
player.seek(await player.currentTime - 10),
),
),
Row(
children: [
Container(
margin: const EdgeInsets.only(left: 10, right: 5),
child: OutlinedButton(
onPressed: player.castVideo,
child: const Text('Cast Video'),
),
),
Container(
margin: const EdgeInsets.only(left: 5, right: 5),
child: OutlinedButton(
onPressed: player.castStop,
child: const Text('Stop Casting'),
),
),
],
),
Row(
children: [
Container(
margin: const EdgeInsets.only(left: 10, right: 5),
child: OutlinedButton(
onPressed: () => castManager.sendMessage(message: 'message'),
child: const Text('Send cast message'),
),
),
],
),
Expanded(
child: Container(
margin: const EdgeInsets.fromLTRB(10, 10, 10, 10),
child: PlayerInfo(key: _playerInfoKey),
),
),
],
);
}
}