none
[UWP]Unable to bind mediaplayer element source property RRS feed

  • Question

  • i am trying to bind the Source property of MediaPlayerElement but i couldn't , the code i am using is below

    <MediaPlayerElement x:Name="myvidplayer" 
                                        Grid.Row="1"
                                        Margin="0,5,0,0"                             
                                        HorizontalAlignment="Left"
                                        AreTransportControlsEnabled="True"
                                        Source="{Binding SelectedVideo,Mode=OneWay, Converter={StaticResource source_convert}}"/>

    the converter using is 

            public object Convert(object value, Type targetType, object parameter, string language)
            {
                var obj = value as VideoModel;
                var element = new MediaPlayerElement();
    
                if (obj != null)
                {
                    element.Source = MediaSource.CreateFromStorageFile(obj.SelectedFile);
    
                    return element.Source;
                }
                else
                {
                    element.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/Videos/lofoten.mp4"));
                }
    
                return element.Source;
            }

    is there any thing i am missing ?


    Sunday, September 24, 2017 11:55 AM

Answers

  • Hi kranthi88,

    You need to return MediaSource directly instead of “MediaPlayerElement.Source”.

    Please refer to the following code sample:

    <MediaPlayerElement x:Name="myvidplayer" 
                                        Grid.Row="1"
                                        Margin="0,5,0,0"                             
                                        HorizontalAlignment="Left"
                                        AreTransportControlsEnabled="True"
                                        Source="{Binding SelectedVideo,Mode=OneWay, Converter={StaticResource source_convert}}"/>
    public object Convert(object value, Type targetType, object parameter, string language)
            {
                var obj = value as StorageFile;
                
                if (obj != null)
                {
                    
                    return MediaSource.CreateFromStorageFile(obj);
                }
                else
                {
                    return MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/Videos/lofoten.mp4"));
                }
            }
    public MainPage()
            {
                this.InitializeComponent();
                this.DataContext = this;
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected async override void OnNavigatedTo(NavigationEventArgs e)
            {
                base.OnNavigatedTo(e);
                SelectedVideo = await Package.Current.InstalledLocation.GetFileAsync("test.mp3");
                
            }
    
            private StorageFile _SelectedVideo;
            public StorageFile SelectedVideo
            {
                get { return _SelectedVideo; }
                set
                {
                    _SelectedVideo = value;
                    RaiseProperyChanged("SelectedVideo");
                }
            }
    
            private void RaiseProperyChanged(string name)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this,new PropertyChangedEventArgs(name));
                }
            }

    Best Regards,

    Xavier Eoro


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 25, 2017 5:41 AM
    Moderator

All replies

  • Hi kranthi88,

    You need to return MediaSource directly instead of “MediaPlayerElement.Source”.

    Please refer to the following code sample:

    <MediaPlayerElement x:Name="myvidplayer" 
                                        Grid.Row="1"
                                        Margin="0,5,0,0"                             
                                        HorizontalAlignment="Left"
                                        AreTransportControlsEnabled="True"
                                        Source="{Binding SelectedVideo,Mode=OneWay, Converter={StaticResource source_convert}}"/>
    public object Convert(object value, Type targetType, object parameter, string language)
            {
                var obj = value as StorageFile;
                
                if (obj != null)
                {
                    
                    return MediaSource.CreateFromStorageFile(obj);
                }
                else
                {
                    return MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/Videos/lofoten.mp4"));
                }
            }
    public MainPage()
            {
                this.InitializeComponent();
                this.DataContext = this;
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected async override void OnNavigatedTo(NavigationEventArgs e)
            {
                base.OnNavigatedTo(e);
                SelectedVideo = await Package.Current.InstalledLocation.GetFileAsync("test.mp3");
                
            }
    
            private StorageFile _SelectedVideo;
            public StorageFile SelectedVideo
            {
                get { return _SelectedVideo; }
                set
                {
                    _SelectedVideo = value;
                    RaiseProperyChanged("SelectedVideo");
                }
            }
    
            private void RaiseProperyChanged(string name)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this,new PropertyChangedEventArgs(name));
                }
            }

    Best Regards,

    Xavier Eoro


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 25, 2017 5:41 AM
    Moderator
  • Now it is working fine but why it failed when returned source property ? , can u explain ? 
    Monday, September 25, 2017 11:52 AM
  • Now it is working fine but why it failed when returned source property ? , can u explain ? 

    Thanks for reporting this issue. It's a known issue. The relevant team is investigating this issue. 

    Best Regards,

    Xavier Eoro


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 26, 2017 5:33 AM
    Moderator
  • Hello,

    Thanks for pointing out this issue and this is by design.

    Thanks,
    Amy Peng


    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, October 4, 2017 2:00 AM
    Owner