none
MediaElement created in code doesn't respect Stop() function, can't reset to starting position.

    Pregunta

  • Hello,

    I'm using a MediaElement created in code to play a sound effect, one that the user can trigger by menu navigation such as a selected noise.

    Since the user can trigger the selected sound rapidly, I want to stop the previous instance, and play the effect again from the beginning.

    But calling MediaElement.Stop() will NOT change the CurrentState from Playing to Stopped. There doesn't seem to be anything I can do to get it to restart from the start position. I tried manually setting the Position, but that only seems to work when it's stopped. I also tried Pause() instead, and just adding a delay between the Stop, setting the position, & Play. The only thing that will let me play from the start position is letting it play through completely, and then doing that. (CanSeek is true).

    I don't think it's relevant, but I actually use two MediaElements so I can play two versions of the sound effect on top of itself, max, but if the user is going really fast (rarer) I'll just cut out one of the two instances. I've tried it with just one as well, same issue.

    I'm concerned it's related to this issue: http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/f5b9cdba-5521-467d-b838-8420afc68e7f that the MediaElements don't get events when not created in the XAML page. However, maybe not, because I'm manually calling Stop(), you'd think that would do *something* without requiring an event. (This app doesn't have multiple pages to navigate between, and no other api to add it to the page is mentioned there, anyway).

    Any ideas?

    Thanks,

      Borut

    sábado, 05 de mayo de 2012 17:49

Respuestas

  • Brent,

    I am very sorry but you really should be writing your DirectX based Metro style application in C++. We really can't support you in C# and we certainly can't support SharpDX. Again I would highly recommend that you move your codebase to C++. This will give you access to XAudio2.

    That said, you can simply add your instantiated ME to the visual tree using: SwapchainBackgroundPanel.Childreen.Append.

    Thanks,

    James


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

    martes, 08 de mayo de 2012 0:39
    Moderador

Todas las respuestas

  • Maybe this is a bug, you should follow that thread.

    NEU_ShieldEdge

    lunes, 07 de mayo de 2012 11:43
  • BorutP,

    Unless the MediaElement is explicitly added to the visual tree internally the message pump of the MediaElement doesn't get well... "pumped". One of the side effects of this is that you don't get events reported by the MediaElement. This is actually by design as the MediaElement does derive from a standard XAML control.

    That said, you say that you are playing a sound effect when a menu item is selected. I'm going to wager that this is a very short sound file of less than one second. There are known issues with the underlying Media Foundation infrastructure that can cause problems when playing sounds less than one second in length. Because of this we don't recommend that you use the Media Element to play very short sounds. We are aware of this problem and hope to have this addressed soon.

    Personally I would recommend that you use XAudio2 to play back very short sound effects. This API is specifically designed for playing short and repeated audio files and has been optimized for this very use. You can also successfully mix XAludio2 sound effects and standard audio playback using the MediaElement in the same application.

    XAudio2 audio stream effect sample
    http://code.msdn.microsoft.com/windowsapps/XAudio2-Stream-Effect-3f95c8f2

    I hope this helps,

    James


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


    martes, 08 de mayo de 2012 0:10
    Moderador
  • Well, I do find it odd that manually calling Stop still needs the MediaElement to pump an event.

    Anyway, you're right, yes it is a relatively short sound effect. But this is C# app and therefore I don't have access to XAudio.

    I have tried using SharpDX which is a public wrapper for DirectX to C#, with some metro support, however I've had issues trying to use Xaudio through it (doesn't seem to be fully supported yet, possibly, unlike the graphics functions which do work fine). Still, MediaElement seems to be the only way provided to play sounds in C#.

    Is there any way to add the element to the tree without doing page navigation (the app only has one page, a SwapchainBackgroundPanel)?

    Thanks,

     Borut

    martes, 08 de mayo de 2012 0:23
  • Brent,

    I am very sorry but you really should be writing your DirectX based Metro style application in C++. We really can't support you in C# and we certainly can't support SharpDX. Again I would highly recommend that you move your codebase to C++. This will give you access to XAudio2.

    That said, you can simply add your instantiated ME to the visual tree using: SwapchainBackgroundPanel.Childreen.Append.

    Thanks,

    James


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

    martes, 08 de mayo de 2012 0:39
    Moderador