How to play default button sound on Xamarin.Android?

I am making an application using Xamarin.forms. You may know that the β€œForms” button is not enough to use an image as a button if you tried it.

So, I use Image as a button and add gesturerecogniger. It works great. It's good that I can use the entire Image bindable property in the same way as with Image. (for example, the Aspect property, etc.)

The only problem is that the Android button has a sound effect when pressed. Mine is not.

How to play default button sound on Android?

[another attempt]

I tried to make a layout and put Image and an empty blank button on it. But if I do this, I cannot use any Image or Button property unless I manually bind it.

So, I think this is not the case.

Thanks.

+9
source share
3 answers

Xamarin.Android :

 var root = FindViewById<View>(Android.Resource.Id.Content); root.PlaySoundEffect(SoundEffects.Click); 

Android playSoundEffect (int soundConstant)

Xamarin.iOS

 UIDevice.CurrentDevice.PlayInputClick(); 

Xamarin.Forms via the Dependency Service :

 public interface ISound { void KeyboardClick () ; } 

And then implement the platform-specific function.

IOS:

 public void KeyboardClick() { UIDevice.CurrentDevice.PlayInputClick(); } 

Android:

 public View root; public void KeyboardClick() { if (root == null) { root = FindViewById<View>(Android.Resource.Id.Content); } root.PlaySoundEffect(SoundEffects.Click); } 
+11
source

Take a look at the following:

MonoTouch.UIKit.IUIInputViewAudioFeedback

An interface that, together with the UIInputViewAudioFeedback_Extensions class , contains the UIInputViewAudioFeedback protocol. See Also: IUIInputViewAudioFeedback

https://developer.xamarin.com/api/type/MonoTouch.UIKit.IUIInputViewAudioFeedback/

You will need something like this (untested):

 public void SomeButtonFunction() { SomeBtn.TouchUpInside += (s, e) => { UIDevice.CurrentDevice.PlayInputClick(); }; } 
0
source

Xamarin Forms :

PCL Interface:

 interface ISoundService { void Click(); } 

Click handler:

 void Handle_OnClick(object sender, EventArgs e) { DependencyService.Get<ISoundService>().Click(); } 

Android:

 public class MainActivity { static MainActivity Instance { get; private set; } OnCreate() { Instance = this; } } class SoundService : ISoundService { public void Click() { var activity = MainActivity.Instance; var view = activity.FindViewById<View>( Android.Resource.Id.Content); view.PlaySoundEffect(SoundEffects.Click); } } 
0
source

All Articles