【问题标题】:I want to access my systems ringtones in flutter我想在颤动中访问我的系统铃声
【发布时间】:2021-12-29 07:11:29
【问题描述】:

有什么方法可以使用flutter获取手机的所有铃声,并将选择的铃声设置为我的应用的默认铃声?

提前致谢

【问题讨论】:

  • 你可以参考这个链接pub.dev/packages/flutter_ringtone_player
  • 我试过了,但它播放的是默认的。我想要的是全部获取它们并让用户选择他想要的。
  • 那么我认为你必须使用 MethodChannel 编写特定于平台的代码来获取 llst。
  • 谢谢,这就是我要做的。
  • 很高兴为您提供帮助...

标签: android flutter dart flutter-dependencies dart-pub


【解决方案1】:

我设法使用本机代码完成了它

  1. 首先,您将在 Flutter 端创建这些东西。

    
    // here where your ringtones will be stored
    List<Object?> result = ['Andromeda'];
    
    // this is the channel that links flutter with native code
    static const channel = MethodChannel('com.example.pomo_app/mychannel');
    
    // this method waits for results from the native code 
    Future<void> getRingtones() async {
        try {
          result = await channel.invokeMethod('getAllRingtones');
        } on PlatformException catch (ex) {
          print('Exception: $ex.message');
        }
      }
    
    
  2. 知道您需要实现本机代码。转到 MainActivity.kt

    // add the name of the channel that we made in flutter code
    private val channel = "com.example.pomo_app/mychannel"
    
    // add this method to handle the calls from flutter
    MethodChannel(flutterEngine.dartExecutor.binaryMessenger, channel)
      .setMethodCallHandler { call, result ->
                when (call.method) {
                    "getAllRingtones" -> {
                        result.success(getAllRingtones(this))
                    }
    
    
    
    // this function will return all the ringtones names as a list
    private fun getAllRingtones(context: Context): List<String> {
    
      val manager = RingtoneManager(context)
      manager.setType(RingtoneManager.TYPE_RINGTONE)
    
      val cursor: Cursor = manager.cursor
    
      val list: MutableList<String> = mutableListOf()
      while (cursor.moveToNext()) {
        val notificationTitle: String = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX)
        list.add(notificationTitle)
      }
      return list
    }
    

就是这样,希望对您有所帮助。有任何问题请告诉我。

【讨论】:

    猜你喜欢
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多