locked
pick multiple files and use it how??

    Question

  • I tried to play multiple files in a media player but it's only playing a single file .. How to play multiple files serielwise. I also created a list box and i want to play from their also by clicking. 

     

    IReadOnlyList<StorageFile> files = await filepicker.PickMultipleFilesAsync();                  

                if (files.Count > 0)                {                                   

      StringBuilder output = new StringBuilder("\n");                 

      // Application now has read/write access to the picked file(s)     

                  foreach (StorageFile file in files)                 

      {                                            

      slider.Value = 0;                   

        vs.Value = 50;                   

        m.Volume = vs.Value;                     

                             ttt.Items.Add(string.Format(file.ContentType)); 

                                               

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

                          m.SetSource(stream,file.ContentType); 

                                                  if (ttt.SelectedItem != null)   

                        {                                                   

                                 m.Play();                 

                                                               

                               }



    Saturday, June 07, 2014 5:45 AM

Answers

  • Here's a quick & not particularly tested class to demonstrate stepping through a list of StorageFiles as CurrentStateChanged fires.

    public sealed partial class MyPlayList
        {
     
            public MyPlayList(MediaElement media, IReadOnlyList<StorageFile> files)
            {
                PlayListCurrent = 0;
                PlayListMediaElement = media;
                PlayListFiles = files;
                media.CurrentStateChanged += PlayList_CurrentStateChanged;
                media.AutoPlay = true;
            }
    
            public async Task PlayAsync()
            {
                if (PlayListCurrent < PlayListFiles.Count)
                {
                    StorageFile mediaFile = PlayListFiles[PlayListCurrent];
                    Debug.WriteLine("PlayFile {0}", mediaFile.Name);
                    var stream = await mediaFile.OpenAsync(FileAccessMode.Read);
                    using (stream)
                    {
                        PlayListMediaElement.SetSource(stream, mediaFile.ContentType);
                    }
                    PlayListCurrent++;
                }
                else
                {
                    // Clear references
                    PlayListMediaElement.CurrentStateChanged -= PlayList_CurrentStateChanged;
                    PlayListMediaElement = null;
                    PlayListFiles = null;
                }
            }
            
            IReadOnlyList<StorageFile> PlayListFiles { get; set; }
            MediaElement PlayListMediaElement { get; set; }
            int PlayListCurrent { get; set; }
            async void PlayList_CurrentStateChanged(object sender, RoutedEventArgs e)
            {
                Debug.WriteLine("{1}/{2} CurrentStateChanged to {0}", PlayListMediaElement.CurrentState, PlayListCurrent, PlayListFiles.Count);
                if (PlayListMediaElement.CurrentState == MediaElementState.Paused)
                {
                    // We paused, so find the next item and play it.
                    // We could be a bit smarter and look at the position to allow pausing in the middle
                    await PlayAsync();
                }
            }
        }

    Tuesday, June 17, 2014 3:41 AM
    Owner

All replies

  • The MediaElement can play only a single file at a time. To play others you can listen for the CurrentStateChanged property and load a new file when the current one ends.

    --Rob

    Saturday, June 07, 2014 8:05 PM
    Owner
  • Ok sorry Rob, But i have a serious problem. I tried CurrentState Changed but still no changed. it reads my multiple files but not playing. look at my new code:::

     private async void OpenFile(object sender, RoutedEventArgs e)
            {
                Button b = sender as Button;
                if (b != null)
                {
                    var CurrentSnappedState = Windows.UI.ViewManagement.ApplicationView.Value;
                    if (CurrentSnappedState == Windows.UI.ViewManagement.ApplicationViewState.Snapped && !Windows.UI.ViewManagement.ApplicationView.TryUnsnap())
                    {
                        return;
                    }

                    FileOpenPicker filepicker = new FileOpenPicker();
                    filepicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
                    filepicker.FileTypeFilter.Add(".mp4");
                    filepicker.FileTypeFilter.Add(".wmv");
                    filepicker.FileTypeFilter.Add(".mp3");
                    filepicker.FileTypeFilter.Add(".flv");
                    filepicker.FileTypeFilter.Add(".avi");
                    filepicker.FileTypeFilter.Add(".mkv");
                    filepicker.FileTypeFilter.Add(".vob");
                    filepicker.FileTypeFilter.Add(".mov");
                    filepicker.FileTypeFilter.Add(".mpg");
                    filepicker.FileTypeFilter.Add(".3gp");
                    filepicker.FileTypeFilter.Add(".rmvb");
                    filepicker.FileTypeFilter.Add(".gif");
                    filepicker.FileTypeFilter.Add(".wav");
                    filepicker.FileTypeFilter.Add(".ogg");
                    filepicker.ViewMode = PickerViewMode.Thumbnail;
                    slider.IsEnabled = true;

                    IReadOnlyList<StorageFile> files = await filepicker.PickMultipleFilesAsync();
                    var storage = Windows.Storage.ApplicationData.Current.LocalFolder;
                    if (files.Count > 0)
                    {
                      
                        StringBuilder output = new StringBuilder("\n");
                        // Application now has read/write access to the picked file(s)
                        foreach (StorageFile file in files)
                        {
                            int i = 0;
                           
                          
                            slider.Value = 0;
                            vs.Value = 50;
                            m.Volume = vs.Value;
                            var stream = await file.OpenAsync(FileAccessMode.Read);

                           
                            var copiedFile = await file.CopyAsync(storage, file.Name, NameCollisionOption.GenerateUniqueName);
                            ttt.Items.Add(string.Format("ms-appdata:///Local/{0}", copiedFile.Name));
                            m.SetSource(stream, "ms-appdata:///Local/{0}"+copiedFile.ContentType);
                            if (ttt.SelectedItem != null)
                            {
                               
                                m.Play();
                               
                            }
                            
                        }
                        ttt.Items.Add(output);
                    }
                    else
                    {
                        ttt.Items.Add("Error!!!");
                    }


    Nitikesh Pattnaik


    Tuesday, June 10, 2014 3:04 AM
  • Ok sorry Rob, But i have a serious problem. I tried CurrentState Changed but still no changed. it reads my multiple files but not playing. look at my new code:::

     private async void OpenFile(object sender, RoutedEventArgs e)
            {
                Button b = sender as Button;
                if (b != null)
                {
                    var CurrentSnappedState = Windows.UI.ViewManagement.ApplicationView.Value;
                    if (CurrentSnappedState == Windows.UI.ViewManagement.ApplicationViewState.Snapped && !Windows.UI.ViewManagement.ApplicationView.TryUnsnap())
                    {
                        return;
                    }

                    FileOpenPicker filepicker = new FileOpenPicker();
                    filepicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
                    filepicker.FileTypeFilter.Add(".mp4");
                    filepicker.FileTypeFilter.Add(".wmv");
                    filepicker.FileTypeFilter.Add(".mp3");
                    filepicker.FileTypeFilter.Add(".flv");
                    filepicker.FileTypeFilter.Add(".avi");
                    filepicker.FileTypeFilter.Add(".mkv");
                    filepicker.FileTypeFilter.Add(".vob");
                    filepicker.FileTypeFilter.Add(".mov");
                    filepicker.FileTypeFilter.Add(".mpg");
                    filepicker.FileTypeFilter.Add(".3gp");
                    filepicker.FileTypeFilter.Add(".rmvb");
                    filepicker.FileTypeFilter.Add(".gif");
                    filepicker.FileTypeFilter.Add(".wav");
                    filepicker.FileTypeFilter.Add(".ogg");
                    filepicker.ViewMode = PickerViewMode.Thumbnail;
                    slider.IsEnabled = true;

                    IReadOnlyList<StorageFile> files = await filepicker.PickMultipleFilesAsync();
                    var storage = Windows.Storage.ApplicationData.Current.LocalFolder;
                    if (files.Count > 0)
                    {
                      
                        StringBuilder output = new StringBuilder("\n");
                        // Application now has read/write access to the picked file(s)
                        foreach (StorageFile file in files)
                        {
                            int i = 0;
                           
                          
                            slider.Value = 0;
                            vs.Value = 50;
                            m.Volume = vs.Value;
                            var stream = await file.OpenAsync(FileAccessMode.Read);

                           
                            var copiedFile = await file.CopyAsync(storage, file.Name, NameCollisionOption.GenerateUniqueName);
                            ttt.Items.Add(string.Format("ms-appdata:///Local/{0}", copiedFile.Name));
                            m.SetSource(stream, "ms-appdata:///Local/{0}"+copiedFile.ContentType);
                            if (ttt.SelectedItem != null)
                            {
                               
                                m.Play();
                               
                            }
                            
                        }
                        ttt.Items.Add(output);
                    }
                    else
                    {
                        ttt.Items.Add("Error!!!");
                    }


    Nitikesh Pattnaik




    Nitikesh Pattnaik

    Tuesday, June 10, 2014 3:11 AM
  • I don't see where you are handling CurrentStateChanged in your code, so I don't know what it is doing wrong. I'm also not sure why you're copying the files. You should hold onto the StorageFiles returned from the FileOpenPicker. When CurrentStateChanged reports that the MediaElement has finished one song you can then get the next one from your StorageFiles list and start playing that one.

    For a more advanced version you could use two MediaElements, watch the position, and cross-fade by starting the second song just before the first one finishes.

    --Rob

    Thursday, June 12, 2014 3:35 AM
    Owner
  • How can i use the storage file list and media currentstate changed..nd it shows me error when i used media current state changed. actually m a beginner in this . so plz help me

    IReadOnlyList<StorageFile> files = await filepicker.PickMultipleFilesAsync();

                    if (files.Count > 0)
                    {

                        StringBuilder output = new StringBuilder("\n");
                        // Application now has read/write access to the picked file(s)
                        foreach (StorageFile file in files)
                        {

                            slider.Value = 0;
                            vs.Value = 50;
                            m.Volume = vs.Value;

                            ttt.Items.Add(file.ContentType);

                            var stream = await file.OpenAsync(FileAccessMode.Read);
                            m.SetSource(stream,ttt.SelectedItem.ToString());

     m.CurrentStateChanged += (Object s, RoutedEventArgs args) =>
                            {
                                if (m.CurrentState == MediaElementState.Playing)
                                {
                                    
                                }
                                else
                                {
                                  
                                }

                            };


                            if (ttt.SelectedItem != null)
                            {

                                m.Play();



                            }

                                                                                                                                                                                                                                                                   

    Nitikesh Pattnaik


    Monday, June 16, 2014 3:18 PM
  • set autoplay property to true. but why are you setting the mediaelement source in a loop? mediaelement will only play one stream. not all streams in the same time

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, June 16, 2014 4:57 PM
  • I already set my autoplay property to true. and also my video's playing but only 2 files then later it only plays 2nd video file. plz help


    Nitikesh Pattnaik


    Monday, June 16, 2014 5:20 PM
  • IReadOnlyList<StorageFile> files = await filepicker.PickMultipleFilesAsync();
                   
                    if (files.Count > 0)
                    {
                      
                        StringBuilder output = new StringBuilder("\n");
                        // Application now has read/write access to the picked file(s)
                        foreach (StorageFile file in files)
                        {
                           
                            slider.Value = 0;
                            vs.Value = 50;
                            m.Volume = vs.Value;
                           
                            ttt.Items.Add(file.ContentType);
                            
                            var stream = await file.OpenAsync(FileAccessMode.Read);
                            m.SetSource(stream,file.ContentType);
                           
                           m.CurrentStateChanged += (Object s, RoutedEventArgs args) =>
                            {
                              if (m.CurrentState == MediaElementState.Stopped)
                                {
                                  
                                    m.SetSource(stream,ttt.SelectedItem.ToString());
                                    m.Play();
                                }
                                else
                                {
                                  
                                }

                            };
                            if (ttt.SelectedItem != null)
                            {
                                                         
                                m.Play();

                                
                               
                            }

    Nitikesh Pattnaik

    Monday, June 16, 2014 5:37 PM
  • Here's a quick & not particularly tested class to demonstrate stepping through a list of StorageFiles as CurrentStateChanged fires.

    public sealed partial class MyPlayList
        {
     
            public MyPlayList(MediaElement media, IReadOnlyList<StorageFile> files)
            {
                PlayListCurrent = 0;
                PlayListMediaElement = media;
                PlayListFiles = files;
                media.CurrentStateChanged += PlayList_CurrentStateChanged;
                media.AutoPlay = true;
            }
    
            public async Task PlayAsync()
            {
                if (PlayListCurrent < PlayListFiles.Count)
                {
                    StorageFile mediaFile = PlayListFiles[PlayListCurrent];
                    Debug.WriteLine("PlayFile {0}", mediaFile.Name);
                    var stream = await mediaFile.OpenAsync(FileAccessMode.Read);
                    using (stream)
                    {
                        PlayListMediaElement.SetSource(stream, mediaFile.ContentType);
                    }
                    PlayListCurrent++;
                }
                else
                {
                    // Clear references
                    PlayListMediaElement.CurrentStateChanged -= PlayList_CurrentStateChanged;
                    PlayListMediaElement = null;
                    PlayListFiles = null;
                }
            }
            
            IReadOnlyList<StorageFile> PlayListFiles { get; set; }
            MediaElement PlayListMediaElement { get; set; }
            int PlayListCurrent { get; set; }
            async void PlayList_CurrentStateChanged(object sender, RoutedEventArgs e)
            {
                Debug.WriteLine("{1}/{2} CurrentStateChanged to {0}", PlayListMediaElement.CurrentState, PlayListCurrent, PlayListFiles.Count);
                if (PlayListMediaElement.CurrentState == MediaElementState.Paused)
                {
                    // We paused, so find the next item and play it.
                    // We could be a bit smarter and look at the position to allow pausing in the middle
                    await PlayAsync();
                }
            }
        }

    Tuesday, June 17, 2014 3:41 AM
    Owner
  • Sr,"filepicker.PickMultipleFilesAsync(); " what's filepicker???.
    Wednesday, June 18, 2014 6:27 AM