问题描述
这是我的代码,我来自MSDN论坛(https://social.msdn.microsoft.com/Forums/ie/en-US/ddb1b7f1-e988-40c7-8e1e-eaf6d8573ec2/uwp-how-to-play-sound-from-wav-fileresource?forum=wpdevelop)的示例。
private dispatcherTimer timer;
private TimeSpan myTime = new TimeSpan(0,60);
public MainPage()
{
this.InitializeComponent();
timer = new dispatcherTimer();
timer.Interval = new TimeSpan(0,1);
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(Object sender,object e)
{
if (myTime.Seconds > 0)
{
myTime -= new TimeSpan(0,1);
MainTextBlock.Text = myTime.ToString();
}
else
{
timer.Stop();
MainTextBlock.Text = "Finished";
PlaySound_Async();
}
}
private async void PlaySound_Async()
{
MediaElement timesup = new MediaElement();
Windows.Storage.StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
Windows.Storage.StorageFile file = await folder.GetFileAsync("timesup.mp3");
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
timesup.SetSource(stream,file.ContentType);
timesup.Play();
}
这是一个例外,我得到了这个例外 我已经在正确的文件夹中添加了文件
解决方法
仅将文件添加到文件资源管理器中的Assets
文件夹中并不是将文件导入到项目中的完整步骤。您还需要将文件添加到 Visual Studio 的 Solution Explorer 中的Assets
中。您可以按照以下步骤导入文件:
- 在 Solution Explorer 中单击
Show All Files
选项,然后在timesup.mp3
文件夹中找到Assets
文件。
- 右键单击
timesup.mp3
文件,然后选择Include In Project
选项。
- 点击
timesup.mp3
文件,并确保在属性窗口中将Build Action
设置为Content
。
除了使用上述步骤将文件导入到Assets
外,您还可以右键点击Assets
并选择 Add>现有项…,然后选择目标文件,然后单击Add
按钮以导入文件。
此外,如果您想知道如何查看文件夹变量的输出,请参阅document。
,如果在GetFolderAsync上设置断点,则应该能够查看文件夹变量,并查看其认为Path的位置...是否与您期望的路径相同(C:\ Users。 ... repos \ Multi-timer)