Xamarin Android 将 SetSound 用于通知通道以在通知时播放自定义声音
Xamarin Android use SetSound for Notification Channel to play custom sound on notification
我已经浪费了至少一天的时间来完成这项工作。收到通知后,我正在尝试播放放在 Resources/raw 中的 mp3 文件。我不知道如何获得 Uri。我的问题是:
1.To 播放自定义文件是否必须将其放在 Resources/raw 中,还是也可以放在 Xamarin Android 项目下的 Assets/Sounds 中。
2.How 我是否根据 mp3 文件所在的位置正确获取 Uri。
这是我的代码:
private void createNotificationChannel()
{
var channelName = GetString(Resource.String.noti_chan_urgent);
var channelDescription = GetString(Resource.String.noti_chan_urgent_description);
// set the vibration patterm for the channel
long[] vibrationPattern = { 100, 200, 300, 400, 500, 400, 300, 200, 400 };
// Creating an Audio Attribute
var alarmAttributes = new AudioAttributes.Builder().SetUsage(AudioUsageKind.Alarm).Build();
// Create the uri for the alarm file
var alarmUri = Android.Net.Uri.Parse("MyApp.Android/Resources/raw/alarm.mp3"); // this must be wrong because its not working
// create chan1 which is the urgent notifications channel
var chan1 = new NotificationChannel(PRIMARY_CHANNEL_ID, channelName, NotificationImportance.High)
{
Description = channelDescription
};
// set the channel properties
chan1.EnableLights(true);
chan1.LightColor = Color.Red;
chan1.EnableVibration(true);
chan1.SetVibrationPattern(vibrationPattern);
chan1.SetSound(alarmUri, alarmAttributes);
chan1.SetBypassDnd(true);
chan1.LockscreenVisibility = NotificationVisibility.Public;
var manager = (NotificationManager)GetSystemService(NotificationService);
manager.CreateNotificationChannel(chan1);
}
}
我想通了,我希望这比对问题投反对票更好地帮助别人,这就是你的做法:
(注意:确保将 mp3 文件放在 Resources/raw/soundFile.mp3 下的 Xamarin Android 项目中,并将文件构建为 Android资源).
然后像这样创建 Uri:
Android.Net.Uri alarmUri = Android.Net.Uri.Parse(${ContentResolver.SchemeAndroidResource}://{Context.PackageName}/{Resource.Raw.soundFile}");
像这样创建警报属性:
var alarmAttributes = new AudioAttributes.Builder()
.SetContentType(AudioContentType.Sonification)
.SetUsage(AudioUsageKind.Notification).Build();
最后 在频道本身设置声音 仅从 Android Oreo 开始(不在通知上,在应用程序启动时创建频道):
chan1.SetSound (alarmUri, alarmAttributes);
uri = Android.Net.Uri.Parse(
"android.resource://" + Application.Context.PackageName + "/raw/sound2");
我必须做的唯一改变。对 Fredsomofspeech 的回答。
android 9.
visualstudio 2019 xamarin.forms 移动 ios android。 sound2.mp3
是 运行 一个文件 android 无法播放,因此请务必下载一个 mp3 文件进行测试,确认它可以正常工作。
我已经浪费了至少一天的时间来完成这项工作。收到通知后,我正在尝试播放放在 Resources/raw 中的 mp3 文件。我不知道如何获得 Uri。我的问题是:
1.To 播放自定义文件是否必须将其放在 Resources/raw 中,还是也可以放在 Xamarin Android 项目下的 Assets/Sounds 中。
2.How 我是否根据 mp3 文件所在的位置正确获取 Uri。
这是我的代码:
private void createNotificationChannel()
{
var channelName = GetString(Resource.String.noti_chan_urgent);
var channelDescription = GetString(Resource.String.noti_chan_urgent_description);
// set the vibration patterm for the channel
long[] vibrationPattern = { 100, 200, 300, 400, 500, 400, 300, 200, 400 };
// Creating an Audio Attribute
var alarmAttributes = new AudioAttributes.Builder().SetUsage(AudioUsageKind.Alarm).Build();
// Create the uri for the alarm file
var alarmUri = Android.Net.Uri.Parse("MyApp.Android/Resources/raw/alarm.mp3"); // this must be wrong because its not working
// create chan1 which is the urgent notifications channel
var chan1 = new NotificationChannel(PRIMARY_CHANNEL_ID, channelName, NotificationImportance.High)
{
Description = channelDescription
};
// set the channel properties
chan1.EnableLights(true);
chan1.LightColor = Color.Red;
chan1.EnableVibration(true);
chan1.SetVibrationPattern(vibrationPattern);
chan1.SetSound(alarmUri, alarmAttributes);
chan1.SetBypassDnd(true);
chan1.LockscreenVisibility = NotificationVisibility.Public;
var manager = (NotificationManager)GetSystemService(NotificationService);
manager.CreateNotificationChannel(chan1);
}
}
我想通了,我希望这比对问题投反对票更好地帮助别人,这就是你的做法: (注意:确保将 mp3 文件放在 Resources/raw/soundFile.mp3 下的 Xamarin Android 项目中,并将文件构建为 Android资源).
然后像这样创建 Uri:
Android.Net.Uri alarmUri = Android.Net.Uri.Parse(${ContentResolver.SchemeAndroidResource}://{Context.PackageName}/{Resource.Raw.soundFile}");
像这样创建警报属性:
var alarmAttributes = new AudioAttributes.Builder()
.SetContentType(AudioContentType.Sonification)
.SetUsage(AudioUsageKind.Notification).Build();
最后 在频道本身设置声音 仅从 Android Oreo 开始(不在通知上,在应用程序启动时创建频道):
chan1.SetSound (alarmUri, alarmAttributes);
uri = Android.Net.Uri.Parse( "android.resource://" + Application.Context.PackageName + "/raw/sound2");
我必须做的唯一改变。对 Fredsomofspeech 的回答。
android 9. visualstudio 2019 xamarin.forms 移动 ios android。 sound2.mp3
是 运行 一个文件 android 无法播放,因此请务必下载一个 mp3 文件进行测试,确认它可以正常工作。