首页 > 解决方案 > 在 Xamarin 中播放声音

问题描述

因此,我正在使用 Xamarin 表单在 Visual Studio 中开发适用于 android 和 iOS 的应用程序,我需要一个按钮来播放声音,但我有一个问题,当我按下按钮时,我得到:“system.argumentnullexception 已被抛出”和我没有“流”值,但我真的看不出哪里丢失了。这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace PrototypeII
{
    public partial class MainPage : ContentPage
    {
        Button panic;
        Button settings;
        Button relax;
        public MainPage()
        {
            InitializeComponent();

            BackgroundColor = Color.LightGray;

            panic = new Button
            {
                Margin = new Thickness(10),
                FontSize = 30,
                BackgroundColor = Color.Red,
                Text = "Panic",
                TextColor = Color.Black,
            };

            panic.Clicked += panic_clicked;

            relax = new Button
            {
                Margin = new Thickness(10),
                FontSize = 30,
                BackgroundColor = Color.GreenYellow,
                Text = "Tchill",
                TextColor = Color.Black,
            };

            settings = new Button
            {
                Margin = new Thickness(10),
                FontSize = 20,
                BackgroundColor = Color.GreenYellow,
                Text = "..."
            };

            var Grid = new Grid
            {
                Margin = new Thickness(20, 40),

                ColumnDefinitions =
                {
                    new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                    new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                    new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                },

                RowDefinitions =
                {
                    new RowDefinition { Height = new GridLength(0.5, GridUnitType.Star)},
                    new RowDefinition { Height = new GridLength(1, GridUnitType.Star)},
                    new RowDefinition { Height = new GridLength(1, GridUnitType.Star)},
                    new RowDefinition { Height = new GridLength(1, GridUnitType.Star)},
                    new RowDefinition { Height = new GridLength(1, GridUnitType.Star)},
                }



            };

            Grid.Children.Add(settings, 3, 0);

            Grid.Children.Add(panic, 0, 1);
            Grid.SetColumnSpan(panic, 2);

            Grid.Children.Add(relax, 2, 2);
            Grid.SetColumnSpan(relax, 2);

            Content = Grid;
        }

        private void panic_clicked(object sender, EventArgs e)
        {
            var assembly = typeof(App).GetTypeInfo().Assembly;

            Stream audioStream = assembly.GetManifestResourceStream("PrototypeII.123.mp3");

            var audio = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;

            audio.Load(audioStream);

            audio.Play();
        }
    }
}

如果你能告诉我如何解决这个问题或者......更简单的方法idk......请告诉我。

标签: c#androidiosxamarinaudio

解决方案


推荐阅读