此应用程序需要使用dispatcherTimer和TimePicker播放音频/视频/ ssml(xml)文件,以选择何时使用MediaElement控件播放文件。 使用FilePicker,MediaElement可以很好地播放位于项目的Assets目录内的文件。但是当我在项目目录之外选择文件时, 像“ C:\ Users \ Flazz \ Music \ AV \ PlayLibray \ Audio \ Filename1.mp3”一样,它不会执行任何操作。 我尝试了添加链接文件,但是vs2017中不存在该选项。据说VisualStudios团队知道他们需要调查此疏忽。 不过,必须有一个简单的解决方案。 也许我的路径是错误的,所以代码如下:
public UC_Mood()
{
this.InitializeComponent();
PickAFileButton.Click += new RoutedEventHandler(PickAFileButton_Click);
PickAFileButton2.Click += new RoutedEventHandler(PickAFileButton2_Click);
PickAFileButton3.Click += new RoutedEventHandler(PickAFileButton3_Click);
PickAFileButton4.Click += new RoutedEventHandler(PickAFileButton4_Click);
}
const string ENV_PROJ_PATH = "ms-appx:///Assets/AV/PlayLibray/Audio/";
const string ENV_CENTRAL_PATH = @"C:\Users\Flazz\Music\AV\PlayLibray\Audio\";
带有FileOpenPicker代码的点击事件处理程序:
private async void PickAFileButton_Click(object sender, RoutedEventArgs e)
{
// Clear previous returned file name, if it exists, between iterations of this scenario
tbFilePicked.Text = "";
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
openPicker.FileTypeFilter.Add(".mp3");
openPicker.FileTypeFilter.Add(".m4a");
openPicker.FileTypeFilter.Add(".m4v");
openPicker.FileTypeFilter.Add(".avi");
openPicker.FileTypeFilter.Add(".wav");
openPicker.FileTypeFilter.Add(".wmv");
openPicker.FileTypeFilter.Add(".mp4");
openPicker.FileTypeFilter.Add(".xml");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
tbFilePicked.Text = file.Name;
Debug.WriteLine(ENV_CENTRAL_PATH + tbFilePicked.Text);
}
else
{
tbFilePicked.Text = "Operation cancelled.";
}
}
带有MediaElement的Timer_Tick,其中项目路径之外的任何文件路径均不播放:
* note-在const string ENV_CENTRAL_PATH = @"C:\Users\Flazz\Music\AV\PlayLibray\Audio\";
private void timer_Tick(object sender, object e)
{
CountDown--;
txtCountDown.Text = CountDown.ToString();
if ((CountDown <= 0))
{
CountDown = 0;
try
{
MediaTool.Source = new Uri(ENV_CENTRAL_PATH + tbFilePicked.Text);
// MediaTool.Source = new Uri(ENV_PROJ_PATH + tbFilePicked.Text);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message.ToString());
}
MediaTool.Volume = 100;
MediaTool.Play();
timer.Stop();
}
}
感谢进阶! 亚当