c# - 在 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......请告诉我。
解决方案
推荐阅读
- javascript - VueJS获取TypeError:删除并添加传感器信息时无法读取未定义的属性
- ios - SwiftUI .onOpenURL 不在物理设备上调用
- python - DataFrame' 对象没有属性 'str'
- google-cloud-platform - 使用 TIBCO for GCP 进行 OAuth 身份验证或 JWT 身份验证
- dask-distributed - Dask 为 AMD 64 核 3990X 分发客户端设置并为其他进程保留内核
- eclipse - 如何修复,无法在 Eclipse 中按 Ctrl+Alt+Up(Down) 重复行
- r - 快速替换大向量中的值
- image - 使用链接中的预览图像预加载图像以提高性能
- python - 如何在一列pyspark中删除带有空值的行和列
- python - 每个动作都显示:AttributeError:模块'importlib._bootstrap'没有属性'SourceFileLoader'