为什么此代码段不起作用? Uwp应用程序内容可播放媒体内容

问题描述

这是我的代码,我来自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 folder

Exception

解决方法

仅将文件添加到文件资源管理器中的Assets文件夹中并不是将文件导入到项目中的完整步骤。您还需要将文件添加到 Visual Studio Solution Explorer 中的Assets中。您可以按照以下步骤导入文件:

  1. Solution Explorer 中单击Show All Files选项,然后在timesup.mp3文件夹中找到Assets文件。

Solution Explorer

  1. 右键单击timesup.mp3文件,然后选择Include In Project选项。

Context Menu

  1. 点击timesup.mp3文件,并确保在属性窗口中将Build Action设置为Content

Properties window

除了使用上述步骤将文件导入到Assets外,您还可以右键点击Assets并选择 Add>现有项…,然后选择目标文件,然后单击Add按钮以导入文件。

此外,如果您想知道如何查看文件夹变量的输出,请参阅document

,

如果在GetFolderAsync上设置断点,则应该能够查看文件夹变量,并查看其认为Path的位置...是否与您期望的路径相同(C:\ Users。 ... repos \ Multi-timer)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...