Windows IoT Raspberry Pi 3 c# MediaPlayer.mediaEnded

1

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.

c#
raspberry-pi
raspberry-pi3
windows-iot-core-10
asked on Stack Overflow Sep 13, 2017 by mylim • edited Sep 13, 2017 by JBC

2 Answers

0

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.

answered on Stack Overflow Sep 14, 2017 by Michael Xu - MSFT
0

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.";

     });
answered on Stack Overflow Sep 18, 2017 by mylim

User contributions licensed under CC BY-SA 3.0