mirror of
https://github.com/johrpan/musicus_mobile.git
synced 2025-10-26 18:57:25 +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
|
package de.johrpan.musicus_player
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.media.MediaPlayer
|
||||||
|
import android.net.Uri
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
import io.flutter.embedding.engine.plugins.FlutterPlugin
|
import io.flutter.embedding.engine.plugins.FlutterPlugin
|
||||||
|
|
@ -11,14 +15,41 @@ import io.flutter.plugin.common.PluginRegistry.Registrar
|
||||||
|
|
||||||
public class MusicusPlayerPlugin: FlutterPlugin, MethodCallHandler {
|
public class MusicusPlayerPlugin: FlutterPlugin, MethodCallHandler {
|
||||||
private lateinit var channel: MethodChannel
|
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) {
|
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
|
||||||
channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "de.johrpan.musicus_player/platform")
|
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) {
|
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) {
|
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,43 @@
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
|
/// A simple music player.
|
||||||
|
///
|
||||||
|
/// Give it an URI using [setUri] and it will start playing.
|
||||||
class MusicusPlayer {
|
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');
|
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);
|
_channel.setMethodCallHandler(_handleMethodCall);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _handleMethodCall(MethodCall call) {
|
Future<void> _handleMethodCall(MethodCall call) async {
|
||||||
// TODO: Implement.
|
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<void> setUri(String uri) async {
|
||||||
|
await _channel.invokeMethod('setUri', {'uri': uri});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Play from the current URI and resume playback if previously paused.
|
||||||
|
Future<void> play() async {
|
||||||
|
await _channel.invokeMethod('play');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pause playback.
|
||||||
|
Future<void> pause() async {
|
||||||
|
await _channel.invokeMethod('pause');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue