可以使用 MediaElement 的 Source 属性 来播放内容吗?
Can one use the Source property of a MediaElement to play content?
我正在尝试使用歌曲的目录路径从本地机器播放歌曲。
MediaElement.Source = new Uri(@"D:\Music\Artist\Album\Song.mp3", UriKind.Absolute);
这甚至可以开始工作,或者 Windows 8 个应用仅使用 mss-appx: 等 URI 方案来访问包数据吗?
当我尝试 运行 代码时,我在 MediaElement 控件上收到一条消息 "Invalid Source"
您需要使用文件打开选择器select D 盘中的文件。
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
openPicker.FileTypeFilter.Add(".mp3");
StorageFile file = await openPicker.PickSingleFileAsync();
IRandomAccessStreamWithContentType content = await file.OpenReadAsync();
Debug.WriteLine("Content Type: " + content.ContentType);
player.SetSource(content, content.ContentType);
Windows 商店应用程序没有对文件系统的完全访问权限。他们只能(通过路径)直接访问有限的位置(即他们的安装和应用程序数据文件夹)。
MediaElement 可以从它可以直接访问的路径加载项目,但这通常没有用,因为这些位置具有 URI(ms-appx: 和 ms-appdata:),无论实际位置如何,它们都会定位到正确的位置路径是.
歌曲通常位于音乐库中,MediaElement 无法直接访问该库。它可以通过 MusicLibrary 功能获得代理访问,但这不允许通过路径访问。应用程序需要通过 KnownFolders 对象访问文件:
private async void Button_Click(object sender, RoutedEventArgs e)
{
StorageFolder musicLib = Windows.Storage.KnownFolders.MusicLibrary;
StorageFile song = await musicLib.GetFileAsync(@"Artist\Album\Song.mp3");
var stream = await song.OpenReadAsync();
me.SetSource(stream, stream.ContentType);
}
如果歌曲不在功能允许的库中,则用户将需要通过 FolderPicker 等方式授予权限。用户可以选择音乐位置的根目录,应用程序可以使用 Windows.Storage.AccessCache 类 缓存它,因此用户不需要多次选择文件夹或单独选择文件。
我在我的博客文章中对此进行了更详细的讨论 Skip the path: stick to the StorageFile
This link has the answer (in case you can use the application folder)
MediaElement.Source = new Uri("ms-appx-web:///Assets/Song.mp3", UriKind.Absolute);
我正在尝试使用歌曲的目录路径从本地机器播放歌曲。
MediaElement.Source = new Uri(@"D:\Music\Artist\Album\Song.mp3", UriKind.Absolute);
这甚至可以开始工作,或者 Windows 8 个应用仅使用 mss-appx: 等 URI 方案来访问包数据吗?
当我尝试 运行 代码时,我在 MediaElement 控件上收到一条消息 "Invalid Source"
您需要使用文件打开选择器select D 盘中的文件。
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
openPicker.FileTypeFilter.Add(".mp3");
StorageFile file = await openPicker.PickSingleFileAsync();
IRandomAccessStreamWithContentType content = await file.OpenReadAsync();
Debug.WriteLine("Content Type: " + content.ContentType);
player.SetSource(content, content.ContentType);
Windows 商店应用程序没有对文件系统的完全访问权限。他们只能(通过路径)直接访问有限的位置(即他们的安装和应用程序数据文件夹)。
MediaElement 可以从它可以直接访问的路径加载项目,但这通常没有用,因为这些位置具有 URI(ms-appx: 和 ms-appdata:),无论实际位置如何,它们都会定位到正确的位置路径是.
歌曲通常位于音乐库中,MediaElement 无法直接访问该库。它可以通过 MusicLibrary 功能获得代理访问,但这不允许通过路径访问。应用程序需要通过 KnownFolders 对象访问文件:
private async void Button_Click(object sender, RoutedEventArgs e)
{
StorageFolder musicLib = Windows.Storage.KnownFolders.MusicLibrary;
StorageFile song = await musicLib.GetFileAsync(@"Artist\Album\Song.mp3");
var stream = await song.OpenReadAsync();
me.SetSource(stream, stream.ContentType);
}
如果歌曲不在功能允许的库中,则用户将需要通过 FolderPicker 等方式授予权限。用户可以选择音乐位置的根目录,应用程序可以使用 Windows.Storage.AccessCache 类 缓存它,因此用户不需要多次选择文件夹或单独选择文件。
我在我的博客文章中对此进行了更详细的讨论 Skip the path: stick to the StorageFile
This link has the answer (in case you can use the application folder)
MediaElement.Source = new Uri("ms-appx-web:///Assets/Song.mp3", UriKind.Absolute);