RemoteMediaClient play, pause, stop

This commit is contained in:
gianlucaparadise 2021-11-30 06:09:32 +01:00
parent d8d815710f
commit 186549c6f0
8 changed files with 228 additions and 0 deletions

View file

@ -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<Object> getCodec() {
@ -452,6 +455,63 @@ public class PlatformBridgeApis {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.CastHostApi.play", getCodec());
if (api != null) {
channel.setMessageHandler((message, reply) -> {
Map<String, Object> 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<Object> channel =
new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.CastHostApi.pause", getCodec());
if (api != null) {
channel.setMessageHandler((message, reply) -> {
Map<String, Object> 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<Object> channel =
new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.CastHostApi.stop", getCodec());
if (api != null) {
channel.setMessageHandler((message, reply) -> {
Map<String, Object> 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 {

View file

@ -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<MutableList<String>> { namespaces ->

View file

@ -96,6 +96,9 @@ NSObject<FlutterMessageCodec> *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<FlutterBinaryMessenger> binaryMessenger, NSObject<CastHostApi> *_Nullable api);

View file

@ -344,6 +344,60 @@ void CastHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, NSObject<CastH
[channel setMessageHandler:nil];
}
}
{
FlutterBasicMessageChannel *channel =
[FlutterBasicMessageChannel
messageChannelWithName:@"dev.flutter.pigeon.CastHostApi.play"
binaryMessenger:binaryMessenger
codec:CastHostApiGetCodec()];
if (api) {
NSCAssert([api respondsToSelector:@selector(playWithError:)], @"CastHostApi api (%@) doesn't respond to @selector(playWithError:)", api);
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
FlutterError *error;
[api playWithError:&error];
callback(wrapResult(nil, error));
}];
}
else {
[channel setMessageHandler:nil];
}
}
{
FlutterBasicMessageChannel *channel =
[FlutterBasicMessageChannel
messageChannelWithName:@"dev.flutter.pigeon.CastHostApi.pause"
binaryMessenger:binaryMessenger
codec:CastHostApiGetCodec()];
if (api) {
NSCAssert([api respondsToSelector:@selector(pauseWithError:)], @"CastHostApi api (%@) doesn't respond to @selector(pauseWithError:)", api);
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
FlutterError *error;
[api pauseWithError:&error];
callback(wrapResult(nil, error));
}];
}
else {
[channel setMessageHandler:nil];
}
}
{
FlutterBasicMessageChannel *channel =
[FlutterBasicMessageChannel
messageChannelWithName:@"dev.flutter.pigeon.CastHostApi.stop"
binaryMessenger:binaryMessenger
codec:CastHostApiGetCodec()];
if (api) {
NSCAssert([api respondsToSelector:@selector(stopWithError:)], @"CastHostApi api (%@) doesn't respond to @selector(stopWithError:)", api);
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
FlutterError *error;
[api stopWithError:&error];
callback(wrapResult(nil, error));
}];
}
else {
[channel setMessageHandler:nil];
}
}
}
@interface CastFlutterApiCodecReader : FlutterStandardReader
@end

View file

@ -203,6 +203,18 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio
return getFlutterMediaInfo(mediaInfo: hostMediaInfo)
}
public func playWithError(_ error: AutoreleasingUnsafeMutablePointer<FlutterError?>) {
remoteMediaClient?.play()
}
public func pauseWithError(_ error: AutoreleasingUnsafeMutablePointer<FlutterError?>) {
remoteMediaClient?.pause()
}
public func stopWithError(_ error: AutoreleasingUnsafeMutablePointer<FlutterError?>) {
remoteMediaClient?.stop()
}
// MARK: - GCKSessionManagerListener
// onSessionSuspended

View file

@ -356,6 +356,75 @@ class CastHostApi {
return (replyMap['result'] as MediaInfo?)!;
}
}
Future<void> play() async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.CastHostApi.play', codec, binaryMessenger: _binaryMessenger);
final Map<Object?, Object?>? replyMap =
await channel.send(null) as Map<Object?, Object?>?;
if (replyMap == null) {
throw PlatformException(
code: 'channel-error',
message: 'Unable to establish connection on channel.',
details: null,
);
} else if (replyMap['error'] != null) {
final Map<Object?, Object?> error = (replyMap['error'] as Map<Object?, Object?>?)!;
throw PlatformException(
code: (error['code'] as String?)!,
message: error['message'] as String?,
details: error['details'],
);
} else {
return;
}
}
Future<void> pause() async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.CastHostApi.pause', codec, binaryMessenger: _binaryMessenger);
final Map<Object?, Object?>? replyMap =
await channel.send(null) as Map<Object?, Object?>?;
if (replyMap == null) {
throw PlatformException(
code: 'channel-error',
message: 'Unable to establish connection on channel.',
details: null,
);
} else if (replyMap['error'] != null) {
final Map<Object?, Object?> error = (replyMap['error'] as Map<Object?, Object?>?)!;
throw PlatformException(
code: (error['code'] as String?)!,
message: error['message'] as String?,
details: error['details'],
);
} else {
return;
}
}
Future<void> stop() async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.CastHostApi.stop', codec, binaryMessenger: _binaryMessenger);
final Map<Object?, Object?>? replyMap =
await channel.send(null) as Map<Object?, Object?>?;
if (replyMap == null) {
throw PlatformException(
code: 'channel-error',
message: 'Unable to establish connection on channel.',
details: null,
);
} else if (replyMap['error'] != null) {
final Map<Object?, Object?> error = (replyMap['error'] as Map<Object?, Object?>?)!;
throw PlatformException(
code: (error['code'] as String?)!,
message: error['message'] as String?,
details: error['details'],
);
} else {
return;
}
}
}
class _CastFlutterApiCodec extends StandardMessageCodec {

View file

@ -223,6 +223,9 @@ abstract class CastHostApi {
void showCastDialog();
void loadMediaLoadRequestData(MediaLoadRequestData request);
MediaInfo getMediaInfo();
void play();
void pause();
void stop();
}
@FlutterApi()

View file

@ -13,6 +13,18 @@ class RemoteMediaClient {
_hostApi.loadMediaLoadRequestData(request);
}
void play() {
_hostApi.play();
}
void pause() {
_hostApi.pause();
}
void stop() {
_hostApi.stop();
}
Future<MediaInfo> getMediaInfo() async {
return await _hostApi.getMediaInfo();
}