使用 CrossSimpleAudioPlayer 播放声音 - Xamarin Forms

Playing sound with CrossSimpleAudioPlayer - Xamarin Forms

最近我一直在尝试在 Xamarin Forms 中使用 CrossSimpleAudioPlayer 来播放 .mp3 文件,但我收到了这条消息

System.NullReferenceException: Object reference not set to an instance of an object.

我将 属性 Build Action 更改为 Embedded Resource,但我的错误仍然存​​在,是我遗漏了什么吗?

这是我的参考资料 https://blog.xamarin.com/adding-sound-xamarin-forms-app/?utm_medium=social&utm_campaign=blog&utm_source=linkedin&utm_content=simpleaudioplayer-nuget 这是我的代码:

public void Play() {
  var assembly = typeof(App).GetTypeInfo().Assembly;
  Stream audioStream = assembly.GetManifestResourceStream("softAlarm." + "softAlarm.mp3");
  var audio = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
  audio.Load(audioStream);

  audio.Play();
}

您的 softAlarm.mp3 文件位于何处?您需要输入以点连接的完整路径,并以您的项目名称为前缀。

因此,如果您的文件位于 Resources 文件夹下名为 SoftAlarm 的项目中,您应该声明:GetManifestResourceStream("SoftAlarm.Resources.softAlarm.mp3");

声音文件应该在您的共享项目中并且具有构建操作Embedded Resource

我在这里为您创建了一个工作示例项目:https://github.com/jfversluis/CrossSimpleAudioPlayerSample