אפרת
אפרת

Reputation: 19

How display MediaPlayerElement from stream in WinUI 3 Net 8

I have Stream and I want create MediaPlayerElement of WinUI 3 from this stream. this my code:

var mediaPlayer = new MediaPlayer()  {
     AutoPlay = x.AutoPlay,
     Source = MediaSource.CreateFromStream(x.Stream.AsRandomAccessStream(), x.ContentType),

 };

MediaPlayerElement.SetMediaPlayer(mediaPlayer);

but I get exception:

System.NotSupportedException
  HResult=0x80004001
  Message=This IRandomAccessStream does not support the CloneStream method because it requires cloning and this stream does not support cloning.
  Source=Microsoft.Windows.SDK.NET
  StackTrace:
   at System.IO.NetFxToWinRtStreamAdapter.ThrowCloningNotSupported(String methodName)

I try copy to MemoryStream but I get this exception. What can I do?

Upvotes: 0

Views: 157

Answers (1)

Jeaninez - MSFT
Jeaninez - MSFT

Reputation: 4040

According to the Doc:How to: Convert between .NET Framework and Windows Runtime streams

.NET Framework streams don't support cloning, even after conversion. If you convert a .NET Framework stream to a Windows Runtime stream and call GetInputStreamAt or GetOutputStreamAt, which call CloneStream, or if you call CloneStream directly, an exception occurs.

You need to convert a System.IO.Stream to a Windows.Storage.Streams.IRandomAccessStream. You could refer to the thread: https://stackoverflow.com/a/48152204

Upvotes: 1

Related Questions