dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.04k stars 1.17k forks source link

MediaPlayer cant play the uri startswith `pack://...` #6185

Closed emako closed 2 years ago

emako commented 2 years ago

MediaPlayer cant play the uri startswith pack://.... such as

// play failed
string uriString1 = $"pack://application:,,,/Lyre.Player.Plugin;component/Resources/FloralZither.mp3";
MediaPlayer player1 = new();
player1.Open(new Uri(uriString1));
player1.Play();

// play ok
string uriString2 = @$"D:\GitHub\Snap.Genshin\Plugins\SG.Plugin.Lyre.Player\src\Resources\FloralZither.mp3";
MediaPlayer player2 = new();
player2.Open(new Uri(uriString2));
player2.Play();

Actual behavior: UriSchemePack type uri cant be played using MediaPlayer.

Expected behavior:

Minimal repro:

lindexi commented 2 years ago

I wrote a demo to reproduce this problem: https://github.com/lindexi/lindexi_gd/tree/af15cde4b0628c2744a8b8eac14a791e2d4f7b54/KufayunurharnaLuragaruker

ThomasGoulet73 commented 2 years ago

MediaPlayer is a wrapper around the Windows Media Player which, to my knowledge, only supports path and URLs. You could extract the file from the assembly, save it to a file, play the file and then delete the file. This is not ideal but for small files (Which I suppose is your case since it doesn't really make sense to embed large file inside the assembly), it should be fast enough.

lindexi commented 2 years ago

@ThomasGoulet73 How about throw an exception when input the pack:// uri?

emako commented 2 years ago

MediaPlayer is a wrapper around the Windows Media Player which, to my knowledge, only supports path and URLs. You could extract the file from the assembly, save it to a file, play the file and then delete the file. This is not ideal but for small files (Which I suppose is your case since it doesn't really make sense to embed large file inside the assembly), it should be fast enough.

Does MediaPlayer intend to append the API that passes the Stream parameter for opening file?

lindexi commented 2 years ago

图片

Good catch. It will raise the MediaFailed event.

ThomasGoulet73 commented 2 years ago

You were faster than me 😄. Here's the code that throws when using pack for reference: https://github.com/dotnet/wpf/blob/4af440108d50958a2ac3e3f67a454aa0cd142e5c/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaPlayerState.cs#L844-L855

Does MediaPlayer intend to append the API that passes the Stream parameter for opening file? This repo is probably not the right place to ask this question since Windows Media Player is probably maintained by the Windows team. I would recommend using third-party solutions which might support using a Stream (VLC based player might support it).

emako commented 2 years ago

Try VLC or extract the file from the assembly to fix problem.