Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play default button's sound on Xamarin.Android?

I'm making an app with using Xamarin.forms. You might know forms' button is not enough to use as image button if you tried one.

So I use Image as a button and add gesturerecogniger. It's working fine. Good thing is that I can use all Image's bindable property same like using Image. (like 'Aspect property' and else)

Only problem is that Android button has sound effect when it's pressed. Mine doesn't have.

How to play default button sound on Android?

[another try]

I tried to make layout and put Image and empty dummy button on it. But If I do this, I can't use any property of Image or Button unless I manually link it.

So I think it's not the right way.

Thanks.

like image 364
Bright Lee Avatar asked Jul 05 '16 02:07

Bright Lee


People also ask

How does Xamarin run on Android?

Xamarin. Android applications run within the Mono execution environment. This execution environment runs side-by-side with the Android Run Time(ART) virtual machine. Both runtime environments run on top of the Linux kernel and expose APIs to the code that allows access to the underlying system.

Is Xamarin native Android?

Generally, the main distinction between the two platforms is that Xamarin. Forms allows reusing the same UI code for multiple OS, whereas Xamarin NativeNativeIn computing, native software or data-formats are those that were designed to run on a particular operating system. In a more technical sense, native code is code written specifically for a certain processor. In contrast, cross-platform software can be run on multiple operating systems and/or computer architectures.https://en.wikipedia.org › wiki › Native_(computing)Native (computing) - Wikipedia is adapted to APIs, specific to a specific platform – Windows, iOS, Android.


1 Answers

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);
    }
}
like image 200
John Deer Avatar answered Sep 27 '22 22:09

John Deer