如何实例化uwpWindows.Media.Playback.MediaPlayer?

How to instantiate uwp Windows.Media.Playback.MediaPlayer?

我正在尝试开发一个小示例,我将在通用 windows 应用程序中使用 MediaCapture 和 MediaPlayer 类 来录制和播放短录音。

以下对构造函数的调用在 Visual Studio 中显示了一条可爱的消息,即没有采用 0 个参数的构造函数。因此代码无法编译。

//using Windows.Media.Playback;
MediaPlayer mediaPlayer = new MediaPlayer();

msdn明明在this link上有这样的构造函数。另一方面,它也指出

[Some information relates to pre-released product which may be substantially modified before it's commercially released. Microsoft makes no warranties, express or implied, with respect to the information provided here.]

这里是不是变了?是我傻吗?我怎样才能知道?

您可以在 uwp 中使用 MediaElement 控件。

MediaElement 应该可以工作,刚刚在我的 Visual Studio

中试过了
MediaElement ml = new MediaElement();

Media player can be instantiate as below:

private void LoadEmbeddedAppFile() { MediaElement mediaPlayer = new MediaElement(); mediaPlayer.RealTimePlayback = true;

}

//Or you can use simple control from add on designer page..

//Than you can define any video file to its source as below...

private void LoadEmbeddedAppFile() { try { Uri pathUri = new Uri("ms-appx:///Videos/video1.mp4"); mediaPlayer.Source = pathUri; } catch (Exception ex) { if (ex is FormatException) { // handle exception. // For example: Log error or notify user problem with file } } }

//Media player can be instantiate as below:

private void LoadEmbeddedAppFile()
{
    MediaElement mediaPlayer = new MediaElement();
    mediaPlayer.RealTimePlayback = true;

}



//Or you can use simple control from add on designer page..

//<MediaElement x:Name="mediaPlayer" 
//Source="Videos/video1.mp4" 
//Width="400" 
//AutoPlay="False"
//AreTransportControlsEnabled="True"/>



//Than you can define any video file to its source as below...

private void LoadEmbeddedAppFile()
{
    try
    {
        Uri pathUri = new Uri("ms-appx:///Videos/video1.mp4");
        mediaPlayer.Source = pathUri;
    }
    catch (Exception ex)
    {
        if (ex is FormatException)
        {
            // handle exception. 
            // For example: Log error or notify user problem with file
        }
    }
}

刚刚在我自己的项目中遇到了同样的问题。如果您不能使用 0 个参数实例化 MediaPlayer,您很可能必须更改 windows 的目标版本。这种音频播放的简化已在周年更新中引入,因此要使用它,请将目标更改为它。

在 Visual Studio (2015):

  1. 右键单击您的项目
  2. 选择Properties
  3. Application选项卡
  4. Targeting 面积
  5. Target version & Min version 设置为至少 Windows 10 Anniversary Edition

Screenshot - Visual Studio 2015 - Target version

希望对遇到同样问题的大家有所帮助=)

有 2 个文档可以提供帮助:

  1. As it says in msdn:

MediaPlayer is different from a MediaElement in that it is not a control that can be added directly to the user interface (UI) of an application. To display media loaded using MediaPlayer, a VideoDrawing or DrawingContext must be used.

所以你应该在绘图中使用它(参见 this example)。

  1. 对方说it requires at least Windows 10.

这取决于您的版本。在版本 10.0.14393 之前,您必须为播放器应用程序创建后台任务。必须这样获得实例:

MediaPlayer player = BackgroundMediaPlayer.Current;

但是周年更新后就不需要后台任务了。您可以这样获得播放器的新实例:

private MediaPlayer CurrentPlayer
   {
      get
      {
         if (_mediaPlayer != null) return _mediaPlayer;
         _mediaPlayer = new MediaPlayer {AutoPlay = false, AudioCategory = MediaPlayerAudioCategory.Media};
         return _mediaPlayer;
      }
   }