简体   繁体   English

如何使用 UnoPlatform 在 IOS/Android/WebAssembly 中嵌入 mp3 文件

[英]How to embed mp3 files in IOS/Android/WebAssembly with UnoPlatform

I am attempting to build a UnoPlatform app which has the capability to play some embedded mp3 sound effect files.我正在尝试构建一个能够播放一些嵌入式 mp3 音效文件的 UnoPlatform 应用程序。

Using MediaPlayerElement, it is straight-forward to play media from HTTP sources, however it seems there must be some platform specific hoops to jump through to get embedded resource files to play.使用 MediaPlayerElement,可以直接播放来自 HTTP 源的媒体,但是似乎必须有一些特定于平台的循环才能播放嵌入的资源文件。

The following event handler works for UWP, but not IOS, Android, or WebAssembly:以下事件处理程序适用于 UWP,但不适用于 IOS、Android 或 WebAssembly:

private void SpinButton_OnClick(object sender, RoutedEventArgs e)
{
   player1.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/Sound.mp3"));
}

I have attempted to follow the info here https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/mediaelement for how to embed the media on IOS, but it does not seem to work for me.我试图按照这里的信息https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/mediaelement了解如何在 IOS 上嵌入媒体,但它似乎不适用于我。

Is there a trick to this?这有什么窍门吗? is there a sample out there with this working?有这个工作的样本吗?

The current implementation of the MediaPlayerElement is still a bit limited, but playing back mp3 on Android and iOS should be supported. MediaPlayerElement的当前实现仍有一些限制,但应该支持在 Android 和 iOS 上播放mp3

The trick is to put the .mp3 files in the right location as they are not yet (as compared to image files) automatically copied over to the correct platform-specific location during build.诀窍是将.mp3文件放在正确的位置,因为它们还没有(与图像文件相比)在构建过程中自动复制到正确的平台特定位置。 In case of Android, you need to put the audio files in the Assets folder, for example:如果是 Android,则需要将音频文件放在Assets文件夹中,例如:

Android 资产文件夹

And the Build Action needs to be set to AndroidAsset .并且Build Action需要设置为AndroidAsset

In case of iOS, the files go to the Resources folder and have the Build Action set to Bundle Resource .在 iOS 的情况下,文件转到Resources文件夹并将Build Action设置为Bundle Resource

You can now reference the files as if they were in fact in the "Assets" folder, using the ms-appx: URI scheme:您现在可以使用ms-appx: URI 方案引用文件,就像它们实际上位于“资产”文件夹中一样:

<Grid>
    <MediaPlayerElement AutoPlay="True" Source="ms-appx:///Assets/test.mp3" />
</Grid>

This is just a quick example, but hopefully you can use it as a starting point.这只是一个快速示例,但希望您可以将其用作起点。 Whenever in doubt, it is quite useful to browse the source code of the respective features on GitHub, for example here is the media file loading implementation for Android.如有疑问,在 GitHub 上浏览相应功能的源代码非常有用,例如这里是 Android 的媒体文件加载实现

For WebAssembly, MediaPlayerElement is not supported yet, but if you want to see it supported soon, please vote up on this issue .对于 WebAssembly,目前还不支持MediaPlayerElement ,但如果你想尽快看到它的支持,请在这个问题上投票

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

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