locked
how to set listview item as source for mediaelement?

    Question

  • is there a way to set listview item as source for mediaelement, if so please provide a sample code.
    Friday, January 9, 2015 2:47 PM

Answers

  • Hi Kranthi88,

    According to your code snippet, I modify it and it can work now. You can see the following code, copy and paste it into your project.

     

    private async void loadall(object sender, RoutedEventArgs e)
    
            {
    
                var folderPicker = new FolderPicker();
    
                folderPicker.FileTypeFilter.Add(".mp3");
    
                var folder = await folderPicker.PickSingleFolderAsync();
    
                var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".mp3" });
    
                queryOptions.FolderDepth = FolderDepth.Deep;
    
    
                var query = folder.CreateItemQueryWithOptions(queryOptions);
    
                var items = await query.GetItemsAsync();
    
                //if user select other folder on drive, you should copy that file in local storage to make sure you can 
    
                //find the music when playing
    
                var files = items.OfType<IStorageFile>().Select(s => new MusicInfo() { Name = s.Name }).ToList();
    
                listf.ItemsSource = files;
    
              
    
                //trying  to play list song
    
               
    
            }
    
    
            private async void playyyy(object sender, ItemClickEventArgs e)
    
            {
    
                MusicInfo music = e.ClickedItem as MusicInfo;
    
                if (music != null)
    
                {
    
                    Debug.WriteLine(music.Name);
    
                    var musiclibrary = Windows.Storage.KnownFolders.MusicLibrary;
    
                    var file =await musiclibrary.GetFileAsync(music.Name);
    
                    MusicProperties musicProp = await file.Properties.GetMusicPropertiesAsync();
    
                    myblock.Text = musicProp.Title;
    
                    var stream = await file.OpenAsync(FileAccessMode.Read);
    
                    mymedia.SetSource(stream, file.ContentType);
    
                    mymedia.Play();
    
                }
    
            }

     public class MusicInfo
        {
            public string Name { get; set; }
        }

    One more thing you need note is that the above code snippet just support querying music in Music library folder. If you need support other folder, you should copy that music file in local storage first.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.



    Wednesday, January 21, 2015 3:24 AM
    Moderator

All replies

  • Hi Kranthi,

    Yes this can be done and the sample code is available in MSDN forum.

    Here is the link to that code:

    https://social.msdn.microsoft.com/Forums/windowsapps/en-US/ac8953a7-556c-49b6-908e-8cd9398de829/binding-a-mediaelement-in-a-listview-?forum=winappswithcsharp

    I hope this will help you.

    Please 'propose as answer' if it helped you, also 'vote helpful' if you like this reply.


    Friday, January 9, 2015 3:19 PM
  • i am trying to play an audio file but failed, my code is below, please resolve my problem(this code is for listview tapped event)

    mymedia.Source = new System.Uri(listf.SelectedValuePath);

    mymedia.Play();

    i am getting uri as "NULL"

    Saturday, January 10, 2015 11:21 AM
  • mediaplayer.Source = new Uri(item.link, UriKind.RelativeOrAbsolute);

    or

    mediaplayer.Source = new Uri(listbox.selecteditems, UriKind.RelativeOrAbsolute);

    Saturday, January 10, 2015 2:31 PM
  • your code is not working herney, i am getting error.
    Saturday, January 10, 2015 3:14 PM
  • Hi Kranthi88,

    Usually, we set object collections to ListView ItemsSource property. When you get object from SelectedItem property of ListView, you need to covert that SelectedItem object to collection element object. Code looks like the following.

    System.Collections.ObjectModel.ObservableCollection<Media> medias = new System.Collections.ObjectModel.ObservableCollection<Media>();
    
                listview1.ItemsSource = medias;
    
                Media media = listview1.SelectedItem as Media;
    
                mymedia.Source = new System.Uri(media.Url);
    

    Please edit the above code snippet to meet your requirement. If not work, please post a repro project and share a link here.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Wednesday, January 14, 2015 9:29 AM
    Moderator
  • thanx for the solution wong, but still i am getting error at Media snippet, though i had declared required namespaces still error is shown at Media as type or namespace could not be found, please help me.

    Wednesday, January 14, 2015 12:23 PM
  • Hi Kranthi88,

    Media class is the element of ListView ItemsSource collection. You should change it to your own class.

    Please post a repro project if you don’t know how to do. Use your OneDrive and share a link here.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Thursday, January 15, 2015 1:20 AM
    Moderator
  • hii wong, here is the link to my project 

    https://app.box.com/s/koi7hqs40b830u0fbxtg278rvs809tg4

    Friday, January 16, 2015 4:12 AM
  • here is the link to my project on onedrive my project
    Monday, January 19, 2015 1:10 PM
  • Hi Kranthi88,

    According to your code snippet, I modify it and it can work now. You can see the following code, copy and paste it into your project.

     

    private async void loadall(object sender, RoutedEventArgs e)
    
            {
    
                var folderPicker = new FolderPicker();
    
                folderPicker.FileTypeFilter.Add(".mp3");
    
                var folder = await folderPicker.PickSingleFolderAsync();
    
                var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".mp3" });
    
                queryOptions.FolderDepth = FolderDepth.Deep;
    
    
                var query = folder.CreateItemQueryWithOptions(queryOptions);
    
                var items = await query.GetItemsAsync();
    
                //if user select other folder on drive, you should copy that file in local storage to make sure you can 
    
                //find the music when playing
    
                var files = items.OfType<IStorageFile>().Select(s => new MusicInfo() { Name = s.Name }).ToList();
    
                listf.ItemsSource = files;
    
              
    
                //trying  to play list song
    
               
    
            }
    
    
            private async void playyyy(object sender, ItemClickEventArgs e)
    
            {
    
                MusicInfo music = e.ClickedItem as MusicInfo;
    
                if (music != null)
    
                {
    
                    Debug.WriteLine(music.Name);
    
                    var musiclibrary = Windows.Storage.KnownFolders.MusicLibrary;
    
                    var file =await musiclibrary.GetFileAsync(music.Name);
    
                    MusicProperties musicProp = await file.Properties.GetMusicPropertiesAsync();
    
                    myblock.Text = musicProp.Title;
    
                    var stream = await file.OpenAsync(FileAccessMode.Read);
    
                    mymedia.SetSource(stream, file.ContentType);
    
                    mymedia.Play();
    
                }
    
            }

     public class MusicInfo
        {
            public string Name { get; set; }
        }

    One more thing you need note is that the above code snippet just support querying music in Music library folder. If you need support other folder, you should copy that music file in local storage first.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.



    Wednesday, January 21, 2015 3:24 AM
    Moderator
  • thanx wong, as stated the code is working fine with music library but it failed to work when files are stored in a folder in music library. 
    Friday, January 23, 2015 8:43 AM
  • Yes, I just pick up music file from Music library root folder. If you need to pick up files from other folder in that folder, make sure you get into the correct folder before calling musiclibrary.GetFileAsync method.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Friday, January 23, 2015 9:27 AM
    Moderator
  • can u please show how to copy music files from other folder & sub folders in other location to local storage ?
    Sunday, February 22, 2015 2:56 PM