MediaElement Dying silently! RRS feed

  • Question

  • Hello

    I am playing audio using MediaElement but i am not able to hear any sound. I am getting no exceptions, nothing!

    I have also updated my drivers but it is of no use.

    Can anyone please guide me through this ?

    Wednesday, January 14, 2015 11:32 AM

All replies

  • Does the MediaElement sample work?


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, January 14, 2015 7:14 PM
  • Are you handling the MediaElement.MediaFailed event?


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

    Thursday, January 15, 2015 12:37 AM
  • @Matt Small

    Yes the sample that you mentioned is working fine.

    You asked for the code. Its here. Can you check that it is going through the .mediaOpen() method and not failing i.e, not going through the mediaFailed() method. Still i am not able to hear any sound. Though when i do it on WPF it works completely fine.

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641 namespace PracMEWindowsApp { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Required; } /// <summary> /// Invoked when this page is about to be displayed in a Frame. /// </summary> /// <param name="e">Event data that describes how this page was reached. /// This parameter is typically used to configure the page.</param> protected override void OnNavigatedTo(NavigationEventArgs e) { // TODO: Prepare page for display here. // TODO: If your application contains multiple pages, ensure that you are // handling the hardware Back button by registering for the // Windows.Phone.UI.Input.HardwareButtons.BackPressed event. // If you are using the NavigationHelper provided by some templates, // this event is handled for you. } private void play_button_click(object sender, RoutedEventArgs e) { mycontrol_MediaOpened(sender, e); } private void pause_button_click(object sender, RoutedEventArgs e) { mycontrol.Pause(); } private void stop_button_click(object sender, RoutedEventArgs e) { mycontrol.Stop(); } private string GetHresultFromErrorMessage(ExceptionRoutedEventArgs e) { String hr = String.Empty; String token = "HRESULT - "; const int hrLength = 10; // eg "0xFFFFFFFF" int tokenPos = e.ErrorMessage.IndexOf(token, StringComparison.Ordinal); if (tokenPos != -1) { hr = e.ErrorMessage.Substring(tokenPos + token.Length, hrLength); } return hr; } private void mycontrol_MediaFailed(object sender, ExceptionRoutedEventArgs e) { // get HRESULT from event args string hr = GetHresultFromErrorMessage(e); // Handle media failed event appropriately } private void mycontrol_MediaOpened(object sender, RoutedEventArgs e) { Uri uri = new Uri("ms-appx:///Addicted.mp3"); mycontrol.Source = uri; //play the new audio file mycontrol.Play(); } } }


        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <MediaElement x:Name="mycontrol" HorizontalAlignment="Left" Height="100" Margin="94,331,0,0" VerticalAlignment="Top" Width="100" AutoPlay="False" MediaFailed="mycontrol_MediaFailed" MediaOpened="mycontrol_MediaOpened"/>
            <Button Content="play" HorizontalAlignment="Left" Margin="75,240,0,0" VerticalAlignment="Top" Width="255" Click="play_button_click"/>
            <Button Content="pause" HorizontalAlignment="Left" Margin="75,336,0,0" VerticalAlignment="Top" Width="255" Click="pause_button_click"/>
            <Button Content="stop" HorizontalAlignment="Left" Margin="75,426,0,0" VerticalAlignment="Top" Width="255" Click="stop_button_click"/>
            <TextBlock HorizontalAlignment="Left" Margin="94,95,0,0" TextWrapping="Wrap" Text="Media Player" VerticalAlignment="Top" Height="82" Width="236" FontSize="36"/>

    • Edited by Soph6 Friday, January 16, 2015 3:55 PM
    Friday, January 16, 2015 2:44 PM
  • @James Dailey

    Yes i have. And i debugged it. It is not going through the mediaFailed() method. It is going into the mediaOpened() method yet i am not able to hear sound. Though i am able to hear it using WPF ?

    • Edited by Soph6 Friday, January 16, 2015 3:56 PM
    Friday, January 16, 2015 2:52 PM
  • Hello,

    You should not set the MediaElement source in the MediaOpened event. You must move all of the code from your mycontrol_MediaOpened event handeler to your play_button_click event handler. Give this a try and let us know if it works.


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

    Thursday, January 29, 2015 9:28 PM
  • @James Dailey

    Yes i have also tried this. But the same result again. I can hear no sound.

    Friday, January 30, 2015 3:21 PM
  • Hello,

    Unfortunately I don't have any other suggestions. Without being able to debug into you code I'm not sure I can offer any additional help. If you would like to create a very small Visual Studio 2013 project that only contains the code necessary to reproduce the problem, zip it up (don't use RAR), put it on your OneDrive and post the link here I will take a look and get back to you with what I find.



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

    Monday, February 2, 2015 10:55 PM