Arslan Pervaiz
Arslan Pervaiz

Reputation: 1643

Mediaelement in Windows 8 Metro App

I have a scenario specific to my app. I am managing music file playlist in XML for my metro app. And its saving music files actual path like this

D:\MyMedia\1.mp3

I have media element in my XAML page and I am setting its Source like this.

 mediaElement.Source = new Uri(@"D:\MyMedia\1.mp3", UriKind.Absolute);
 mediaElement.Play();

but its not playing the media and its giving the error like this

MF_MEDIA_ENGINE_ERR_SRC_NOT_SUPPORTED : HRESULT - 0x80070005

So someone tell me how I can play some media file in MediaElement of metro app with absoulte path. Or how I can get stream of my local file to play this media in my mediaElement of Metro app.

Upvotes: 1

Views: 2108

Answers (4)

ArchAngel
ArchAngel

Reputation: 644

I know it's an old problem, I found a really simple solution..

Windows.Storage.StorageFile file = null;
mediaElement player = new MediaElement();


    async Task PlayAsync()
    {
        if (file == null)
        {
            file = await OpenFileAsync();

            try { player.MediaOpened -= Player_MediaOpenedAsync; } catch { }
            try { player.MediaEnded -= Player_MediaEnded; } catch { }
            try { player.MediaFailed -= Player_MediaFailed; } catch { }

            player.MediaOpened += Player_MediaOpenedAsync;
            player.MediaEnded += Player_MediaEnded;
            player.MediaFailed += Player_MediaFailed;

            player.SetPlaybackSource(MediaSource.CreateFromStorageFile(file)); //Works with media playback..
            //player.Source = new Uri(mediasource, UriKind.RelativeOrAbsolute); //Doesn't work with media playback for some reason..
            Playing = true;
            Paused = false;
        }
        else
        {
            if (Paused)
            {
                player.Play();
                Paused = false;
            }
            else
            {
                player.Pause();
                Paused = true;
            }
        }
    }

    async Task<StorageFile> OpenFileAsync()
    {
        try
        {
            var ofd = new FileOpenPicker();
            ofd.FileTypeFilter.Add("*");
            return await ofd.PickSingleFileAsync();
        }
        catch { }
        return null;
    }

Upvotes: 0

KidsAndIWriteCode
KidsAndIWriteCode

Reputation: 1

This can be done without a file picker. You just have to add Music Library capabilities to your app manifest, make sure the music is in My Music or on an SD Card, and use KnownFolders.MusicLibrary.

In short and in keeping with the music theme of the question:

"You might think file picker...

...but all I need is StorageFile"

    //
    // Opens and plays song titled "[Cars]You_Might_Think.mp3"
    // in the 80s folder.
    //
    // IMPORTANT: the example song must be in the user's Music
    // folder or SD card to be found.   Also, the app manifest
    // must have 'Music Library' capabilities enabled.
    //
    // 
    //
    async void OpenAndPlayAwesomeSongFromThe80s()
    {            
        Windows.Storage.StorageFolder folder = KnownFolders.MusicLibrary;

        StorageFile song = await folder.GetFileAsync("80s\\[Cars]You_Might_Think.mp3");

        if (song != null)
        {
            IRandomAccessStream stream = await song.OpenAsync(Windows.Storage.FileAccessMode.Read);

            if (stream != null)
            {
                mediaElement = new MediaElement();
                mediaElement.SetSource(stream, song.ContentType);
                mediaElement.Play();
            }
        }
    }

Upvotes: 0

Johann
Johann

Reputation: 523

There is only a limited number of locations on a user's PC that you can access. "D:\Mymedia" is not one of those. You will find all the necessary information in the Microsoft help. Check out these two articles:

Upvotes: 0

Antonio Pelleriti
Antonio Pelleriti

Reputation: 859

To open files on the local system, you can use the FileOpenPicker to get the file and SetSource to set the media source.

var openPicker = new Windows.Storage.Pickers.FileOpenPicker();

openPicker.FileTypeFilter.Add(".mp3");
var file = await openPicker.PickSingleFileAsync();

var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

if (null != file)
{
    mediaElement.SetSource(stream, file.ContentType);
    mediaElement.Play();
}

Upvotes: 4

Related Questions