UWP-系统异常:指定的路径无效

时间:2017-08-14 12:30:51

标签: uwp xamarin.uwp

目前我开发了可以播放声音/音频功能的Windows 10移动应用程序。 当我写这个语句来播放音频/声音时,它会在Tap Tapped事件被点击时显示错误。 源代码如下:

MediaElement mysong = new MediaElement();

        try
        {
            var folder = await StorageFolder.GetFolderFromPathAsync(@"ms-appx://Assets/Media/");
            if (folder != null)
            {
                var file = await folder.GetFileAsync("police_alarm.mp3");
                if (file != null)
                {
                    var stream = await file.OpenReadAsync();
                    mysong.SetSource(stream, file.ContentType);
                    mysong.Volume = 100;
                    mysong.Play();
                }
            }
            else
            {
                MessageDialog dialog = new MessageDialog("Siren can't play !!! Please keep yourself safe !!!", "Error");
                await dialog.ShowAsync();
            }
        }
        catch(Exception ex)
        {
            MessageDialog dialog = new MessageDialog(ex.ToString(), "Error");
            await dialog.ShowAsync();
        }

错误 System Exception : The Specified path is invalid

是针对此的任何解决方案吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

问题是您使用了错误的文件夹路径。

var folder = await StorageFolder.GetFolderFromPathAsync(@"ms-appx://Assets/Media/");

请使用以下代码替换文件夹路径。

string root = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
string path = root + @"\Assets\Media";
var folder = await StorageFolder.GetFolderFromPathAsync(path);

有关详情,请参阅File access permissions