MediaElement working within WPF but not within Windows 8.1 App

0

The code shown below, which plays an audio file, runs fine within my WPF application. But when I execute the same code within a Windows 8.1 app, I am not getting any exceptions but I am also receiving no sound. Can anyone help?

private void myMediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
    myMediaElement.Source =
        new Uri(@"C:\Users\Soph\Music\Addicted.mp3", UriKind.Absolute);
    myMediaElement.Play();
}

private void btn1_Click(object sender, RoutedEventArgs e)
{
    myMediaElement_MediaOpened(sender,e);
}

EDIT:

I have added per the advice the mediaFailed event (followed from http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.mediaelement.mediafailed)

    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 
    }

Then i tried to debug this:

Name Value Type

this {PracMEWindowsApp.MainPage} PracMEWindowsApp.MainPage e {Windows.UI.Xaml.ExceptionRoutedEventArgs} Windows.UI.Xaml.ExceptionRoutedEventArgs hr "0x80070003" string token "HRESULT - " string tokenPos 40 int hrLength 10 int

What is this hr capturing ? Why is my file not played ?

c#
wpf
windows-store-apps
asked on Stack Overflow Jan 14, 2015 by SoProgram • edited Jan 14, 2015 by SoProgram

3 Answers

0

To access the Music library specify the Music Library capability in app manifest. It is also good practice to always handle the MediaFailed event.

answered on Stack Overflow Jan 14, 2015 by user2250152
0

HRESULT 0x80070003 is "Directory Not Found". That would suggest that the media element doesn't like something about the file path. If you get HRESULT 0x80070005 (Access Denied), then it's probably a permissions-related thing.

answered on Stack Overflow Jan 14, 2015 by Keith Frechette
0

Windows Store app does not have access to files in local file system, it runs in a sandboxed environment and only has access to its own data folder, a path like C:\Users\Soph\Music\Addicted.mp3 is unrecognizable.

There are several ways in which you can play the media file.

a.Specify the Music Library capability in Package.appxmanifest (this is required), and then

private async void btn1_Click(object sender, RoutedEventArgs e)
{
    var file = await Windows.Storage.KnownFolders.MusicLibrary.GetFileAsync("Addicted.mp3");
    var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
    myMediaElement.SetSource(stream, file.ContentType);
}

or

b. use FileOpenPicker, choose the song manually, you can access any .mp3 file in the file system.

private async void btn1_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.FileTypeFilter.Add(".mp3");
    openPicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
    var file = await openPicker.PickSingleFileAsync();
    var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
    myMediaElement.SetSource(stream, file.ContentType);
}

or

c. Add Addicted.mp3 to your project, and then

Uri uri = new Uri("ms-appx:///Addicted.mp3");
myMediaElement.Source = uri;

you can also set it in XAML

<MediaElement x:Name="myMediaElement" Source="Addicted.mp3" />
answered on Stack Overflow Jan 15, 2015 by kennyzx • edited Jan 15, 2015 by kennyzx

User contributions licensed under CC BY-SA 3.0