android - 新的 Xamarin Forms 4.6 MediaElement 不适用于 Android

标签 android xamarin.forms mediaelement xamarin-forms-4

我在一个非常简单的 Xamarin Forms Shell 项目中试用了新的 Xamarin Forms 4.6.0.726 MediaElement 控件。
我在 ContentPage 中添加了 MediaElement 控件并设置了它的属性(AutoPlay 为 true,IsLooping 为 true,Source 为 mp4 文件,ShowPlaybackControls 为 true)。
我还在 App.xaml.cs 中为 MediaElement 添加了 Experimental-Flag。

当我运行应用程序时,视频在 iOS 上播放,声音、图像和播放器控件可见,但在 Android 上不起作用。
在 Android 上,播放器控件不显示,没有任何 react 。

还有其他人有这个问题吗?

最佳答案

您可以尝试检查这些,确保您已在平台项目中存储了媒体文件。

在 Android 上,媒体文件必须存储在 的子文件夹中资源 命名为 生的 . 生的 文件夹不能包含子文件夹。媒体文件必须有 Build ActionAndroidResource .

enter image description here

然后在您的 page.xaml 中(不要使用布局来包装 MediaElement ):

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="MediaElementDemos.PlayAppPackageVideoResourcePage"
         Title="Play app package video resource">
    <MediaElement Source="ms-appx:///XamarinForms101UsingEmbeddedImages.mp4"
              ShowsPlaybackControls="True" IsLooping="True" AutoPlay="True" />
</ContentPage>

添加 Device.SetFlags(new string[] { "MediaElement_Experimental" });在您的应用程序.xaml.cs
public App()
    {
        Device.SetFlags(new string[] { "MediaElement_Experimental" });
        InitializeComponent();
        MainPage = new NavigationPage(new PlayPage());
    }

更新 :

如果您想从 URL 播放 mp4。
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
     x:Class="MediaElementDemos.PlayAppPackageVideoResourcePage"
     Title="Play app package video resource">
    <MediaElement Source="https://sec.ch9.ms/ch9/5d93/a1eab4bf-3288-4faf-81c4-294402a85d93/XamarinShow_mid.mp4"
          ShowsPlaybackControls="True" IsLooping="True" AutoPlay="True" />
</ContentPage>

关于android - 新的 Xamarin Forms 4.6 MediaElement 不适用于 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61727692/

相关文章:

java - 如何在 Android 11 和 12 中获取 PDF 文件路径

android - Gradle 无法解析 Android Studio 中的库

java - 为什么 Android Firebase 会跳过身份验证 Activity

android - Xamarin.Forms Android AOT 支持

android - Xamarin.Forms.Labs Mediapicker 卡在 Android 上尝试使用异步方法从照片库中提取图像

C# Xamarin Forms MessagingCenter 不更新项目通过

c# - 在MediaElement中播放音频时显示一些内容

java - 加速度计错误报告

wpf - 24/7计算机,中介元素WPF出现问题,在3小时后变黑

c# - 多个 MediaElement 停止工作 wp8