Search code examples
c#windows-store-appswinrt-xamlmedia-playeruwp

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


I am trying to devlop a small example where I would use MediaCapture and MediaPlayer classes in universal windows app in order to record and play a short recording.

The following call to a constructor displays that lovely message in Visual Studio saying that there is no constructor which takes 0 arguments. The code doesn't compile therefore.

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

Msdn clearly states that there is such a constructor on this link. On the other hand it states as well that

[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.]

Is it the case here that it has changed? Is it me being dumb? How can I find out?


Solution

  • It depends on your Version. Before version 10.0.14393 you had to create background task for your player app. Instance has to be obtained thus:

    MediaPlayer player = BackgroundMediaPlayer.Current;
    

    But after anniversary update you don't need background task. You just can get new instance of player thus:

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