我想在颤动中访问我的系统铃声
Posted
技术标签:
【中文标题】我想在颤动中访问我的系统铃声【英文标题】:I want to access my systems ringtones in flutter 【发布时间】:2021-12-29 07:11:29 【问题描述】:有什么方法可以使用flutter获取手机的所有铃声,并将选择的铃声设置为我的应用的默认铃声?
提前致谢
【问题讨论】:
你可以参考这个链接pub.dev/packages/flutter_ringtone_player 我试过了,但它播放的是默认的。我想要的是全部获取它们并让用户选择他想要的。 那么我认为你必须使用 MethodChannel 编写特定于平台的代码来获取 llst。 谢谢,这就是我要做的。 很高兴为您提供帮助... 【参考方案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');
知道您需要实现本机代码。转到 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
就是这样,希望对您有所帮助。有任何问题请告诉我。
【讨论】:
以上是关于我想在颤动中访问我的系统铃声的主要内容,如果未能解决你的问题,请参考以下文章