I’m trying to use the MediaPlayer API in the most simple way. All I want to do is play back an audio file (and possibly get a notification when it’s done).
Unfortunately, this forces me to use winRT and asynchronous operations, which I’m not very familiar with. So I’m despairing over the simple task of opening a file and passing it to the media player. I invested many hours and tried various approaches, but it still doesn’t work.
Here’s what I got at the moment:
using namespace winrt::Windows;
auto async = Storage::StorageFile::GetFileFromPathAsync(filename);
async.Completed([] (auto const & res, Foundation::AsyncStatus const status) {
if (status == Foundation::AsyncStatus::Completed) {
auto source = Media::Core::MediaSource::CreateFromStorageFile(res.GetResults());
Media::Playback::MediaPlayer player;
player.Source(source);
player.Play();
}
});
Please note that I actually don’t care about asynchronous ops. It’s fine in this context to wait for a sec for the file opening. But a simple async.get() or wait_for() asserts in debug builds and IIRC can create problems in release builds.
While the above code compiles and runs, playback never starts. Watching the global Windows error variable, I get various errors from MediaSource::CreateFromStorageFile(), depending on which file I’m trying to open: 1008 (“An attempt was made to reference a token that does not exist.”) and 14007 (“The requested lookup key was not found in any active activation context.”). Very cryptic.
So my question is actually twofold: Am I doing the file opening right or is there a better/simpler way? What’s up with those errors and how do I successfully open local audio files?