【问题标题】:New Xamarin Forms 4.6 MediaElement not working for Android新的 Xamarin Forms 4.6 MediaElement 不适用于 Android
【发布时间】:2020-05-11 10:37:33
【问题描述】:

我在一个非常简单的 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 上,播放器控件不显示,没有任何反应。

其他人有这个问题吗?

【问题讨论】:

  • 确保你已经在 Android 项目中添加了权限。
  • @LucasZhang-MSFT - 你的意思是 MainActivity 中的 SetFlag MediaElement_Experimental 吗?我已经试过了,没有任何区别。如果您还有其他意思,请提供更多详细信息。

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


【解决方案1】:

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

在 Android 上,媒体文件必须存储在名为 rawResources 子文件夹中。 raw 文件夹不能包含子文件夹。媒体文件的Build Action 必须是AndroidResource

然后在您的 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>

在您的 App.xaml.cs

中添加 Device.SetFlags(new string[] { "MediaElement_Experimental" });
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>

【讨论】:

  • 我没有在应用程序中嵌入我的 mp4,而是从 URL 访问文件。我已经在 App.xaml.cs 中设置了 Experimental 标志。
  • @DanielSimon 我更改了上面的更新,如果你想从 URL 播放文件,你只需将 Source 更改为你的 url。
  • 您的解决方案适用于您的 mp4 文件,尽管文件的播放开始得很晚。但它不适用于我的 mp4 文件。我等了几分钟,但没有任何反应。该文件没有损坏,在 iOS 中可以正常运行,在 iOS 下播放开始也快很多。
  • @DanielSimon 您可以尝试使用我示例中的 url 来查看它是否有效。
  • 请检查这个应用程序 -->github.com/devcrux/Xamarin.Forms-Shapes ,我已经完成了提供的所有说明。它在 android 9 中可以正常工作,但不能在 android 10 上工作。
【解决方案2】:

我必须根据这篇文章在上述答案之上添加一些内容才能使其正常工作:https://github.com/xamarin/Xamarin.Forms/issues/9785

Device.StartTimer(TimeSpan.FromSeconds(1), () =>
            {
                videoPlayer.Play();

                videoPlayer.ScaleTo(0.99f);
                videoPlayer.ScaleTo(1.00f);

                return false;
            });

确保在添加之前添加上述答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多