0

Trying to make some mediaplayer app. I put the mediaelement on wpf, wrote code to open media. But when i try to play it nothing is happening...

public void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
    if (args.Files.Count > 0)
    {
        foreach (StorageFile file in args.Files)
        {
            if (playlist.Contains(file.Path)) return;
            playlist.Add(file.Path);
        }
    }
}

private void PlayButton_OnClick(object sender, RoutedEventArgs e)
{
    MyMedia.Source = new Uri(playlist[0], UriKind.RelativeOrAbsolute);
    MyMedia.Play();
}

Checked that mediaelement source isn't empty, it has a right value of path. Try to rebuild like that, still doesnt work

private async void PlayButton_OnClick(object sender, RoutedEventArgs e)
    {
        var stream = await Playlist[0].OpenAsync(FileAccessMode.Read);
        MyMedia.SetSource(stream, Playlist[0].ContentType);
        MyMedia.Play();
    }
3

3 Answers 3

2

Sorry it was my mistake.

Into my XAML, I forgot that the AutoPlay property of the MediaElement control was set to false.

This solve my problem.

0
0

Couple of things you need to watch out.

foreach (StorageFile file in args.Files)
{
    if(playlist.Contains(file.Path))
    {
       return;// Dont return use continue. You will probably skip rest of the files.
    }
    playlist.Add(file.Path);
}

Add a Watch/Quickwatch to see if the new Uri(playlist[0], UriKind.RelativeOrAbsolute) points to a right location.

1
  • I think it is right "D:\a\3 Doors Down – Here Without You.mp3" if "D:\" emulated sdcard Feb 14, 2016 at 13:18
0

Setup source from storage file:

var storageFile = await KnownFolders.MusicLibrary.GetFileAsync("line.mp3");
var stream = await storageFile.OpenAsync(FileAccessMode.Read);
mediaElement.SetSource(stream, storageFile.ContentType);
mediaElement.Play();

I got it from this answer: How to play file from Library by MediaElement?

7
  • Can you provide me a sample? Feb 14, 2016 at 13:46
  • private async void PlayButton_OnClick(object sender, RoutedEventArgs e) { var stream = await Playlist[0].OpenAsync(FileAccessMode.Read); MyMedia.SetSource(stream, Playlist[0].ContentType); MyMedia.Play(); } Feb 14, 2016 at 13:54
  • where you declared media element? Feb 14, 2016 at 13:55
  • did you mean? <MediaElement x:Name="MyMedia" MediaFailed="MyMedia_OnMediaFailed" ></MediaElement> Feb 14, 2016 at 14:01
  • yes, sure. All is good. I don't see any problem. can you send me an archive? Feb 14, 2016 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.