这是我的代码,我从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();
}
Just adding files to the Assets
folder in File Explorer is not a complete step of importing file into the project. You also need to add the file to the Assets
in the Solution Explorer of Visual Studio. You can check the following steps to import the file:
Show All Files
option in the Solution Explorer, and find the timesup.mp3
file in the Assets
folder.timesup.mp3
file and select the Include In Project
option.timesup.mp3
file and ensure the Build Action
is set to Content
in the Properties window.除了使用上述步骤将文件导入到之外Assets
,您还可以右键单击Assets
并选择添加>现有项…,选择目标文件,然后单击Add
按钮以导入文件。
此外,如果您想了解如何查看文件夹变量的输出,请参阅文档。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句