【问题标题】:Playing a standard/system sound on phone xamarin forms在手机 xamarin 表单上播放标准/系统声音
【发布时间】:2020-04-19 05:43:24
【问题描述】:

是否有标准的跨平台方式在 Xamarin Forms iOS 和 Android 上播放 250 毫秒左右的“叮当”?

马克·沃德尔

【问题讨论】:

  • 您好,您可以为每个平台使用 Dependecy Service 来实现。

标签: xamarin xamarin.forms


【解决方案1】:

您可以使用DependencyService在各个平台播放默认的系统通知声音。

创建IPlaySoundService接口:

public interface IPlaySoundService 
{
    void PlaySystemSound();
}

iOS中实现PlaySystemSound方法如下:

[assembly: Xamarin.Forms.Dependency(typeof(PlaySoundService))]
namespace AppCarouselViewSample.iOS
{
    public class PlaySoundService : IPlaySoundService
    {
        public void PlaySystemSound()
        {    
            var sound = new SystemSound(1000);
            sound.PlaySystemSound();
        }
    }
}

Android中实现PlaySystemSound方法如下:

[assembly:Xamarin.Forms.Dependency(typeof(PlaySoundService))]
namespace AppCarouselViewSample.Droid
{
    public class PlaySoundService : IPlaySoundService
    {
        public void PlaySystemSound()
        {
            Android.Net.Uri uri = RingtoneManager.GetDefaultUri(RingtoneType.Ringtone);
            Ringtone rt = RingtoneManager.GetRingtone(MainActivity.instance.ApplicationContext, uri);
            rt.Play();
        }
    }
}

这是 MainActivityinstance 的定义:

namespace xxx.Droid
{
    [Activity(Label = "AppCarouselViewSample", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        public static MainActivity instance { set; get; }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            instance = this;

            Xamarin.Forms.Forms.SetFlags(new string[] { "CarouselView_Experimental", "SwipeView_Experimental", "IndicatorView_Experimental" });
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());


        }
...
}

然后他们将在每个平台播放默认通知声音。您可以在 iOS 中修改 SystemSoundID 以满足您的需求。这里是 the Sound ID list

【讨论】:

  • 谢谢,如果可行,我会测试并标记为已回答
  • @MarkWardell 我还更新了 answer 中的声音 id 列表,您可以检查一下您需要哪一个。
  • 我的 ios 已经编译好了,我的 MainActivity 不太顺利
  • Jiang iOS 太棒了...... Android 没有完全编译......然后我在这里找到创意,它炸毁了。但我可以从这里...
  • 好吧,你可以在MainActivity中创建一个静态参数(instance),然后在PlaySoundService中使用。我已经更新了答案。
【解决方案2】:

这是我现在正在编译但崩溃的 Droid 实现

使用 Android.Media; 使用 TripCalculator.Droid.Services;

[assembly: Xamarin.Forms.Dependency(typeof(PlaySoundService))]
// Crashes
namespace TripCalculator.Droid.Services
{
    public class PlaySoundService : IPlaySoundService
    {
        public void PlaySystemSound()
        {
            var currentContext = Android.App.Application.Context;
            Android.Net.Uri uri = RingtoneManager.GetDefaultUri(RingtoneType.Ringtone);

            Ringtone rt = RingtoneManager.GetRingtone(currentContext.ApplicationContext, uri);
            rt.Play();
        }
    }
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 2012-09-07
    • 2019-09-23
    • 1970-01-01
    • 2012-07-17
    • 2017-10-08
    相关资源
    最近更新 更多