Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 832
Mark Wardell Avatar asked Apr 19 '20 05:04

Mark Wardell


People also ask

Is Xamarin being discontinued?

With the sunsetting of Xamarin. Essentials, it will continue to be serviced through November 2022, but new features and APIs will only be added to .

Is Xamarin slower than native?

Though both Xamarin and React Native offer near-native performance, Xamarin runs the fastest code on Android and iOS and has a user interface (UI) for using native tools. TLDR: In Xamarin vs. React Native, Xamarin has more brownie points for native-like performance.

Does Xamarin support mobile applications?

Xamarin extends the . NET developer platform with tools and libraries specifically for building apps for Android, iOS, tvOS, watchOS, macOS, and Windows.


1 Answers

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 .

like image 132
Junior Jiang Avatar answered Oct 18 '22 00:10

Junior Jiang