簡體   English   中英

新的 Xamarin Forms 4.6 MediaElement 不適用於 Android

[英]New Xamarin Forms 4.6 MediaElement not working for Android

I tried out the new Xamarin Forms 4.6.0.726 MediaElement control in a very simple Xamarin Forms Shell Project. 我在 ContentPage 中添加了 MediaElement 控件並設置了它的屬性(AutoPlay 為 true,IsLooping 為 true,Source 為 mp4 文件,ShowPlaybackControls 為 true)。 我還在 App.xaml.cs 中添加了 MediaElement 的 Experimental-Flag。

當我運行應用程序時,在 iOS 上播放視頻,聲音、圖像和播放器控件可見,但在 Android 上不起作用。 在 Android 上,播放器控件不顯示,沒有任何反應。

還有其他人有這個問題嗎?

您可以嘗試檢查這些,確保您已在平台項目中存儲了媒體文件。

在 Android 上,媒體文件必須存儲在名為rawResources子文件夾中。 原始文件夾不能包含子文件夾。 媒體文件必須具有AndroidResourceBuild Action

在此處輸入圖像描述

然后在您的 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" }); 在你的App.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>

我必須根據本文在上述答案之上添加一些內容以使其工作: 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;
            });

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM