I’ve developing a Windows 8 app in Visual Studio 2012 ultimate but i’m having an issue with the MediaElement where the MediaOpened and MediaEnded events are not being fired.
Below is my code:
public static async void PlaySound(string AudioFile, RoutedEventHandler PlayerEnded)
{
Uri audioUri = new Uri("ms-appx:///Assets/" + AudioFile);
StorageFile audioStorage = await StorageFile.GetFileFromApplicationUriAsync(audioUri);
if (audioStorage != null)
{
var stream = await audioStorage.OpenAsync(Windows.Storage.FileAccessMode.Read);
MediaElement player = new MediaElement();
player.AudioCategory = AudioCategory.GameEffects;
player.AutoPlay = false;
player.MediaEnded += PlayerEnded;
player.MediaOpened += new RoutedEventHandler(delegate(Object sender, RoutedEventArgs e)
{
player.Play();
});
player.SetSource(stream, audioStorage.ContentType);
}
}
Basically this will be passed a button click sound file which after finish playing will call the this.Frame.Navigate method to switch to another page. But it doesn’t work since none of the events are being fired.
If I set AutoPlay = true then it plays fine, but then events still don’t get fired.
Can anyone see what is wrong?
Thanks for your help
I think your MediaElement needs to be in the visual tree to fire.
See http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/f5b9cdba-5521-467d-b838-8420afc68e7f/