我想在 flutter 中访问我的系统铃声

I want to access my systems ringtones in flutter

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

提前致谢

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

  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
    }
    

就是这样,希望对您有所帮助。任何问题让我知道。