diff --git a/android/src/main/java/com/gianlucaparadise/flutter_cast_framework/PlatformBridgeApis.java b/android/src/main/java/com/gianlucaparadise/flutter_cast_framework/PlatformBridgeApis.java index 3fb776c..b1156c0 100644 --- a/android/src/main/java/com/gianlucaparadise/flutter_cast_framework/PlatformBridgeApis.java +++ b/android/src/main/java/com/gianlucaparadise/flutter_cast_framework/PlatformBridgeApis.java @@ -358,6 +358,9 @@ public class PlatformBridgeApis { void showCastDialog(); void loadMediaLoadRequestData(MediaLoadRequestData request); MediaInfo getMediaInfo(); + void play(); + void pause(); + void stop(); /** The codec used by CastHostApi. */ static MessageCodec getCodec() { @@ -452,6 +455,63 @@ public class PlatformBridgeApis { channel.setMessageHandler(null); } } + { + BasicMessageChannel channel = + new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.CastHostApi.play", getCodec()); + if (api != null) { + channel.setMessageHandler((message, reply) -> { + Map wrapped = new HashMap<>(); + try { + api.play(); + wrapped.put("result", null); + } + catch (Error | RuntimeException exception) { + wrapped.put("error", wrapError(exception)); + } + reply.reply(wrapped); + }); + } else { + channel.setMessageHandler(null); + } + } + { + BasicMessageChannel channel = + new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.CastHostApi.pause", getCodec()); + if (api != null) { + channel.setMessageHandler((message, reply) -> { + Map wrapped = new HashMap<>(); + try { + api.pause(); + wrapped.put("result", null); + } + catch (Error | RuntimeException exception) { + wrapped.put("error", wrapError(exception)); + } + reply.reply(wrapped); + }); + } else { + channel.setMessageHandler(null); + } + } + { + BasicMessageChannel channel = + new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.CastHostApi.stop", getCodec()); + if (api != null) { + channel.setMessageHandler((message, reply) -> { + Map wrapped = new HashMap<>(); + try { + api.stop(); + wrapped.put("result", null); + } + catch (Error | RuntimeException exception) { + wrapped.put("error", wrapError(exception)); + } + reply.reply(wrapped); + }); + } else { + channel.setMessageHandler(null); + } + } } } private static class CastFlutterApiCodec extends StandardMessageCodec { diff --git a/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt b/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt index 83cc3b5..52e9772 100644 --- a/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt +++ b/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt @@ -255,6 +255,21 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa return getFlutterMediaInfo(hostMediaInfo) } + + override fun play() { + val remoteMediaClient: RemoteMediaClient = remoteMediaClient ?: return + remoteMediaClient.play() + } + + override fun pause() { + val remoteMediaClient: RemoteMediaClient = remoteMediaClient ?: return + remoteMediaClient.pause() + } + + override fun stop() { + val remoteMediaClient: RemoteMediaClient = remoteMediaClient ?: return + remoteMediaClient.stop() + } } private fun getOnNamespaceResult(oldSession: CastSession?, newSession: CastSession?) = PlatformBridgeApis.CastFlutterApi.Reply> { namespaces -> diff --git a/ios/Classes/PlatformBridgeApis.h b/ios/Classes/PlatformBridgeApis.h index 99619f9..715edb8 100644 --- a/ios/Classes/PlatformBridgeApis.h +++ b/ios/Classes/PlatformBridgeApis.h @@ -96,6 +96,9 @@ NSObject *CastHostApiGetCodec(void); - (void)showCastDialogWithError:(FlutterError *_Nullable *_Nonnull)error; - (void)loadMediaLoadRequestDataRequest:(MediaLoadRequestData *)request error:(FlutterError *_Nullable *_Nonnull)error; - (nullable MediaInfo *)getMediaInfoWithError:(FlutterError *_Nullable *_Nonnull)error; +- (void)playWithError:(FlutterError *_Nullable *_Nonnull)error; +- (void)pauseWithError:(FlutterError *_Nullable *_Nonnull)error; +- (void)stopWithError:(FlutterError *_Nullable *_Nonnull)error; @end extern void CastHostApiSetup(id binaryMessenger, NSObject *_Nullable api); diff --git a/ios/Classes/PlatformBridgeApis.m b/ios/Classes/PlatformBridgeApis.m index 57204f0..229f840 100644 --- a/ios/Classes/PlatformBridgeApis.m +++ b/ios/Classes/PlatformBridgeApis.m @@ -344,6 +344,60 @@ void CastHostApiSetup(id binaryMessenger, NSObject) { + remoteMediaClient?.play() + } + + public func pauseWithError(_ error: AutoreleasingUnsafeMutablePointer) { + remoteMediaClient?.pause() + } + + public func stopWithError(_ error: AutoreleasingUnsafeMutablePointer) { + remoteMediaClient?.stop() + } + // MARK: - GCKSessionManagerListener // onSessionSuspended diff --git a/lib/src/PlatformBridgeApis.dart b/lib/src/PlatformBridgeApis.dart index c767eb2..353ece8 100644 --- a/lib/src/PlatformBridgeApis.dart +++ b/lib/src/PlatformBridgeApis.dart @@ -356,6 +356,75 @@ class CastHostApi { return (replyMap['result'] as MediaInfo?)!; } } + + Future play() async { + final BasicMessageChannel channel = BasicMessageChannel( + 'dev.flutter.pigeon.CastHostApi.play', codec, binaryMessenger: _binaryMessenger); + final Map? replyMap = + await channel.send(null) as Map?; + if (replyMap == null) { + throw PlatformException( + code: 'channel-error', + message: 'Unable to establish connection on channel.', + details: null, + ); + } else if (replyMap['error'] != null) { + final Map error = (replyMap['error'] as Map?)!; + throw PlatformException( + code: (error['code'] as String?)!, + message: error['message'] as String?, + details: error['details'], + ); + } else { + return; + } + } + + Future pause() async { + final BasicMessageChannel channel = BasicMessageChannel( + 'dev.flutter.pigeon.CastHostApi.pause', codec, binaryMessenger: _binaryMessenger); + final Map? replyMap = + await channel.send(null) as Map?; + if (replyMap == null) { + throw PlatformException( + code: 'channel-error', + message: 'Unable to establish connection on channel.', + details: null, + ); + } else if (replyMap['error'] != null) { + final Map error = (replyMap['error'] as Map?)!; + throw PlatformException( + code: (error['code'] as String?)!, + message: error['message'] as String?, + details: error['details'], + ); + } else { + return; + } + } + + Future stop() async { + final BasicMessageChannel channel = BasicMessageChannel( + 'dev.flutter.pigeon.CastHostApi.stop', codec, binaryMessenger: _binaryMessenger); + final Map? replyMap = + await channel.send(null) as Map?; + if (replyMap == null) { + throw PlatformException( + code: 'channel-error', + message: 'Unable to establish connection on channel.', + details: null, + ); + } else if (replyMap['error'] != null) { + final Map error = (replyMap['error'] as Map?)!; + throw PlatformException( + code: (error['code'] as String?)!, + message: error['message'] as String?, + details: error['details'], + ); + } else { + return; + } + } } class _CastFlutterApiCodec extends StandardMessageCodec { diff --git a/lib/src/PlatformBridgeApisDefinition.dart b/lib/src/PlatformBridgeApisDefinition.dart index 17fb31d..a0c57e9 100644 --- a/lib/src/PlatformBridgeApisDefinition.dart +++ b/lib/src/PlatformBridgeApisDefinition.dart @@ -223,6 +223,9 @@ abstract class CastHostApi { void showCastDialog(); void loadMediaLoadRequestData(MediaLoadRequestData request); MediaInfo getMediaInfo(); + void play(); + void pause(); + void stop(); } @FlutterApi() diff --git a/lib/src/cast/RemoteMediaClient.dart b/lib/src/cast/RemoteMediaClient.dart index 313d704..37f5600 100644 --- a/lib/src/cast/RemoteMediaClient.dart +++ b/lib/src/cast/RemoteMediaClient.dart @@ -13,6 +13,18 @@ class RemoteMediaClient { _hostApi.loadMediaLoadRequestData(request); } + void play() { + _hostApi.play(); + } + + void pause() { + _hostApi.pause(); + } + + void stop() { + _hostApi.stop(); + } + Future getMediaInfo() async { return await _hostApi.getMediaInfo(); }