player: Publish position and duration information

A new method getPosition was introduced and the setUri method returns
the duration of the new track now.
This commit is contained in:
Elias Projahn 2020-04-21 18:01:09 +02:00
parent 524a6d9994
commit eb7b45c39e
2 changed files with 20 additions and 9 deletions

View file

@ -40,10 +40,13 @@ public class MusicusPlayerPlugin: FlutterPlugin, MethodCallHandler {
channel.invokeMethod("onComplete", null) channel.invokeMethod("onComplete", null)
} }
result.success(null) result.success(mediaPlayer?.getDuration())
} else if (call.method == "play") { } else if (call.method == "play") {
mediaPlayer?.start() mediaPlayer?.start()
result.success(null) result.success(null)
} else if (call.method == "getPosition") {
// TODO: Check, if mediaPlayer is in a valid state.
result.success(mediaPlayer?.getCurrentPosition())
} else if (call.method == "pause") { } else if (call.method == "pause") {
mediaPlayer?.pause() mediaPlayer?.pause()
result.success(null) result.success(null)

View file

@ -13,7 +13,9 @@ class MusicusPlayer {
/// ///
/// This will do nothing, until [setUri] was called. If the player reaches /// This will do nothing, until [setUri] was called. If the player reaches
/// the end of the current audio file, [onComplete] will be called. /// the end of the current audio file, [onComplete] will be called.
MusicusPlayer({this.onComplete}) { MusicusPlayer({
this.onComplete,
}) {
_channel.setMethodCallHandler(_handleMethodCall); _channel.setMethodCallHandler(_handleMethodCall);
} }
@ -25,10 +27,11 @@ class MusicusPlayer {
/// Set URI of the audio file to play. /// Set URI of the audio file to play.
/// ///
/// If the player will always stop doing, what it did before, and start /// The player will always stop doing, what it did before, and start
/// playing from the provided URI if possible. /// playing from the provided URI if possible. The return value is the
Future<void> setUri(String uri) async { /// duration of the new track in milliseconds.
await _channel.invokeMethod('setUri', {'uri': uri}); Future<int> setUri(String uri) async {
return await _channel.invokeMethod('setUri', {'uri': uri});
} }
/// Play from the current URI and resume playback if previously paused. /// Play from the current URI and resume playback if previously paused.
@ -36,6 +39,11 @@ class MusicusPlayer {
await _channel.invokeMethod('play'); await _channel.invokeMethod('play');
} }
/// Get the current playback position in milliseconds.
Future<int> getPosition() async {
return await _channel.invokeMethod('getPosition');
}
/// Pause playback. /// Pause playback.
Future<void> pause() async { Future<void> pause() async {
await _channel.invokeMethod('pause'); await _channel.invokeMethod('pause');