freiberuflicher Software Entwickler C#, vb.Net, vba, UWP, WPF, WinForms, SQL Server, Access, Excel, ASP.Net Core MVC Telefon: 07022/9319004   Kontakt
#

UWP: MP4 Internet Datei anzeigen mit dem MediaPlayerElement

 

Der folgende Code lädt das MP4 Video vom Internet und stellt es auf einem UWP MediaPlayerElement dar.

Der Trick dabei ist, dass die Datei sich im Internet befindet und über einen RandomAccess Stream angebunden wird.

Man kann damit .mp4 Videos über eine LAN Verbindung auf Windows 10 anzeigen

 

Leider kann man damit nicht Video-Stream wie Youtube anzeigen.

 

 

 

C#

 

Der folgende Code lädt das MP4 Video vom Internet und stellt es auf einem UWP MediaPlayerElement dar.

private async Task show_web_Video()

        {

            //----------------< show_web_Video() >---------------

            //< init >

            //YES ok  MP4 File: http://video.ch9.ms/ch9/70cc/83e17e76-8be8-441b-b469-87cf0e6a70cc/ASPNETwithScottHunter_high.mp4

            //NOT ok Youtube: https://www.youtube.com/watch?v=wcgWCdph0Tc

 

            Uri uri = new Uri("http://video.ch9.ms/ch9/70cc/83e17e76-8be8-441b-b469-87cf0e6a70cc/ASPNETwithScottHunter_high.mp4");

            //</ init >

 

 

            HttpClient client = new HttpClient();

 

            // Add custom headers, credentials..

            client.DefaultRequestHeaders.Add("user", "myName");

            //< load HTTP Stream >

            HttpRandomAccessStream stream = await HttpRandomAccessStream.CreateAsync(client, uri);

            //</ load HTTP Stream >

 

            //< load MediaPlayerElement >

            mediaPlayer.AutoPlay = true;

            mediaPlayer.Source = MediaSource.CreateFromStream(stream, stream.ContentType);

            //</ load MediaPlayerElement >

            //----------------</ show_web_Video() >---------------

        }

 

 

XAML Frontend

 

<Page

    x:Class="uwp_MediaPlayer.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:uwp_MediaPlayer"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <Grid.RowDefinitions>

            <RowDefinition Height="57*"/>

            <RowDefinition Height="583*"/>

        </Grid.RowDefinitions>

        <Button Content="Button" HorizontalAlignment="Left"  VerticalAlignment="Top" Click="Button_Click" Grid.Row="0"/>

        <MediaPlayerElement x:Name="mediaPlayer" Grid.Row="1"

         ></MediaPlayerElement>

    </Grid>

</Page>

 

 

 

Zusätzlich wird benötigt:

Quelle: https://github.com/kiewic/MediaPlayerElementWithHttpClient

 

class HttpRandomAccessStream : IRandomAccessStreamWithContentType

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.Linq;

using System.Runtime.InteropServices.WindowsRuntime;

using System.Text;

using System.Threading.Tasks;

using Windows.Foundation;

using Windows.Storage.Streams;

using Windows.Web.Http;

 

namespace uwp_MediaPlayer

{

    class HttpRandomAccessStream : IRandomAccessStreamWithContentType

    {

        private HttpClient client;

        private IInputStream inputStream;

        private ulong size;

        private ulong requestedPosition;

        private string etagHeader;

        private string lastModifiedHeader;

        private Uri requestedUri;

 

        // No public constructor, factory methods instead to handle async tasks.

        private HttpRandomAccessStream(HttpClient client, Uri uri)

        {

            this.client = client;

            requestedUri = uri;

            requestedPosition = 0;

        }

 

        static public IAsyncOperation<HttpRandomAccessStream> CreateAsync(HttpClient client, Uri uri)

        {

            HttpRandomAccessStream randomStream = new HttpRandomAccessStream(client, uri);

 

            return AsyncInfo.Run<HttpRandomAccessStream>(async (cancellationToken) =>

            {

                await randomStream.SendRequesAsync().ConfigureAwait(false);

                return randomStream;

            });

        }

 

        private async Task SendRequesAsync()

        {

            Debug.Assert(inputStream == null);

 

            HttpRequestMessage request = null;

            request = new HttpRequestMessage(HttpMethod.Get, requestedUri);

 

            request.Headers.Add("Range", String.Format("bytes={0}-", requestedPosition));

 

            if (!String.IsNullOrEmpty(etagHeader))

            {

                request.Headers.Add("If-Match", etagHeader);

            }

 

            if (!String.IsNullOrEmpty(lastModifiedHeader))

            {

                request.Headers.Add("If-Unmodified-Since", lastModifiedHeader);

            }

 

            HttpResponseMessage response = await client.SendRequestAsync(

                request,

                HttpCompletionOption.ResponseHeadersRead).AsTask().ConfigureAwait(false);

 

            if (response.Content.Headers.ContentType != null)

            {

                this.ContentType = response.Content.Headers.ContentType.MediaType;

            }

 

            size = response.Content.Headers.ContentLength.Value;

 

            if (response.StatusCode != HttpStatusCode.PartialContent && requestedPosition != 0)

            {

                throw new Exception("HTTP server did not reply with a '206 Partial Content' status.");

            }

 

            if (!response.Headers.ContainsKey("Accept-Ranges"))

            {

                throw new Exception(String.Format(

                    "HTTP server does not support range requests: {0}",

                    "http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.5"));

            }

 

            if (String.IsNullOrEmpty(etagHeader) && response.Headers.ContainsKey("ETag"))

            {

                etagHeader = response.Headers["ETag"];

            }

 

            if (String.IsNullOrEmpty(lastModifiedHeader) && response.Content.Headers.ContainsKey("Last-Modified"))

            {

                lastModifiedHeader = response.Content.Headers["Last-Modified"];

            }

            if (response.Content.Headers.ContainsKey("Content-Type"))

            {

                contentType = response.Content.Headers["Content-Type"];

            }

 

            inputStream = await response.Content.ReadAsInputStreamAsync().AsTask().ConfigureAwait(false);

        }

 

        private string contentType = string.Empty;

 

        public string ContentType

        {

            get { return contentType; }

            private set { contentType = value; }

        }

 

        public bool CanRead

        {

            get

            {

                return true;

            }

        }

 

        public bool CanWrite

        {

            get

            {

                return false;

            }

        }

 

        public IRandomAccessStream CloneStream()

        {

            // If there is only one MediaPlayerElement using the stream, it is safe to return itself.

            return this;

        }

 

        public IInputStream GetInputStreamAt(ulong position)

        {

            throw new NotImplementedException();

        }

 

        public IOutputStream GetOutputStreamAt(ulong position)

        {

            throw new NotImplementedException();

        }

 

        public ulong Position

        {

            get

            {

                return requestedPosition;

            }

        }

 

        public void Seek(ulong position)

        {

            if (requestedPosition != position)

            {

                if (inputStream != null)

                {

                    inputStream.Dispose();

                    inputStream = null;

                }

                Debug.WriteLine("Seek: {0:N0} -> {1:N0}", requestedPosition, position);

                requestedPosition = position;

            }

        }

 

        public ulong Size

        {

            get

            {

                return size;

            }

            set

            {

                throw new NotImplementedException();

            }

        }

 

        public void Dispose()

        {

            if (inputStream != null)

            {

                inputStream.Dispose();

                inputStream = null;

            }

        }

 

        public Windows.Foundation.IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)

        {

            return AsyncInfo.Run<IBuffer, uint>(async (cancellationToken, progress) =>

            {

                progress.Report(0);

 

                try

                {

                    if (inputStream == null)

                    {

                        await SendRequesAsync().ConfigureAwait(false);

                    }

                }

                catch (Exception ex)

                {

                    Debug.WriteLine(ex);

                    throw;

                }

 

                IBuffer result = await inputStream.ReadAsync(buffer, count, options).AsTask(cancellationToken, progress).ConfigureAwait(false);

 

                // Move position forward.

                requestedPosition += result.Length;

                Debug.WriteLine("requestedPosition = {0:N0}", requestedPosition);

 

                return result;

            });

        }

 

        public Windows.Foundation.IAsyncOperation<bool> FlushAsync()

        {

            throw new NotImplementedException();

        }

 

        public Windows.Foundation.IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer)

        {

            throw new NotImplementedException();

        }

    }

}

 

 

 

Mobile
»
UWP: MP4 Internet Datei anzeigen mit dem MediaPlayerElement

.

Jobs, Projekte, Angebote für Freiberufler, Selbstständig an Raimund.Popp@Microsoft-Programmierer.de