Mark Wardell
Mark Wardell

Reputation: 565

Playing a standard/system sound on phone xamarin forms

Is there a standard cross platform way of playing a 250 ms or so 'ding' on Xamarin Forms iOS and Android?

Mark Wardell

Upvotes: 8

Views: 4494

Answers (2)

Junior Jiang
Junior Jiang

Reputation: 12723

You can use DependencyService to play default system notification sound in each platform .

Create IPlaySoundService Interface :

public interface IPlaySoundService 
{
    void PlaySystemSound();
}

Implement the PlaySystemSound method in iOS as follow:

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

Implement the PlaySystemSound method in Android as follow :

[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();
        }
    }
}

This is the definition of instance from MainActivity :

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());


        }
...
}

Then it they will play the default notification Sound in each platform . You can modify SystemSoundID in iOS to fit your wants .Here is the Sound ID list .

Upvotes: 12

Mark Wardell
Mark Wardell

Reputation: 565

here is my now compiling yet crashing Droid implementation

using Android.Media; using 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();
        }
    }
} 

Upvotes: 0

Related Questions