Skip to content

Commit

Permalink
Implement createMappedAudioSourceSource API in MethodChannelAudioPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
hacker1024 committed Jul 23, 2022
1 parent fc9e498 commit 5e45b08
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
5 changes: 5 additions & 0 deletions just_audio/lib/just_audio.dart
Expand Up @@ -1329,6 +1329,11 @@ class AudioPlayer {
final platform = active
? await (_nativePlatform = _pluginPlatform.init(InitRequest(
id: _id,
getAudioSourceMessage: (id) {
assert(_audioSources.containsKey(id),
'Audio source with ID $id does not exist!');
return _audioSources[id]!._toMessage();
},
audioLoadConfiguration: _audioLoadConfiguration?._toMessage(),
androidAudioEffects: (_isAndroid() || _isUnitTest())
? _audioPipeline.androidAudioEffects
Expand Down
Expand Up @@ -63,8 +63,12 @@ abstract class JustAudioPlatform extends PlatformInterface {
/// [AudioPlayerPlatform] methods.
abstract class AudioPlayerPlatform {
final String id;
final AudioSourceMessageGetter getAudioSourceMessage;

AudioPlayerPlatform(this.id);
AudioPlayerPlatform(
this.id, {
this.getAudioSourceMessage = _unimplementedGetAudioServiceMessage,
});

/// A broadcast stream of playback events.
Stream<PlaybackEventMessage> get playbackEventMessageStream {
Expand Down Expand Up @@ -222,8 +226,14 @@ abstract class AudioPlayerPlatform {
throw UnimplementedError(
"androidEqualizerBandSetGain() has not been implemented.");
}

static Never _unimplementedGetAudioServiceMessage(String id) =>
throw UnimplementedError(
'getAudioServiceMessage() has not been implemented.');
}

typedef AudioSourceMessageGetter = AudioSourceMessage Function(String id);

/// A data update communicated from the platform implementation to the Flutter
/// plugin. Each field should trigger a state update in the frontend plugin if
/// and only if it is not null. Normally, the platform implementation will not
Expand Down Expand Up @@ -385,13 +395,15 @@ class IcyHeadersMessage {
/// player instance.
class InitRequest {
final String id;
AudioSourceMessageGetter getAudioSourceMessage;
final AudioLoadConfigurationMessage? audioLoadConfiguration;
final List<AudioEffectMessage> androidAudioEffects;
final List<AudioEffectMessage> darwinAudioEffects;
final bool? androidOffloadSchedulingEnabled;

InitRequest({
required this.id,
required this.getAudioSourceMessage,
this.audioLoadConfiguration,
this.androidAudioEffects = const [],
this.darwinAudioEffects = const [],
Expand Down
30 changes: 26 additions & 4 deletions just_audio_platform_interface/lib/method_channel_just_audio.dart
Expand Up @@ -11,7 +11,10 @@ class MethodChannelJustAudio extends JustAudioPlatform {
@override
Future<AudioPlayerPlatform> init(InitRequest request) async {
await _mainChannel.invokeMethod<void>('init', request.toMap());
return MethodChannelAudioPlayer(request.id);
return MethodChannelAudioPlayer(
request.id,
getAudioSourceMessage: request.getAudioSourceMessage,
);
}

@override
Expand All @@ -35,9 +38,16 @@ class MethodChannelJustAudio extends JustAudioPlatform {
class MethodChannelAudioPlayer extends AudioPlayerPlatform {
final MethodChannel _channel;

MethodChannelAudioPlayer(String id)
: _channel = MethodChannel('com.ryanheise.just_audio.methods.$id'),
super(id);
MethodChannelAudioPlayer(
String id, {
required AudioSourceMessageGetter getAudioSourceMessage,
}) : _channel = MethodChannel('com.ryanheise.just_audio.methods.$id'),
super(
id,
getAudioSourceMessage: getAudioSourceMessage,
) {
_channel.setMethodCallHandler(_methodCallHandler);
}

@override
Stream<PlaybackEventMessage> get playbackEventMessageStream =>
Expand Down Expand Up @@ -223,4 +233,16 @@ class MethodChannelAudioPlayer extends AudioPlayerPlatform {
(await _channel.invokeMethod<Map<dynamic, dynamic>>(
'androidEqualizerBandSetGain', request.toMap()))!);
}

Future<Object?> _methodCallHandler(MethodCall call) async {
switch (call.method) {
case 'createMappedAudioSourceSource':
final id = (call.arguments as Map)['id'] as String;
final message = getAudioSourceMessage(id) as MappingAudioSourceMessage;
final innerMessage = await message.createAudioSourceMessage();
return innerMessage.toMap();
default:
throw UnimplementedError('Unimplemented method: ${call.method}');
}
}
}
21 changes: 18 additions & 3 deletions just_audio_web/lib/just_audio_web.dart
Expand Up @@ -23,7 +23,10 @@ class JustAudioPlugin extends JustAudioPlatform {
code: "error",
message: "Platform player ${request.id} already exists");
}
final player = Html5AudioPlayer(id: request.id);
final player = Html5AudioPlayer(
id: request.id,
getAudioServiceMessage: request.getAudioSourceMessage,
);
players[request.id] = player;
return player;
}
Expand Down Expand Up @@ -57,7 +60,13 @@ abstract class JustAudioPlayer extends AudioPlayerPlatform {
double _speed = 1.0;

/// Creates a platform player with the given [id].
JustAudioPlayer({required String id}) : super(id);
JustAudioPlayer({
required String id,
required AudioSourceMessageGetter getAudioServiceMessage,
}) : super(
id,
getAudioSourceMessage: getAudioServiceMessage,
);

@mustCallSuper
Future<void> release() async {
Expand Down Expand Up @@ -108,7 +117,13 @@ class Html5AudioPlayer extends JustAudioPlayer {
final Map<String, AudioSourcePlayer> _audioSourcePlayers = {};

/// Creates an [Html5AudioPlayer] with the given [id].
Html5AudioPlayer({required String id}) : super(id: id) {
Html5AudioPlayer(
{required String id,
required AudioSourceMessageGetter getAudioServiceMessage})
: super(
id: id,
getAudioServiceMessage: getAudioServiceMessage,
) {
_audioElement.addEventListener('durationchange', (event) {
_durationCompleter?.complete();
broadcastPlaybackEvent();
Expand Down

0 comments on commit 5e45b08

Please sign in to comment.