UWP MediaPlayerElement不播放任何声音

时间:2019-12-22 09:01:42

标签: uwp windows-media-player

我尝试在UWP应用中播放mp3。在下面的简单代码中,它不会播放任何声音和视频文件。没有错误信息。 XAML控件库中的示例效果很好。

SELECT 
  dates.date AS date,
  to_char(dates.date, 'day') AS day_of_week,
  sh.name, 
  COALESCE(oh.time_open, th.time_open) AS time_open,
  CASE
    WHEN oh.time_open IS NOT NULL
    THEN oh.time_open + (oh.duration::text||' minute')::INTERVAL
    ELSE th.time_open + (th.duration::text||' minute')::INTERVAL
  END AS time_close
FROM (
   SELECT current_date - extract(dow from current_date)::Int + series AS date 
   FROM generate_series(0,6) AS series
) AS dates
LEFT JOIN trading_hours th ON th.day_of_week = extract(dow from dates.date)
LEFT JOIN other_hours oh ON 
   th.shop_id=oh.shop_id AND 
   oh.day_of_week=th.day_of_week AND 
   dates.date >= oh.start_date AND 
   dates.date <= oh.end_date
JOIN shop sh ON th.shop_id=sh.id
ORDER BY sh.name, dates.date, time_open;

使用FileOpenPicker没什么区别,文件路径和名称输入似乎不是原因。我想念什么?

1 个答案:

答案 0 :(得分:1)

首先,在后台代码中创建MediaPlayerElement控件时,需要将其添加到父面板中以使其在前台显示。在这种情况下,它将播放。

此外,uri也不正确。您可以使用URI方案来引用来自应用程序包,数据文件夹或资源而不是D驱动器的应用程序文件。如果要使用完整路径播放,可以使用StorageFile.GetFileFromPathAsync()方法或FileOpenPicker来获取文件,然后将文件传递给MediaSource.CreateFromStorageFile()方法以设置Source。

private void Button_Click(object sender, RoutedEventArgs e)
{
    MediaPlayerElement _MediaPlayerElement = new MediaPlayerElement();
    //uri
    _MediaPlayerElement.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/xxx.mp3"));

    //file path
    //StorageFile file = await StorageFile.GetFileFromPathAsync(@"D:\xxx.mp3");
    //_MediaPlayerElement.Source = MediaSource.CreateFromStorageFile(file);
    _MediaPlayerElement.AutoPlay = true;

    MyBigPanel.Children.Add(_MediaPlayerElement);
}