diff --git a/musicus_player/android/src/main/kotlin/de/johrpan/musicus_player/MusicusPlayerPlugin.kt b/musicus_player/android/src/main/kotlin/de/johrpan/musicus_player/MusicusPlayerPlugin.kt index e134ef0..dac9bcb 100644 --- a/musicus_player/android/src/main/kotlin/de/johrpan/musicus_player/MusicusPlayerPlugin.kt +++ b/musicus_player/android/src/main/kotlin/de/johrpan/musicus_player/MusicusPlayerPlugin.kt @@ -1,5 +1,9 @@ package de.johrpan.musicus_player +import android.content.Context +import android.media.MediaPlayer +import android.net.Uri + import androidx.annotation.NonNull; import io.flutter.embedding.engine.plugins.FlutterPlugin @@ -11,14 +15,41 @@ import io.flutter.plugin.common.PluginRegistry.Registrar public class MusicusPlayerPlugin: FlutterPlugin, MethodCallHandler { private lateinit var channel: MethodChannel + private lateinit var context: Context + + private var uri: Uri? = null + private var mediaPlayer: MediaPlayer? = null override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "de.johrpan.musicus_player/platform") - channel.setMethodCallHandler(this); + channel.setMethodCallHandler(this) + context = flutterPluginBinding.getApplicationContext() } override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { - // TODO: Implement. + if (call.method == "setUri") { + val newUri = Uri.parse(call.argument("uri")) + uri = newUri + + if (mediaPlayer != null) { + mediaPlayer?.release() + } + + mediaPlayer = MediaPlayer.create(context, uri) + mediaPlayer?.setOnCompletionListener { + channel.invokeMethod("onComplete", null) + } + + result.success(null) + } else if (call.method == "play") { + mediaPlayer?.start() + result.success(null) + } else if (call.method == "pause") { + mediaPlayer?.pause() + result.success(null) + } else { + result.notImplemented() + } } override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { diff --git a/musicus_player/lib/musicus_player.dart b/musicus_player/lib/musicus_player.dart index 2b252dd..105855e 100644 --- a/musicus_player/lib/musicus_player.dart +++ b/musicus_player/lib/musicus_player.dart @@ -1,13 +1,43 @@ import 'package:flutter/services.dart'; +/// A simple music player. +/// +/// Give it an URI using [setUri] and it will start playing. class MusicusPlayer { + /// Called, when the player reaches the end of the audio file. + final void Function() onComplete; + final _channel = MethodChannel('de.johrpan.musicus_player/platform'); - MusicusPlayer() { + /// Create a new player. + /// + /// This will do nothing, until [setUri] was called. If the player reaches + /// the end of the current audio file, [onComplete] will be called. + MusicusPlayer({this.onComplete}) { _channel.setMethodCallHandler(_handleMethodCall); } - Future _handleMethodCall(MethodCall call) { - // TODO: Implement. + Future _handleMethodCall(MethodCall call) async { + if (call.method == 'onComplete' && onComplete != null) { + onComplete(); + } + } + + /// Set URI of the audio file to play. + /// + /// If the player will always stop doing, what it did before, and start + /// playing from the provided URI if possible. + Future setUri(String uri) async { + await _channel.invokeMethod('setUri', {'uri': uri}); + } + + /// Play from the current URI and resume playback if previously paused. + Future play() async { + await _channel.invokeMethod('play'); + } + + /// Pause playback. + Future pause() async { + await _channel.invokeMethod('pause'); } }