locked
How to play a sound? RRS feed

  • Question


  • Well I don't know for you guys but I tried

    This :

    string sSound = "ms-appx:///Sounds/ShipThrust.mp3";
    StorageFile sf = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri(sSound));
    Windows.Storage.Streams.IRandomAccessStream s = await sf.OpenAsync(FileAccessMode.Read);
    SoundPlayer.SetSource(s, sf.ContentType);
    

    This

    SoundPlayer.Source = new Uri(this.BaseUri, "..///Sounds/ShipThrust.mp3");

    This

    SoundPlayer.Source = new Uri(this.BaseUri, "..//Sounds/ShipThrust.mp3");

    And finally this

    SoundPlayer.Source = new Uri(this.BaseUri, "../Sounds/ShipThrust.mp3");

    None of this plays the sound  and here's the code that plays

    SoundPlayer.Position = TimeSpan.Zero;
    SoundPlayer.Play();

    What am I missing ?


    Monday, December 30, 2013 7:52 PM

Answers

  • Hi GearWorld,

    Your first clip looks reasonable, but not complete. I assume SoundPlayer is a MediaElement. To play it needs to be in the visual tree and you need to either set it to AutoPlay=true or call Play() after the source has finished loading and is ready. Calling Play immediately after SetSource won't work. Instead you'll need to wait on the CurrentStateChanged event to know when it's ready.

    The other clips look like they probably aren't finding the right source and would be better off with the ms-appx:///Sounds/ShipThrust.mp3 syntax.

    --Rob

    Monday, December 30, 2013 9:55 PM
    Moderator

All replies

  • Well, I found out that I was not adding the player to the UI that's how. As soon as I did Children.Add(MyPlayer) it worked.  Sorry about that

    Monday, December 30, 2013 9:52 PM
  • Hi GearWorld,

    Your first clip looks reasonable, but not complete. I assume SoundPlayer is a MediaElement. To play it needs to be in the visual tree and you need to either set it to AutoPlay=true or call Play() after the source has finished loading and is ready. Calling Play immediately after SetSource won't work. Instead you'll need to wait on the CurrentStateChanged event to know when it's ready.

    The other clips look like they probably aren't finding the right source and would be better off with the ms-appx:///Sounds/ShipThrust.mp3 syntax.

    --Rob

    Monday, December 30, 2013 9:55 PM
    Moderator
  • It's all good now. Thank you !

    The game starts to look awesome already.  My first one ever.  Well it's not really original I agreed since it's a replica of the old Gorgon from Apple IIe 1987 :) but well I had to learn and now I'll take probably a step further going original in C++. 

    C# and xaml is so easy and I didn't know I would be able to put a complete game alive with it.
    Here's a small video showing what it looks like actually :

    https://skydrive.live.com/redir?resid=7056CFFE05236B7D!916&authkey=!AJahUfIVwew7nFk&ithint=video%2c.wmv

    Enjoy the show

    Tuesday, January 7, 2014 10:55 AM