繁体   English   中英

如何在 Xamarin 中“单击”带有声音的按钮

[英]How to "click" a button with sound in Xamarin

我正在开发一个应用程序,当您按下按钮时,它听起来就像您在按钮中拥有的图像。 我正在使用 NuGet SimpleAudioPlayer但是当我尝试导入声音时 NuGet 无法识别 MP3 档案,我不知道该怎么做是我的 Z8CBFD56579569C46DDED1B21D83F1 代码:

<!--Perro-->

                   <ImageButton Grid.Row="1"
                           Grid.Column="0"
                           Source="perro.png"
                           WidthRequest="100"
                                x:Name="boton_perro"
                                Clicked="boton_perro_Clicked"
                           BackgroundColor="White"
                           HeightRequest="100"
                           CornerRadius="70"/>

这是我的 C# 代码:

void boton_oveja_Clicked(System.Object sender, System.EventArgs e)
        {
            Player(SonidosApp.Sonidos.sonidogato.mp3);
        }


        //Metodo de reproductor.
        public void Player( string sonido)
        {
            var assembly = typeof(App).GetTypeInfo().Assembly;
            Stream audioStream = assembly.GetManifestResourceStream(sonido);
            var audio = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
            audio.Load(audioStream);
            audio.Play();
        }

但是,当我尝试运行我的代码时,它会出现此错误: Error CS0234: The type or namespace name 'Sonidos' does not exist in the namespace 'SonidosApp' (are you missing an assembly reference?) (CS0234) (SonidosApp)路线mp3 的声音是:SonidosApp.Sonidos 但它不认识那块地毯,有什么想法吗? 或者我做错了什么。 提前感谢您的回答

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通过依赖服务

public interface ISound
{
    void KeyboardClick () ;
}

然后实现平台特定的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);
}

来自SushiHangover 的这篇文章的回答

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM