我在一个非常简单的Xamarin Forms Shell项目中试用了新的Xamarin Forms 4.6.0.726 MediaElement控件。我在ContentPage中添加了MediaElement控件,并设置了其属性(true时为AutoPlay,true时为IsLooping,mp4文件的Source,true的ShowPlaybackControls)。我还在App.xaml.cs中为MediaElement添加了实验标志。
[当我运行该应用程序时,在iOS上播放视频,同时显示声音,图像和播放器控件,但在Android上不起作用。在Android上,播放器控件未显示,没有任何反应。
其他人有这个问题吗?
您可以尝试检查这些,确保已在平台项目中存储了媒体文件。
在Android上,媒体文件必须存储在名为raw的资源的子文件夹中。 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>
将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>