尝试播放视频,我现在正在 Xamarin.Forms XAML 中使用 WebView。
我在 Android 上遇到了很大的问题(这是一个表单项目,它也可以在 iOS 和 WinPhone 上运行),主要是播放本地文件。
为了获得最低限度的失败版本,我发现即使从网络上播放视频也不起作用。哦,它(有点——至少我得到了音频)在模拟器中工作,但在我的设备上不工作,这是三星 Note 1。
我在做的是
var htmlSource = new HtmlWebViewSource();
html = "<!Doctype HTML><html><body><video controls> <source src=\"http://techslides.com/demos/sample-videos/small.webm\" type=\"video/webm\"> <source src=\"http://techslides.com/demos/sample-videos/small.ogv\" type=\"video/ogg\"> <source src=\"http://techslides.com/demos/sample-videos/small.mp4\" type=\"video/mp4\"> <source src=\"http://techslides.com/demos/sample-videos/small.3gp\" type=\"video/3gp\"></video></body></html>";
htmlSource.Html = html;
myWebView.Source = htmlSource;
html直接从http://techslides.com/sample-webm-ogg-and-mp4-video-files-for-html5复制而来(仅格式化和添加“”) .
在我添加的AndroidManifest.xml中
android:hardwareAccelerated="true"
到
<manifest />
(我也试过false。)
我在 Android 设备上看到的:
网站打开,显示视频控件(虽然只有很小的区域,但它有一个占位符背景)。单击播放,我在调试器中收到“handleMessage - PLAY”,但没有任何反应。此外,单击全屏图标,我得到一个“enterFullScreenVideoState called”。在调试器中,但没有任何反应。
附加: 开业http://techslides.com/sample-webm-ogg-and-mp4-video-files-for-html5直接在 Android 浏览器中工作(我没有预览视频,但是当我点击播放时它会加载),但是设置
myWebView.Source = "http://techslides.com/sample-webm-ogg-and-mp4-video-files-for-html5";
也会产生与我显式设置 HtmlWebViewSource 时相同的不良行为。
应用程序具有互联网权限。
出了什么问题,如何解决?
(我知道在 stackoverflow 上有很多关于此的线程,但很多都是 3-5 年前的,没有一个适用于 Xamarin,至少从我目前所见来看是这样。)
最佳答案
如果您想在 native 播放器中播放视频而不是启动 WebView,您可以在 Xamarin 组件商店中查看视频播放器组件。它允许您在 iOS、Android 和 Windows Phone 上呈现 native 视频播放器。下面的代码 fragment 显示了将其放入并使用它的最简单示例。您还可以连接播放、暂停、停止、完成等事件。您可以控制音量、自动播放和重复播放等。
https://github.com/adamfisher/Xamarin.Forms.VideoPlayer
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:o="clr-namespace:Octane.Xam.VideoPlayer;assembly=Octane.Xam.VideoPlayer"
x:Class="VideoPlayerSamples.VideoPlayerBasicExamplePage"
Title="Basic Video Player">
<o:VideoPlayer Source="http://vjs.zencdn.net/v/oceans.mp4" />
</ContentPage>
免责声明:这是我的组件。
关于android - xamarin android webview : video not playing on device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29772914/