locked
MediaElement Control the infromation about currently playing media file audio or video RRS feed

  • Question

  • Hi All,


    I am new to windows store app development. I have started creating a media player using "MediaElement" control available in Visual Studio 2012.

    The Media player I have created is working fine with basic functionality like Play, Pause, Stop, Next, Back, Volume Up & Volume Down. And the list of the songs are also listed in the playlist. Everything is working fine so far. Audio and video files are playing well.

    The issue came up when I tried to add the "Full Windows" feature to the Video file. I want the "Full Windows" button be visible or should appear only when the currently playing media file is "Video file" .

    If the currently playing media file is any audio then the "Full windows" Button should not be visible.

    But I do not know how to check that the currently playing media file is audio or video.

    Any help would be highly appreciated.

    Thanks.

    Friday, January 3, 2014 8:14 AM

Answers

  • First of All, Wish You A Very Happy New Year.

    Thanks for the post it gave me some insights for solving the issue mentioned in my question. During implementing the solution you suggested me to follow I discovered the other way to do it. The following is the code snippet.

    I have added the code to the "SelectionChenged" Event of the ListBox which I have named as "Playlist".

    private async void Playlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                media.AudioStreamIndex = Playlist.SelectedIndex;
                StorageFile file = Playlist.SelectedItem as StorageFile;
                      
                    if (file != null)
                    {
                          if (file.FileType == ".mp3")      //if the file type is .mp3 then hide the full screen button
                          {
                                  btnFullScreenToggle.Visibility = Visibility.Collapsed;
                          }
                          else // otherwise show the full screen button
                          {
                                 btnFullScreenToggle.Visibility = Visibility.Visible;
                           }

                        // Open the selected file and set it as the MediaElement's source
                        IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
                        media.SetSource(stream, file.ContentType);
                    }
                
                
            }

    • Marked as answer by Anne Jing Tuesday, January 7, 2014 6:06 AM
    Saturday, January 4, 2014 7:48 AM

All replies

  • It really depends on your model: Do you use data binding? The easiest way might be to bind to the SelectedItem property of your playlist and use a Converter:

    <AppBarButton Label="Fullscreen" IsEnabled="{Binding ElementName=playlistListBox, Path=SelectedItem.Type, Converter="{StaticResource ContextualConverter}, ConverterParameter=fullscreenIsEnabled}" />

    And the Converter:

    using System;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Data;
    
    ...
    
        public class SampleConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string culture)
            {
                switch (parameter as string)
                {
                     case "fullscreenIsEnabled": return ((value as MediaType) == MediaType.Video);//return true for video files
                }
                return null;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string culture)
            {
                throw new NotImplementedException();
            }
        }

    happy new year!


    Life is unsure - always eat the dessert first!

    Friday, January 3, 2014 10:34 AM
  • First of All, Wish You A Very Happy New Year.

    Thanks for the post it gave me some insights for solving the issue mentioned in my question. During implementing the solution you suggested me to follow I discovered the other way to do it. The following is the code snippet.

    I have added the code to the "SelectionChenged" Event of the ListBox which I have named as "Playlist".

    private async void Playlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                media.AudioStreamIndex = Playlist.SelectedIndex;
                StorageFile file = Playlist.SelectedItem as StorageFile;
                      
                    if (file != null)
                    {
                          if (file.FileType == ".mp3")      //if the file type is .mp3 then hide the full screen button
                          {
                                  btnFullScreenToggle.Visibility = Visibility.Collapsed;
                          }
                          else // otherwise show the full screen button
                          {
                                 btnFullScreenToggle.Visibility = Visibility.Visible;
                           }

                        // Open the selected file and set it as the MediaElement's source
                        IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
                        media.SetSource(stream, file.ContentType);
                    }
                
                
            }

    • Marked as answer by Anne Jing Tuesday, January 7, 2014 6:06 AM
    Saturday, January 4, 2014 7:48 AM