如何使用 ypoutube 或 xamarin 表单中的任何其他 url 播放视频

问题描述 投票:0回答:1

我想播放来自 youtube 链接或其他视频网址的视频。但视频无法播放

<xct:MediaElement x:Name="mediaElement"Grid.Row="0"   HeightRequest="300"
ShowsPlaybackControls="True" Source="https://www.youtube.com/watch?v=FPeGkedZykA"/>
xamarin
1个回答
1
投票

需要明确的是,youtube url 链接是一个基于网络的视频播放器,而不是直接的视频链接。您可以通过 2 方式在 Xamarin 表单中播放 youtube url。

1。使用 YoutubeExplode 插件。

    <xct:MediaElement x:Name="MyMediaElement" WidthRequest="200" 
     HeightRequest="200"  ShowsPlaybackControls="True" />

您页面中的.cs

  public MainPage()
    {
        InitializeComponent();
        GetVideoContent();
    }

    private async void GetVideoContent()
    {
        var youtube = new YoutubeClient();
        // You can specify video ID or URL
        var video = await youtube.Videos.GetAsync("https://www.youtube.com/watch?v=FPeGkedZykA");
        var title = video.Title; // "Downloaded Video Title"
        var author = video.Author; // "Downloaded Video Author"
        var duration = video.Duration; // "Downloaded Video Duration Count"

        //Now it's time to get stream :
        var streamManifest = await youtube.Videos.Streams.GetManifestAsync("https://www.youtube.com/watch?v=FPeGkedZykA");

        var streamInfo = streamManifest.GetMuxedStreams().GetWithHighestVideoQuality();

        if (streamInfo != null)
        {
            // Get the actual stream
            var stream = await youtube.Videos.Streams.GetAsync(streamInfo);
            MyMediaElement.Source = streamInfo.Url;
        }
    }

2.使用webview

<WebView x:Name="myurl"  VerticalOptions="FillAndExpand" HeightRequest="600"></WebView>

然后在page.cs中设置url。

 myurl.Source = "https://www.youtube.com/watch?v=FPeGkedZykA";

© www.soinside.com 2019 - 2024. All rights reserved.