代码之家  ›  专栏  ›  技术社区  ›  Eng Soon Cheah

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

  •  0
  • Eng Soon Cheah  · 技术社区  · 7 年前

    目前,我开发了Windows 10移动应用程序,可以播放声音/音频功能。 当我编写此语句来播放音频/声音时,当点击图像点击事件时,它将显示错误。 源代码如下:

    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 回复  |  直到 7 年前
        1
  •  1
  •   Nico Zhu    7 年前

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

    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