diff --git a/src/CommunityToolkit.Maui.MediaElement/Extensions/AppInfoUtils.windows.cs b/src/CommunityToolkit.Maui.MediaElement/Extensions/AppInfoUtils.windows.cs new file mode 100644 index 000000000..ec66b91d3 --- /dev/null +++ b/src/CommunityToolkit.Maui.MediaElement/Extensions/AppInfoUtils.windows.cs @@ -0,0 +1,95 @@ +using System.Reflection; +using Windows.ApplicationModel; + +namespace CommunityToolkit.Maui.Extensions; + +// Since MediaElement can't access .NET MAUI internals we have to copy this code here +// https://github.com/dotnet/maui/blob/main/src/Essentials/src/AppInfo/AppInfo.uwp.cs +static class AppInfoUtils +{ + static readonly Lazy isPackagedAppLazy = new Lazy(() => + { + try + { + if (Package.Current != null) + { + return true; + } + } + catch + { + // no-op + } + + return false; + }); + + /// + /// Gets if this app is a packaged app. + /// + public static bool IsPackagedApp => isPackagedAppLazy.Value; + + static readonly Lazy platformGetFullAppPackageFilePath = new Lazy(() => + { + return IsPackagedApp + ? Package.Current.InstalledLocation.Path + : AppContext.BaseDirectory; + }); + + /// + /// Gets full application path. + /// + public static string PlatformGetFullAppPackageFilePath => platformGetFullAppPackageFilePath.Value; + + /// + /// Converts a object to a object. + /// + /// The to convert. + /// A new object with the version information of this app. + public static Version ToVersion(this PackageVersion version) => + new Version(version.Major, version.Minor, version.Build, version.Revision); + + /// + /// Gets the version information for this app. + /// + /// The assembly to retrieve the version information for. + /// The key that is used to retrieve the version information from the metadata. + /// if is or empty, or no version information could be found with the value of . + public static Version GetAppInfoVersionValue(this Assembly assembly, string name) + { + if (assembly.GetAppInfoValue(name) is string value && !string.IsNullOrEmpty(value)) + { + return Version.Parse(value); + } + + return null!; + } + + /// + /// Gets the app info from this apps' metadata. + /// + /// The assembly to retrieve the app info for. + /// The key of the metadata to be retrieved (e.g. PackageName, PublisherName or Name). + /// The value that corresponds to the given key in . + public static string GetAppInfoValue(this Assembly assembly, string name) => + assembly.GetMetadataAttributeValue("Microsoft.Maui.ApplicationModel.AppInfo." + name); + + /// + /// Gets the value for a given key from the assembly metadata. + /// + /// The assembly to retrieve the information for. + /// The key of the metadata to be retrieved (e.g. PackageName, PublisherName or Name). + /// The value that corresponds to the given key in . + public static string GetMetadataAttributeValue(this Assembly assembly, string key) + { + foreach (var attr in assembly.GetCustomAttributes()) + { + if (attr.Key == key) + { + return attr.Value!; + } + } + + return null!; + } +} \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.MediaElement/Extensions/FileSystemUtils.windows.cs b/src/CommunityToolkit.Maui.MediaElement/Extensions/FileSystemUtils.windows.cs new file mode 100644 index 000000000..d24c4a294 --- /dev/null +++ b/src/CommunityToolkit.Maui.MediaElement/Extensions/FileSystemUtils.windows.cs @@ -0,0 +1,63 @@ +using System.Diagnostics.CodeAnalysis; + +namespace CommunityToolkit.Maui.Extensions; + +// Since MediaElement can't access .NET MAUI internals we have to copy this code here +// https://github.com/dotnet/maui/blob/main/src/Essentials/src/AppInfo/AppInfo.uwp.cs +static class FileSystemUtils +{ + // + // Summary: + // Normalizes the given file path for the current platform. + // + // Parameters: + // filename: + // The file path to normalize. + // + // Returns: + // The normalized version of the file path provided in filename. Forward and backward + // slashes will be replaced by System.IO.Path.DirectorySeparatorChar so that it + // is correct for the current platform. + public static string NormalizePath(string filename) + { + return filename.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar); + } + + public static bool AppPackageFileExists(string filename) + { + string path = PlatformGetFullAppPackageFilePath(filename); + return File.Exists(path); + } + + public static string PlatformGetFullAppPackageFilePath(string filename) + { + if (filename == null) + { + throw new ArgumentNullException("filename"); + } + + filename = NormalizePath(filename); + return Path.Combine(AppInfoUtils.PlatformGetFullAppPackageFilePath, filename); + } + + public static bool TryGetAppPackageFileUri(string filename, [NotNullWhen(true)] out string? uri) + { + string text = PlatformGetFullAppPackageFilePath(filename); + if (File.Exists(text)) + { + if (AppInfoUtils.IsPackagedApp) + { + uri = "ms-appx:///" + filename.Replace('\\', '/'); + } + else + { + uri = "file:///" + text.Replace('\\', '/'); + } + + return true; + } + + uri = null; + return false; + } +} \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.windows.cs b/src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.windows.cs index a62a32b9a..6801b0704 100644 --- a/src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.windows.cs +++ b/src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.windows.cs @@ -1,6 +1,7 @@ using System.Diagnostics; using System.Numerics; using CommunityToolkit.Maui.Core.Primitives; +using CommunityToolkit.Maui.Extensions; using CommunityToolkit.Maui.Views; using Microsoft.Extensions.Logging; using Microsoft.UI.Xaml.Controls; @@ -301,14 +302,21 @@ protected virtual async partial ValueTask PlatformUpdateSource() } else if (MediaElement.Source is ResourceMediaSource resourceMediaSource) { - string path = "ms-appx:///" + resourceMediaSource.Path; + if(string.IsNullOrWhiteSpace(resourceMediaSource.Path)) + { + Logger.LogInformation("ResourceMediaSource Path is null or empty"); + return; + } + + // To test this run app as unpackaged and packaged. + string path = FileSystemUtils.PlatformGetFullAppPackageFilePath(resourceMediaSource.Path); if (!string.IsNullOrWhiteSpace(path)) { Player.Source = WinMediaSource.CreateFromUri(new Uri(path)); } } } - + protected virtual partial void PlatformUpdateShouldLoopPlayback() { if (Player is null)