Xamarin Android使用SetSound for Notification Channel在通知上播放自定义声音 [英] Xamarin Android use SetSound for Notification Channel to play custom sound on notification

查看:184
本文介绍了Xamarin Android使用SetSound for Notification Channel在通知上播放自定义声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我至少在浪费一天的时间来完成这项工作.收到通知后,我正在尝试播放放置在资源/原始文件中的mp3文件.我不知道该如何获得Uri.我的问题是:

I have I have been wasting at least a day trying to make this work. I am trying to play an mp3 file that i placed in Resources/raw once a notification is received. I do not know exactly how to get the Uri. My questions please are:

1.要播放自定义文件,您必须将其放置在Resources/raw中,或者也可以将其放置在Xamarin Android项目下的Assets/Sounds中.

1.To play a custom file do you have to place it in Resources/raw or can it be also in Assets/Sounds under the Xamarin Android project.

2.如何根据mp3文件所在的位置正确获取Uri.

2.How do i get the Uri correctly based on where the mp3 file resides.

这是我的代码:

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文件放在Xamarin Android项目的 Resources/raw/soundFile.mp3 下,并将文件构建为 Android资源.)

I figured it out and I hope this will help someone better than getting a downvote for a question, this is how you do it: (Note: Make sure you put your mp3 file in your Xamarin Android project under Resources/raw/soundFile.mp3 and build the file as Android Resource).

然后像这样创建Uri:

Then create the Uri like this:

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开始在频道本身上 setSound (不是在通知中,而是在应用程序启动时创建频道):

And finally setSound on the channel itself ONLY from Android Oreo onwards (not on the notification, create the channel at application launch):

chan1.SetSound (alarmUri, alarmAttributes);

这篇关于Xamarin Android使用SetSound for Notification Channel在通知上播放自定义声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆