【问题标题】:setDataSource failed con Xamarin Forms MediaElementsetDataSource 失败 con Xamarin Forms MediaElement
【发布时间】:2021-08-02 14:52:49
【问题描述】:

我正在尝试使用 Xamarin.CommunityToolkit 中的 MediaElement 播放 youtube/vimeo 视频。好吧:在 Android 上,将 youtube 视频的 url 添加到 MediaElement Source 时,我总是得到错误 setDataSource failed: status = 0x80000000。

Android 权限。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

iOS 权限。

<key>NSAppTransportSecurity</key>
  <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
  </dict>

在 iOS 中,播放界面打开,但我无法播放视频。

我也从 Android 收到此错误

enter image description here

【问题讨论】:

  • 请向我们提供您的代码示例,这将使我们更容易提供帮助
  • 我只用视频的de url设置MediaElement的属性Source。代码没有复杂性

标签: xamarin.forms


【解决方案1】:

我搜索了一些信息并尝试在 MediaElement 中播放 Youtube 视频,但发现无法直接播放。 我找到了另一种方法,您可以安装 YoutubeExplode,将 youtube 视频 url 转换为流。

<ContentPage
x:Class="mediasample.MainPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="clr-namespace:Xamarin.CommunityToolkit.UI.Views;assembly=Xamarin.CommunityToolkit">
<xct:MediaElement
    x:Name="mediasource"
    AutoPlay="True"
    ShowsPlaybackControls="True"
    VerticalOptions="FillAndExpand" />
public MainPage()
    {
        InitializeComponent();
        GetVideoContent();
    }
    private async void GetVideoContent()
    {
        YoutubeClient youtube = new YoutubeClient();
        // You can specify video ID or URL
        YoutubeExplode.Videos.Video video = await youtube.Videos.GetAsync("https://www.youtube.com/watch?v=Cld92SD0jrA");
        string title = video.Title; // "Downloaded Video Title"
        //string author = video.Author; // "Downloaded Video Author"
        TimeSpan duration = (TimeSpan)video.Duration; // "Downloaded Video Duration Count"
        //Now it's time to get stream :
        StreamManifest streamManifest = await youtube.Videos.Streams.GetManifestAsync("https://www.youtube.com/watch?v=Cld92SD0jrA");
        IVideoStreamInfo streamInfo = streamManifest.GetMuxedStreams().GetWithHighestVideoQuality();
        if (streamInfo != null)
        {
            // Get the actual stream
            System.IO.Stream stream = await youtube.Videos.Streams.GetAsync(streamInfo);
            mediasource.Source = streamInfo.Url;
        }
    }

截图:

【讨论】:

  • @iscalo 你试试我的解决方案吗?还有其他问题吗?
  • 对不起,我在度假。我已经尝试过您的解决方案,并且效果很好,但它不能播放 vimeo 视频。显然,YoutubeExplode 仅适用于 youtube 视频。我会继续寻找 vimeo 视频的解决方案。希望早日找到
  • @iscalo 也许你可以使用 Webview 播放 vimeo 视频。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-20
  • 1970-01-01
  • 2016-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多