locked
Playing sounds RRS feed

  • Question

  • There is the example of playing sounds available which works. But I can't seem to optimize it for constantly replaying the sound.

    In the example we get this...

    var var_assets = await Package.Current.InstalledLocation.GetFolderAsync("assets");
    var var_file = await var_assets.GetFileAsync(sound_name);
    var var_stream = await var_file.OpenAsync(FileAccessMode.Read);
    MediaElement m = new MediaElement();
    m.SetSource(var_stream, var_file.ContentType);
    m.Play()

     What I have learned about this is that you do NOT actually need to call Play() in order to play the sound. The first time everything runs up to setsource(), the sound is played. What I am trying to do is build sound engine where the sound data is loaded and ready to be played over and over. Running into 2 problems.

    1) the sound is played before I actually call Play(). My initialization routine calls everything up to SetSource, and when setsource is called the sound is played. I can't seem to stop this from happening.

    2) When I am ready to play the sound, I can  not play the sound over and over by calling play. Pretend I can a button click hooked to the play() function, when it's called it will only play from beginning if if has completely finished playing from the previous Play() call. I tried doing things like calling Stop() first, but it does not work.

    Any Ideas?

    Thanks

    Matt

    Monday, November 12, 2012 8:53 PM

Answers

  • for the first issue turn the AutoPlay property on false?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Ososky Thursday, November 15, 2012 3:23 AM
    Monday, November 12, 2012 9:11 PM

All replies

  • for the first issue turn the AutoPlay property on false?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Ososky Thursday, November 15, 2012 3:23 AM
    Monday, November 12, 2012 9:11 PM
  • Yes, that solves that problem.
    Monday, November 12, 2012 10:13 PM
  • and about the second i am afraid the media element is not quick enough for it... know a friend who tried and he switched to direct x

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, November 12, 2012 10:15 PM
  • I've worked with Direct X. But one would think there is a logical way to restart a media element from the beginning on demand.
    • Edited by Ososky Monday, November 12, 2012 11:10 PM
    Monday, November 12, 2012 11:09 PM
  • Well I guess that's as good as it gets. I was somewhat able to solve the second problem not by 'restarting' the media element, but by calling the SetSource on the element again. It doesn't feel quite right, but it works.
    Thursday, November 15, 2012 3:26 AM