Windows IoT Raspberry Pi 3 c# MediaPlayer.mediaEnded

786 views Asked by At

I am new to this. I am trying to do a Audio Message Player on Rasp Pi 3 with Win IoT core. I manage to play audio file from my USB thumbdrive but I need to know when the audio file has completed playing.

 mediaPlayer = new MediaPlayer();
 mediaPlayer.MediaEnded += MediaPlayer_MediaEnded;

 private void MediaPlayer_MediaEnded(MediaPlayer sender, object args)
    {
        GeneralMessage.Text = "Message Complete!";
    }

i get an error message with the above code.

System.Exception occurred
  HResult=0x8001010E
  Message=The application called an interface that was marshalled for a 
different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

Please help.

2

There are 2 answers

0
mylim On BEST ANSWER

i manage to solve the invoke thingy.. code as follow

private async void MediaPlayer_MediaEnded(MediaPlayer sender, object args)
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            GeneralMessage.Text = "Done playing.";

     });
1
Michael Xu On

The reason of your problem is that, UI changes must be made on the UI thread but MediaEnded event is raised on a different thread.

In windows IoT Core, when you update the element in UI from another thread, please use the Dispatcher.RunAsync method.

Task.Run(async () =>
{
     //this part is run on a background thread...

     await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, 
        ()=> 
        {
             //this part is being executed back on the UI thread...
             GeneralMessage.Text = "Message Complete!";
        });
});

Please reference here Media items, playlists, and tracks.This article showed how to use MediaSource , and included the operations performed within a CoreDispatcher.RunAsync call.