对于Windows 8 App,在C#/ Xaml中,我想使用Launcher.LaunchFileAsync在计算机上启动带有媒体播放器的视频。
我有一个来自网址的视频文件,我正在使用此代码:
var uri = new Uri(myUrl,UriKind.RelativeOrAbsolute);
var file = await StorageFile.CreateStreamedFileFromUriAsync("aaa", uri, RandomAccessStreamReference.CreateFromUri(uri));
if (file != null)
{
// Launch the retrieved file
var success = await Windows.System.Launcher.LaunchFileAsync(file);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
这实际上是LaunchFileAsync文档中的示例代码。对我来说似乎很好,但成功总是错误的。 你知道为什么吗? 是否可以在网址中使用LaunchFileAsync作为视频? 还有另一种方法吗?
答案 0 :(得分:1)
我遇到了同样的问题,我试图从特定的文件路径位置播放.mp3文件。
我无法使用 StorageFile.CreateStreamedFileFromUriAsync 打开该文件。我收到了无效的争论异常或未经授权的访问错误。
尝试替换更改行:
String myUrl = @"ms-appx:///sources/sounds/ffd19.mp3";
var file = await StorageFile.GetFileFromApplicationUriAsync(uri);
唯一的缺点是文件必须作为项目的资源进行注册。
在我的情况下,.mp3位于解决方案资源管理器中的sources / sounds /文件夹中,与应用程序文件位于同一文件夹中。
答案 1 :(得分:0)
我对您的代码所说的内容感到困惑。
var file = await StorageFile.CreateStreamedFileFromUriAsync("aaa", uri, RandomAccessStreamReference.CreateFromUri(uri));
“file”获取函数的返回值,但函数返回的是StorageFile的一个对象,可以引用Window.Storage.StorageFile。
我认为这可能是问题,但我不知道该怎么做。
此外,函数“LaunchFileAsync()”对它可以启动的文件类型有限制,这也可能导致此问题。
对不起我的无答案建议。就是这样。