flutter - 有没有办法在 Flutter 中控制系统音乐?

我希望能够暂停/播放和跳过正在不同应用程序(例如 Spotify/Youtube Music 等)中播放的音乐。有没有办法在 Flutter 应用程序中控制它?

提前致谢!

最佳答案

据我所知,目前没有针对此的软件包。我有一个我正在尝试运行的仅限 Android 的应用程序,解决方案是创建一个平台 channel ,然后为此做一些事情。

package com.example.myapp

import android.content.Context
import android.content.Intent
import android.media.AudioManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.KeyEvent
import androidx.annotation.RequiresApi
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {
  private val METHOD_CHANNEL_NAME = "com.example.myapp/media_control"
  private var methodChannel: MethodChannel? = null
  private var audioManager: AudioManager? = null;

  @RequiresApi(Build.VERSION_CODES.O)
  override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
      super.configureFlutterEngine(flutterEngine)

      setupChannels(this, flutterEngine.dartExecutor.binaryMessenger)
  }

  @RequiresApi(Build.VERSION_CODES.O)
  override fun onCreate(savedInstanceState: Bundle?) {
      audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
      super.onCreate(savedInstanceState)
  }

  @RequiresApi(Build.VERSION_CODES.O)
  private fun setupChannels(mainActivity: MainActivity, binaryMessenger: BinaryMessenger) {
      methodChannel = MethodChannel(binaryMessenger, METHOD_CHANNEL_NAME)
      methodChannel!!.setMethodCallHandler { call, result ->
            if(call.method == "pause") {
                sendMediaButton(KeyEvent.KEYCODE_MEDIA_PAUSE)
                result.success("Success");
            }
            else if (call.method == "play") {
                sendMediaButton(KeyEvent.KEYCODE_MEDIA_PLAY)
                result.success("Success");
            }
            else if (call.method == "skipNext") {
                sendMediaButton(KeyEvent.KEYCODE_MEDIA_NEXT)
                result.success("Success");
            }
            else if (call.method == "skipPrevious") {
                sendMediaButton(KeyEvent.KEYCODE_MEDIA_PREVIOUS)
                result.success("Success");
            }
      }
  }

    private fun sendMediaButton(keyCode: Int) {
        var keyEvent = KeyEvent(KeyEvent.ACTION_DOWN, keyCode)
        audioManager!!.dispatchMediaKeyEvent(keyEvent)

        keyEvent = KeyEvent(KeyEvent.ACTION_UP, keyCode)
        audioManager!!.dispatchMediaKeyEvent(keyEvent)
    }

}

https://stackoverflow.com/questions/60345024/

相关文章:

angular - 使用 GitHub Actions 运行 Angular e2e 测试会抛出 "

node.js - 错误 "Your cache folder contains root-owne

logging - 如何为 glib 调试日志添加文件名、行、函数前缀

python-3.x - 从目录流式传输图像并将预测与 tensorflow 中的文件名相关联

vue.js - 如何更新 vuetify mdi 图标? (Nuxt.js)

python - API错误异常: (BadArgument) 'recognitionModel'

django - 使用 xlswriter || 自动调整列宽 Django

git - 当我提交 Git 时,找不到 cmd.exe

python - 由于自定义度量函数,Keras 中出现无效的磁带状态错误

python - 使用 PIL 合并图像时模式不匹配