为什么此代码片段不起作用? (Uwp app content 播放一个媒体内容)
Why is this code snippet not working? (Uwp app content to play a media content)
这是我的代码,我从 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, 0, 60);
public MainPage()
{
this.InitializeComponent();
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(Object sender, object e)
{
if (myTime.Seconds > 0)
{
myTime -= new TimeSpan(0, 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();
}
这是异常 我得到了这个异常
我已经在正确的文件夹中添加了文件
如果您在 GetFolderAsync 上设置断点,您应该能够查看文件夹变量并查看它认为路径所在的位置...它是否与您期望的绝对路径相同(C:\用户....repos\Multi-timer)
仅将文件添加到 文件资源管理器 中的 Assets
文件夹并不是将文件导入项目的完整步骤。您还需要将文件添加到 Visual Studio 的 Solution Explorer 中的 Assets
。您可以检查以下步骤来导入文件:
- 单击解决方案资源管理器中的
Show All Files
选项,然后在Assets
文件夹中找到timesup.mp3
文件。
- 右键单击
timesup.mp3
文件和 select Include In Project
选项。
- 单击
timesup.mp3
文件并确保 Build Action
在 Properties window 中设置为 Content
。
除了使用上述步骤将文件导入Assets
外,还可以right-clickAssets
和selectAdd > Existing item…, select目标文件,点击Add
按钮导入文件。
此外,如果您想知道如何查看文件夹变量的输出,请参阅document。
这是我的代码,我从 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, 0, 60);
public MainPage()
{
this.InitializeComponent();
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(Object sender, object e)
{
if (myTime.Seconds > 0)
{
myTime -= new TimeSpan(0, 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();
}
这是异常 我得到了这个异常
我已经在正确的文件夹中添加了文件
如果您在 GetFolderAsync 上设置断点,您应该能够查看文件夹变量并查看它认为路径所在的位置...它是否与您期望的绝对路径相同(C:\用户....repos\Multi-timer)
仅将文件添加到 文件资源管理器 中的 Assets
文件夹并不是将文件导入项目的完整步骤。您还需要将文件添加到 Visual Studio 的 Solution Explorer 中的 Assets
。您可以检查以下步骤来导入文件:
- 单击解决方案资源管理器中的
Show All Files
选项,然后在Assets
文件夹中找到timesup.mp3
文件。
- 右键单击
timesup.mp3
文件和 selectInclude In Project
选项。
- 单击
timesup.mp3
文件并确保Build Action
在 Properties window 中设置为Content
。
除了使用上述步骤将文件导入Assets
外,还可以right-clickAssets
和selectAdd > Existing item…, select目标文件,点击Add
按钮导入文件。
此外,如果您想知道如何查看文件夹变量的输出,请参阅document。