Adds Apple airplay/Google cast support to the app
This commit is contained in:
@@ -0,0 +1,211 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
enum CastingStatus { unavailable, idle, connecting, connected, error }
|
||||
|
||||
enum CastingPlaybackStatus { unknown, stopped, buffering, playing }
|
||||
|
||||
class CastingBusinessObject extends ChangeNotifier {
|
||||
static const MethodChannel _methodChannel = MethodChannel(
|
||||
'kryz_go_flutter/casting/methods',
|
||||
);
|
||||
static const EventChannel _eventChannel = EventChannel(
|
||||
'kryz_go_flutter/casting/events',
|
||||
);
|
||||
|
||||
StreamSubscription<dynamic>? _eventSubscription;
|
||||
CastingStatus _status = CastingStatus.unavailable;
|
||||
CastingPlaybackStatus _playbackStatus = CastingPlaybackStatus.unknown;
|
||||
String? _connectedDeviceName;
|
||||
String? _lastError;
|
||||
|
||||
CastingStatus get status => _status;
|
||||
CastingPlaybackStatus get playbackStatus => _playbackStatus;
|
||||
String? get connectedDeviceName => _connectedDeviceName;
|
||||
String? get lastError => _lastError;
|
||||
|
||||
bool get isSupportedOnThisPlatform {
|
||||
if (kIsWeb) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return defaultTargetPlatform == TargetPlatform.iOS ||
|
||||
defaultTargetPlatform == TargetPlatform.android;
|
||||
}
|
||||
|
||||
bool get isConnected => _status == CastingStatus.connected;
|
||||
bool get isPlaying => _playbackStatus == CastingPlaybackStatus.playing;
|
||||
bool get isBuffering => _playbackStatus == CastingPlaybackStatus.buffering;
|
||||
bool get isStopped => _playbackStatus == CastingPlaybackStatus.stopped;
|
||||
|
||||
Future<void> initialize() async {
|
||||
if (!isSupportedOnThisPlatform) {
|
||||
_setStatus(CastingStatus.unavailable);
|
||||
return;
|
||||
}
|
||||
|
||||
_eventSubscription?.cancel();
|
||||
_eventSubscription = _eventChannel.receiveBroadcastStream().listen(
|
||||
_handleEvent,
|
||||
onError: (Object error) {
|
||||
_lastError = error.toString();
|
||||
_setStatus(CastingStatus.error);
|
||||
},
|
||||
);
|
||||
|
||||
try {
|
||||
final dynamic status = await _methodChannel.invokeMethod<dynamic>(
|
||||
'getStatus',
|
||||
);
|
||||
_handleEvent(status);
|
||||
} on PlatformException catch (error) {
|
||||
_lastError = error.message ?? error.code;
|
||||
_setStatus(CastingStatus.error);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> showDevicePicker() async {
|
||||
if (!isSupportedOnThisPlatform) {
|
||||
_setStatus(CastingStatus.unavailable);
|
||||
return;
|
||||
}
|
||||
|
||||
_setStatus(CastingStatus.connecting);
|
||||
|
||||
try {
|
||||
await _methodChannel.invokeMethod<void>('showDevicePicker');
|
||||
} on PlatformException catch (error) {
|
||||
_lastError = error.message ?? error.code;
|
||||
_setStatus(CastingStatus.error);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> startCastingLiveStream({
|
||||
required String streamUrl,
|
||||
required String title,
|
||||
required String subtitle,
|
||||
}) async {
|
||||
if (!isSupportedOnThisPlatform) {
|
||||
_setStatus(CastingStatus.unavailable);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await _methodChannel.invokeMethod<void>('startCastingLiveStream', {
|
||||
'streamUrl': streamUrl,
|
||||
'title': title,
|
||||
'subtitle': subtitle,
|
||||
});
|
||||
} on PlatformException catch (error) {
|
||||
_lastError = error.message ?? error.code;
|
||||
_setStatus(CastingStatus.error);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> disconnect() async {
|
||||
if (!isSupportedOnThisPlatform) {
|
||||
_setStatus(CastingStatus.unavailable);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await _methodChannel.invokeMethod<void>('disconnect');
|
||||
} on PlatformException catch (error) {
|
||||
_lastError = error.message ?? error.code;
|
||||
_setStatus(CastingStatus.error);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> stopPlayback() async {
|
||||
if (!isSupportedOnThisPlatform) {
|
||||
_setStatus(CastingStatus.unavailable);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await _methodChannel.invokeMethod<void>('stopCastingPlayback');
|
||||
} on PlatformException catch (error) {
|
||||
_lastError = error.message ?? error.code;
|
||||
_setStatus(CastingStatus.error);
|
||||
}
|
||||
}
|
||||
|
||||
void _handleEvent(dynamic event) {
|
||||
if (event is! Map) {
|
||||
return;
|
||||
}
|
||||
|
||||
final String? status = event['status'] as String?;
|
||||
final String? playbackStatus = event['playbackStatus'] as String?;
|
||||
final String? deviceName = event['deviceName'] as String?;
|
||||
final String? message = event['message'] as String?;
|
||||
|
||||
if (deviceName != null && deviceName.isNotEmpty) {
|
||||
_connectedDeviceName = deviceName;
|
||||
}
|
||||
|
||||
if (message != null && message.isNotEmpty) {
|
||||
_lastError = message;
|
||||
}
|
||||
|
||||
_setPlaybackStatus(_parsePlaybackStatus(playbackStatus));
|
||||
|
||||
switch (status) {
|
||||
case 'idle':
|
||||
_setStatus(CastingStatus.idle);
|
||||
break;
|
||||
case 'connecting':
|
||||
_setStatus(CastingStatus.connecting);
|
||||
break;
|
||||
case 'connected':
|
||||
_setStatus(CastingStatus.connected);
|
||||
break;
|
||||
case 'error':
|
||||
_setStatus(CastingStatus.error);
|
||||
break;
|
||||
case 'unavailable':
|
||||
default:
|
||||
_setStatus(CastingStatus.unavailable);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void _setStatus(CastingStatus status) {
|
||||
if (_status == status) {
|
||||
return;
|
||||
}
|
||||
|
||||
_status = status;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
CastingPlaybackStatus _parsePlaybackStatus(String? playbackStatus) {
|
||||
switch (playbackStatus) {
|
||||
case 'playing':
|
||||
return CastingPlaybackStatus.playing;
|
||||
case 'buffering':
|
||||
return CastingPlaybackStatus.buffering;
|
||||
case 'stopped':
|
||||
return CastingPlaybackStatus.stopped;
|
||||
default:
|
||||
return CastingPlaybackStatus.unknown;
|
||||
}
|
||||
}
|
||||
|
||||
void _setPlaybackStatus(CastingPlaybackStatus status) {
|
||||
if (_playbackStatus == status) {
|
||||
return;
|
||||
}
|
||||
|
||||
_playbackStatus = status;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_eventSubscription?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
+182
-11
@@ -1,7 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:just_audio_background/just_audio_background.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'app_theme_business_object.dart';
|
||||
import 'casting_business_object.dart';
|
||||
import 'live_info_business_object.dart';
|
||||
import 'live_info_panel.dart';
|
||||
import 'streaming_audio_business_object.dart';
|
||||
@@ -32,9 +34,17 @@ class MyApp extends StatefulWidget {
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
final StreamingAudioBusinessObject _audio = StreamingAudioBusinessObject();
|
||||
final CastingBusinessObject _casting = CastingBusinessObject();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_casting.initialize();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_casting.dispose();
|
||||
_audio.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
@@ -46,16 +56,21 @@ class _MyAppState extends State<MyApp> {
|
||||
theme: AppThemeBusinessObject.lightTheme(),
|
||||
darkTheme: AppThemeBusinessObject.darkTheme(),
|
||||
themeMode: ThemeMode.system,
|
||||
home: MainPage(audio: _audio),
|
||||
home: MainPage(audio: _audio, casting: _casting),
|
||||
debugShowCheckedModeBanner: false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MainPage extends StatefulWidget {
|
||||
const MainPage({super.key, required this.audio});
|
||||
const MainPage({
|
||||
super.key,
|
||||
required this.audio,
|
||||
required this.casting,
|
||||
});
|
||||
|
||||
final StreamingAudioBusinessObject audio;
|
||||
final CastingBusinessObject casting;
|
||||
|
||||
@override
|
||||
State<MainPage> createState() => _MainPageState();
|
||||
@@ -63,24 +78,61 @@ class MainPage extends StatefulWidget {
|
||||
|
||||
class _MainPageState extends State<MainPage> {
|
||||
StreamingAudioBusinessObject get _audio => widget.audio;
|
||||
CastingBusinessObject get _casting => widget.casting;
|
||||
final LiveInfoBusinessObject _liveInfo = LiveInfoBusinessObject();
|
||||
static const List<int> _intervalOptions = <int>[5, 10, 15, 30, 60];
|
||||
bool _castStreamStartedForActiveSession = false;
|
||||
bool _isStartingCastStream = false;
|
||||
bool _pendingRemotePlay = false;
|
||||
|
||||
bool get _usesExternalCastPlayback =>
|
||||
_casting.isConnected && defaultTargetPlatform == TargetPlatform.android;
|
||||
|
||||
bool get _isIosAirPlay => defaultTargetPlatform == TargetPlatform.iOS;
|
||||
|
||||
bool get _isUsingRemotePlayback => _usesExternalCastPlayback;
|
||||
|
||||
bool get _isPlaybackStopped {
|
||||
if (_isUsingRemotePlayback) {
|
||||
return _casting.isStopped || _casting.playbackStatus == CastingPlaybackStatus.unknown;
|
||||
}
|
||||
|
||||
return _audio.isStopped;
|
||||
}
|
||||
|
||||
bool get _isPlaybackPlaying {
|
||||
if (_isUsingRemotePlayback) {
|
||||
return _casting.isPlaying;
|
||||
}
|
||||
|
||||
return _audio.isPlaying;
|
||||
}
|
||||
|
||||
bool get _isPlaybackBuffering {
|
||||
if (_isUsingRemotePlayback) {
|
||||
return _casting.isBuffering;
|
||||
}
|
||||
|
||||
return _audio.isBuffering;
|
||||
}
|
||||
|
||||
String _playbackStatusLabel() {
|
||||
switch (_audio.playbackStatus) {
|
||||
case StreamingPlaybackStatus.stopped:
|
||||
return 'Stopped';
|
||||
case StreamingPlaybackStatus.buffering:
|
||||
return 'Buffering...';
|
||||
case StreamingPlaybackStatus.playing:
|
||||
return 'Playing';
|
||||
if (_isPlaybackBuffering) {
|
||||
return 'Buffering...';
|
||||
}
|
||||
|
||||
if (_isPlaybackPlaying) {
|
||||
return 'Playing';
|
||||
}
|
||||
|
||||
return 'Stopped';
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_audio.addListener(_handleAudioStateChanged);
|
||||
_casting.addListener(_handleCastingStateChanged);
|
||||
_liveInfo.addListener(_handleLiveInfoChanged);
|
||||
_liveInfo.start();
|
||||
}
|
||||
@@ -105,17 +157,127 @@ class _MainPageState extends State<MainPage> {
|
||||
});
|
||||
}
|
||||
|
||||
void _handleCastingStateChanged() {
|
||||
if (
|
||||
_usesExternalCastPlayback &&
|
||||
(!_castStreamStartedForActiveSession || _pendingRemotePlay) &&
|
||||
!_isStartingCastStream) {
|
||||
_startCastingLiveStream();
|
||||
}
|
||||
|
||||
if (!_usesExternalCastPlayback) {
|
||||
_castStreamStartedForActiveSession = false;
|
||||
_isStartingCastStream = false;
|
||||
_pendingRemotePlay = false;
|
||||
}
|
||||
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
// Rebuild when cast state changes from the native platforms.
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _startCastingLiveStream() async {
|
||||
_isStartingCastStream = true;
|
||||
|
||||
try {
|
||||
await _casting.startCastingLiveStream(
|
||||
streamUrl: _audio.streamUrl,
|
||||
title: 'KRYZ Live Stream',
|
||||
subtitle: 'KRYZ Radio',
|
||||
);
|
||||
|
||||
_castStreamStartedForActiveSession = true;
|
||||
_pendingRemotePlay = false;
|
||||
|
||||
if (_usesExternalCastPlayback && !_audio.isStopped) {
|
||||
await _audio.stop();
|
||||
}
|
||||
} catch (_) {
|
||||
_castStreamStartedForActiveSession = false;
|
||||
// State is already updated by the casting business object.
|
||||
} finally {
|
||||
_isStartingCastStream = false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _play() async {
|
||||
if (_usesExternalCastPlayback) {
|
||||
if (_casting.isPlaying || _isStartingCastStream) {
|
||||
return;
|
||||
}
|
||||
|
||||
await _startCastingLiveStream();
|
||||
return;
|
||||
}
|
||||
|
||||
if (_usesExternalCastPlayback && _casting.status == CastingStatus.connecting) {
|
||||
_pendingRemotePlay = true;
|
||||
return;
|
||||
}
|
||||
|
||||
await _audio.play();
|
||||
}
|
||||
|
||||
Future<void> _stop() async {
|
||||
if (_usesExternalCastPlayback) {
|
||||
await _casting.stopPlayback();
|
||||
return;
|
||||
}
|
||||
|
||||
await _audio.stop();
|
||||
}
|
||||
|
||||
Future<void> _toggleCasting() async {
|
||||
if (_casting.isConnected) {
|
||||
await _casting.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
await _casting.showDevicePicker();
|
||||
}
|
||||
|
||||
String _castingLabel() {
|
||||
final idleLabel = _isIosAirPlay ? 'AirPlay' : 'Cast';
|
||||
final connectingLabel = _isIosAirPlay ? 'AirPlay...' : 'Casting...';
|
||||
final errorLabel = _isIosAirPlay ? 'AirPlay Error' : 'Cast Error';
|
||||
final unavailableLabel = _isIosAirPlay ? 'No AirPlay' : 'No Cast';
|
||||
|
||||
switch (_casting.status) {
|
||||
case CastingStatus.idle:
|
||||
return idleLabel;
|
||||
case CastingStatus.connecting:
|
||||
return connectingLabel;
|
||||
case CastingStatus.connected:
|
||||
final deviceName = _casting.connectedDeviceName;
|
||||
if (deviceName != null && deviceName.isNotEmpty) {
|
||||
return 'On $deviceName';
|
||||
}
|
||||
return _isIosAirPlay ? 'AirPlaying' : 'Casting';
|
||||
case CastingStatus.error:
|
||||
return errorLabel;
|
||||
case CastingStatus.unavailable:
|
||||
return unavailableLabel;
|
||||
}
|
||||
}
|
||||
|
||||
IconData _castingIcon() {
|
||||
if (_isIosAirPlay) {
|
||||
return Icons.airplay_rounded;
|
||||
}
|
||||
|
||||
return _casting.isConnected
|
||||
? Icons.cast_connected_rounded
|
||||
: Icons.cast_rounded;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_audio.removeListener(_handleAudioStateChanged);
|
||||
_casting.removeListener(_handleCastingStateChanged);
|
||||
_liveInfo.removeListener(_handleLiveInfoChanged);
|
||||
_liveInfo.dispose();
|
||||
super.dispose();
|
||||
@@ -225,15 +387,24 @@ class _MainPageState extends State<MainPage> {
|
||||
runSpacing: 12,
|
||||
children: [
|
||||
ElevatedButton.icon(
|
||||
onPressed: _audio.isStopped ? _play : null,
|
||||
onPressed: _isPlaybackStopped ? _play : null,
|
||||
icon: const Icon(Icons.play_arrow_rounded),
|
||||
label: const Text('Play'),
|
||||
),
|
||||
ElevatedButton.icon(
|
||||
onPressed: _audio.isStopped ? null : _stop,
|
||||
onPressed: _isPlaybackStopped ? null : _stop,
|
||||
icon: const Icon(Icons.stop_rounded),
|
||||
label: const Text('Stop'),
|
||||
),
|
||||
ElevatedButton.icon(
|
||||
onPressed:
|
||||
_casting.status == CastingStatus.connecting ||
|
||||
_casting.status == CastingStatus.unavailable
|
||||
? null
|
||||
: _toggleCasting,
|
||||
icon: Icon(_castingIcon()),
|
||||
label: Text(_castingLabel()),
|
||||
),
|
||||
Text(
|
||||
_playbackStatusLabel(),
|
||||
style: theme.textTheme.titleSmall?.copyWith(
|
||||
|
||||
Reference in New Issue
Block a user