【问题标题】:How to "click" a button with sound in Xamarin如何在 Xamarin 中“单击”带有声音的按钮
【发布时间】:2022-01-05 17:30:52
【问题描述】:

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

<!--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 但它不识别那块地毯,有什么想法吗?或者我做错了什么。提前感谢您的回答

【问题讨论】:

  • 你的按钮指向boton_perro_Clicked而不是boton_oveja_Clicked,我想我会提到,你可能错误地发布了错误的按钮标记..
  • SonidosApp.Sonidos.sonidogato.mp3需要用引号引起来,因为是字符串路径,即"SonidosApp.Sonidos.sonidogato.mp3"
  • 是的!非常感谢您的评论,问题是引号,非常感谢,
  • 你让它工作真是太好了!如果您在下面添加“您的答案”,那么每个人都会很快看到您不再需要帮助 :) 如果将来其他人也有类似的问题,他们可以投票支持该答案,这样它就会更加引人注目。

标签: c# xamarin xamarin.forms audio-player


【解决方案1】:

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 通过Dependency Service

public interface ISound
{
    void KeyboardClick () ;
}

然后实现平台特定的功能。

iOS:

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

安卓:

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

this post by SushiHangover的回答

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多