Skip to content

Commit 580df1b

Browse files
Fix Windows Media Source bug with Packaged Apps (#2609)
* Fix media element in windows where playing resource mediasource does not work when running app as packaged. * Remove Unused Code * `dotnet format` --------- Co-authored-by: Brandon Minnick <[email protected]>
1 parent 7675608 commit 580df1b

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Windows.ApplicationModel;
2+
3+
namespace CommunityToolkit.Maui.Extensions;
4+
5+
// Since MediaElement can't access .NET MAUI internals we have to copy this code here
6+
// https://github.com/dotnet/maui/blob/main/src/Essentials/src/AppInfo/AppInfo.uwp.cs
7+
static class AppPackageService
8+
{
9+
static readonly Lazy<bool> isPackagedAppHolder = new(() =>
10+
{
11+
try
12+
{
13+
if (Package.Current is not null)
14+
{
15+
return true;
16+
}
17+
}
18+
catch
19+
{
20+
// no-op
21+
}
22+
23+
return false;
24+
});
25+
26+
static readonly Lazy<string> fullAppPackageFilePathHolder = new(() =>
27+
{
28+
return IsPackagedApp
29+
? Package.Current.InstalledLocation.Path
30+
: AppContext.BaseDirectory;
31+
});
32+
33+
/// <summary>
34+
/// Gets if this app is a packaged app.
35+
/// </summary>
36+
public static bool IsPackagedApp => isPackagedAppHolder.Value;
37+
38+
39+
/// <summary>
40+
/// Gets full application path.
41+
/// </summary>
42+
public static string FullAppPackageFilePath => fullAppPackageFilePathHolder.Value;
43+
}

src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.windows.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Diagnostics;
22
using System.Numerics;
33
using CommunityToolkit.Maui.Core.Primitives;
4+
using CommunityToolkit.Maui.Extensions;
45
using CommunityToolkit.Maui.Views;
56
using Microsoft.Extensions.Logging;
67
using Microsoft.UI.Xaml.Controls;
@@ -301,7 +302,13 @@ protected virtual async partial ValueTask PlatformUpdateSource()
301302
}
302303
else if (MediaElement.Source is ResourceMediaSource resourceMediaSource)
303304
{
304-
string path = "ms-appx:///" + resourceMediaSource.Path;
305+
if (string.IsNullOrWhiteSpace(resourceMediaSource.Path))
306+
{
307+
Logger.LogInformation("ResourceMediaSource Path is null or empty");
308+
return;
309+
}
310+
311+
string path = GetFullAppPackageFilePath(resourceMediaSource.Path);
305312
if (!string.IsNullOrWhiteSpace(path))
306313
{
307314
Player.Source = WinMediaSource.CreateFromUri(new Uri(path));
@@ -352,6 +359,16 @@ protected virtual void Dispose(bool disposing)
352359
}
353360
}
354361

362+
static string GetFullAppPackageFilePath(in string filename)
363+
{
364+
ArgumentNullException.ThrowIfNull(filename);
365+
366+
var normalizedFilename = NormalizePath(filename);
367+
return Path.Combine(AppPackageService.FullAppPackageFilePath, normalizedFilename);
368+
369+
static string NormalizePath(string filename) => filename.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar);
370+
}
371+
355372
static bool IsZero<TValue>(TValue numericValue) where TValue : INumber<TValue>
356373
{
357374
return TValue.IsZero(numericValue);

0 commit comments

Comments
 (0)