mirror of
https://github.com/johrpan/musicus_mobile.git
synced 2025-10-27 03:07:26 +01:00
musicus_player: Add basic playback functionality
This commit is contained in:
parent
bf0168a2d3
commit
2df5546eb6
2 changed files with 66 additions and 5 deletions
|
|
@ -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<String>("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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue