如何通过MediaElement从Library播放文件? [英] How to play file from Library by MediaElement?

查看:41
本文介绍了如何通过MediaElement从Library播放文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只能从应用程序存储中播放文件,但是,我必须从库"或其他来源播放文件.我尝试:

I can play files only from application storage, but, I have to play file from Library or another source. The I try to:

        var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("C:\\Users\\admin\\Music\\line.mp3"));
        var stream = await storageFile.OpenAsync(FileAccessMode.Read);

        mediaElement.SetSource(stream, storageFile.ContentType);

        mediaElement.Play();

有例外:"Polar.exe中发生了'System.ArgumentException'类型的异常,但未在用户代码中处理.附加信息:值不在预期范围内."

It's gives exception: "An exception of type 'System.ArgumentException' occurred in Polar.exe but was not handled in user code. Additional information: Value does not fall within the expected range."

我尝试使用mediaElement.Source(),但是element没有播放声音.也不例外,没有任何事情.

I tried mediaElement.Source() to, but element is not playing the sound. No exception, no anything.

我认为这是一个愚蠢的问题,但是我找不到解决方案.我在做什么错了?

I think it's foolish problem, but I can't find solution. What I am doing wrong?

推荐答案

如果要播放库中的音频文件,则需要使用 KnownFolders . GetFileFromApplicationUriAsync 无法正常工作.

If you want to play audio files from library, then you need to use KnownFolders. GetFileFromApplicationUriAsync won't work.

var storageFile = await KnownFolders.MusicLibrary.GetFileAsync("line.mp3");
var stream = await storageFile.OpenAsync(FileAccessMode.Read);
mediaElement.SetSource(stream, storageFile.ContentType);
mediaElement.Play();

如果要枚举所有媒体文件或所有文件夹,请使用它.

If you want enumerate all the media file or all the folders then use this.

var AllStorageFiles = await KnownFolders.MusicLibrary.GetFilesAsync();
var AllStorageFolders = await KnownFolders.MusicLibrary.GetFoldersAsync();

这篇关于如何通过MediaElement从Library播放文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆