使用Uri表示.wav文件

时间:2014-03-10 13:26:33

标签: c# wpf audio uri

我需要在wpf项目中播放一些.wav文件。没有Uri的经验,所以我被困住了。我已将两个.wav放在我的Rescources文件中,并且我声明:

Uri uri2 = new Uri(@"pack://application:,,,/Resources/WindowsExclamation.wav", UriKind.Absolute);

Uri uri1 = new Uri(@"pack://application:,,,/Resources/WindowsCriticalStop.wav", UriKind.Absolute);
            var player = new MediaPlayer();

我在以下几点上提出了一个断点:

player.Open(uri2);
player.Play();

运行代码时,我得到了uri2 Authority的这个例外:

{"The generic authority 'application:,,,' is not a valid dns name."}

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用更简单的亲戚Uri

Uri uri2 = new Uri(@"../../Resources/WindowsExclamation.wav", UriKind.Relative);

答案 1 :(得分:0)

在WPF中从XAML引用项目资源时,我发现通常最容易使用这种格式来查找相关文件:

"/ReferencedAssembly;component/Subfolder/ResourceFile.type"

这也可以引用并与Uri对象一起使用:

Uri uri = new Uri("/ReferencedAssembly;component/Subfolder/ResourceFile.type", 
    UriKind.Relative);

您还可以将此值公开为string属性,然后将数据绑定到MediaElement.Source属性:

您可以在MSDN上的Pack URIs in WPF页面上找到更多信息。