[英]Custom sound not working if we make release build in flutter app
Everything is working fine in debug mode and if I run, flutter run --release.在调试模式下一切正常,如果我运行,flutter run --release。 But custom sound is not working if I generate release build and running using that.
但是,如果我生成发布版本并使用它运行,自定义声音将不起作用。 I am using flutter.
我正在使用 flutter。
here is the code,这是代码,
Future _showNotificationWithSound(title, message) async {
var vibrationPattern = Int64List(4);
vibrationPattern[0] = 0;
vibrationPattern[1] = 1000;
vibrationPattern[2] = 5000;
vibrationPattern[3] = 2000;
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'added_my_channel_id',
'abc',
'abc',
icon: 'app_icon',
playSound: true,
sound: RawResourceAndroidNotificationSound('notifiysound'),
vibrationPattern: vibrationPattern,
enableLights: true,
color: const Color.fromARGB(255, 255, 0, 0),
ledColor: const Color.fromARGB(255, 255, 0, 0),
ledOnMs: 1000,
ledOffMs: 500);
var iOSPlatformChannelSpecifics =
IOSNotificationDetails(sound: 'notifiysound.mp3');
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
flutterLocalNotificationsPlugin. show(
0, title, message,platformChannelSpecifics);
}
You're probably missing a keep.xml in raw folder.您可能在 raw 文件夹中缺少 keep.xml 。 Then the files won't be included in build but only available when running in debug mode.
然后文件将不会包含在构建中,但仅在以调试模式运行时可用。 See last answer here: Flutter local notification custom sound doesn't work (path issue)
在此处查看最后一个答案: Flutter 本地通知自定义声音不起作用(路径问题)
Add full path instead of file name添加完整路径而不是文件名
Incorret: IOSNotificationDetails(sound: 'notifiysound.mp3'); Incorret: IOSNotificationDetails(sound: 'notifiysound.mp3');
Correct: IOSNotificationDetails(sound: 'assets/notifiysound.mp3');正确: IOSNotificationDetails(sound: 'assets/notifiysound.mp3');
please put below code in your MainActivity.java file in android folder.请将以下代码放入 android 文件夹中的 MainActivity.java 文件中。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
Uri soundUri=Uri.parse("android.resource://"+getApplicationContext()
.getPackageName() + "/" + R.raw.alert);
AudioAttributes audioAttributes =AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
NotificationChannel channel = new
NotificationChannel("channelId","channelName",
NotificationManager.IMPORTANCE_HIGH);
channel.setSound(soundUri, audioAttributes);
NotificationManager notificationManager =
getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
and put your Custom sound mp3 file in your projects android/app/src/raw/mp3 file
并将您的自定义声音 mp3 文件放入您的项目 android/app/src/raw/mp3 文件中
Note: it will only work for Android custom sound注意:它仅适用于 Android 自定义声音
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.