locked
Media Events not Firing if instantiated from function

    Question

  • I added a event handler to get called whenever MediaElement State changes but it doesn't seem to get invoked. Any idea ? I am playing a .mp3 song file.

    However, if I define it in XAML (e.g <MediaElement x:Name="Main" CurrentStateChanged="Somehandler"/> it works as expected.

    What is happening here ?

    BlankPage::BlankPage()
    {
    	InitializeComponent();
    	MainSong = ref new MediaElement();
    	MainSong->AutoPlay = false;
    	MainSong->CurrentStateChanged  += ref new RoutedEventHandler(this, &Application2::BlankPage::StateChangedEventHandler); //This event handler is never getting invoked
    }








    • Edited by Shawn Dan Sunday, March 11, 2012 4:59 AM
    Sunday, March 11, 2012 3:13 AM

Answers

  • Hello,

    I did figure out why events are not being fired by the Media Element if it is not explicitly added to the XAML declaration. The Media Element must be in the visual tree before it's events will be picked up by the event pump of the page. Navigating from one page to another expectedly invalidates the Media Element on the previous page and causes the audio to stop playing.

    I hope this helps,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Tuesday, April 10, 2012 10:40 PM
    Moderator

All replies

  • Hello Sam,

    Just taking a quick look at this it should work for your as expected. Unfortunately I need to leave for the day but I will definitely look at this for you. If you don't hear from me in a day or so and are still having this issue please bump this thread and I will make sure to get back to you.

    Thanks,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Tuesday, March 13, 2012 1:16 AM
    Moderator
  • Hi James,

    Did you get a chance to look into this ? I can send you my project if you want to check it

    Thanks

    Tuesday, March 13, 2012 11:50 PM
  • Hello Sam,

    Sorry for the late reply. Please make your project available to me and I will certainly take a look. skydrive.live.com is preferred.

    Thanks,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Thursday, March 15, 2012 12:25 AM
    Moderator
  • Was there a solution to this issue? I am having the same problem.
    Monday, March 26, 2012 8:38 PM
  • Hello Diamond,

    I didn't get a project from Sam. Can you make your code available on skydrive.live.com for me?

    Thanks,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Tuesday, March 27, 2012 12:33 AM
    Moderator
  • James

    Here is the link Demo App

    This app will attempt to play a song on load. No events will fire.

    Desired outcome: Music continues to play between pages AND events still fire.

    Monday, April 2, 2012 2:31 AM
  • Hello Diamond,

    Sorry for the late reply. I was out of the office longer than expected. Are you still having problems with this? If so please let me know and I will do what I can to help you.

    Thanks,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Friday, April 6, 2012 10:54 PM
    Moderator
  • James,

    Did you get a chance to try out his demo ? Yes, the problem still exists.

    Thanks

    Sunday, April 8, 2012 5:16 AM
  • Hello,

    I did figure out why events are not being fired by the Media Element if it is not explicitly added to the XAML declaration. The Media Element must be in the visual tree before it's events will be picked up by the event pump of the page. Navigating from one page to another expectedly invalidates the Media Element on the previous page and causes the audio to stop playing.

    I hope this helps,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Tuesday, April 10, 2012 10:40 PM
    Moderator
  • I'm not sure how to fix it...

    How could I add the media element into the visual tree via code (to a place where it wont get ignored depending on other pages/current page)

    or maybe some way of not invalidating a page when it's navigated away from???

    Wednesday, April 11, 2012 4:58 AM
  • Add a frame to your base page with the ME and then navigate to your other pages. I tested this and it works very well.

    -James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Thursday, April 12, 2012 1:17 AM
    Moderator
  • Add a frame to your base page with the ME and then navigate to your other pages. I tested this and it works very well.

    -James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Can you show How to do this?

    cmorgado

    Friday, March 7, 2014 12:58 PM