RadMediaPlayer System.NullReferenceException : Object reference not set to an instance of an object

2 posts, 0 answers
  1. Catherine
    Catherine avatar
    21 posts
    Member since:
    Mar 2009

    Posted 30 May 2011 Link to this post

    Hi,

    I got an error with the RadMediaPlay.

    Here is the Stacktrace :
       à Telerik.Windows.Controls.RadMediaPlayer.HandleCanExecuteJumpCommands(RadMediaPlayer player, ICommand command, Object parameter)
       à Telerik.Windows.Controls.RadMediaPlayer.MediaCommandCanExecute(Object sender, CanExecuteRoutedEventArgs e)
       à Telerik.Windows.Controls.CanExecuteRoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object target)
       à Telerik.Windows.RadRoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       à Telerik.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RadRoutedEventArgs routedEventArgs)
       à Telerik.Windows.RouteItem.InvokeHandler(RadRoutedEventArgs routedEventArgs)
       à Telerik.Windows.EventRoute.InvokeHandlersImpl(Object source, RadRoutedEventArgs args, Boolean raisedAgain)
       à Telerik.Windows.EventRoute.InvokeHandlers(Object source, RadRoutedEventArgs args)
       à Telerik.Windows.RadRoutedEventHelper.RaiseEvent(DependencyObject element, RadRoutedEventArgs args)
       à Telerik.Windows.DependencyObjectExtensions.RaiseEvent(DependencyObject element, RadRoutedEventArgs e)
       à Telerik.Windows.Controls.RoutedCommand.CanExecute(Object parameter, UIElement target)
       à Telerik.Windows.Controls.RadButton.CanExecuteApply()
       à Telerik.Windows.Controls.RadButton.CanExecuteChanged(Object sender, EventArgs e)
       à Telerik.Windows.Controls.WeakEventHandlerManager.CallHandler(Object sender, EventHandler eventHandler)
       à Telerik.Windows.Controls.WeakEventHandlerManager.CallWeakReferenceHandlers(Object sender, List`1 handlers)
       à Telerik.Windows.Controls.CommandManager.RaiseRequerySuggested(Object obj)
       à Telerik.Windows.Controls.CommandManager.<RaiseRequerySuggested>b__5()

    My scenario is to open player in a window, the playlist is loaded dynamically.
    First, I created a usercontrol that uses the RadMediaPlayer. His name is PlayerControl.
    Here is a part of the UserControl :
    <telerik:RadMediaPlayer x:Name="player" 
                                    Loaded="PlayerLoaded" 
                                    Unloaded="PlayerUnloaded"
                                    MediaOpened="PlayerMediaOpened"
                                    MediaEnded="PlayerMediaEnded"
                                    Header="{Binding RelativeSource={RelativeSource Self}, Path=MediaElement}"
                                    ItemsSource="{Binding Items}"
                                    AutoPlay="{Binding AutoPlay}"
                                    IsAutoPlaylistAdvance="{Binding IsAutoPlaylistAdvance}"
                                    IsAutoReverse="{Binding IsAutoReverse}"
                                    IsPlaylistVisible="{Binding IsPlaylistVisible}"
                                    Height="{Binding VideoHeight}"
                                    Width="{Binding VideoWidth}"
                                    SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"
                                    SelectedItem="{Binding SelectedMediaItem, Mode=TwoWay}"
                                    IsVideoPanelHidden="{Binding IsVideoPanelHidden}"
                                    IsVideoPanelPinned="{Binding IsVideoPanelPinned }"
                                    HeaderTemplate="{Binding HeaderTemplate}">
                  
                <telerik:RadMediaPlayer.ItemTemplateSelector>
                    <Template:MediaTemplateSelector>
                        <Template:MediaTemplateSelector.MediaItemTemplate>
                            <DataTemplate>
                                <telerik:ContainerBinding.ContainerBindings>
                                    <telerik:ContainerBindingCollection>
                                        <telerik:ContainerBinding PropertyName="Source" Binding="{Binding Source}" />
                                        <telerik:ContainerBinding PropertyName="IsSelected" Binding="{Binding IsSelected,Mode=TwoWay}" />
                                        <telerik:ContainerBinding PropertyName="Tag" Binding="{Binding}" />
                                    </telerik:ContainerBindingCollection>
                                </telerik:ContainerBinding.ContainerBindings>
                                <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
                                    <Image Source="{Binding Image}" Stretch="Uniform" Margin="4" />
                                    <TextBlock Text="{Binding Title}" VerticalAlignment="Center" Margin="4" />
                                </StackPanel>
                            </DataTemplate>
                        </Template:MediaTemplateSelector.MediaItemTemplate>
                        <Template:MediaTemplateSelector.VideoMediaItemTemplate>
                            <DataTemplate>
                                <telerik:ContainerBinding.ContainerBindings>
                                    <telerik:ContainerBindingCollection>
                                        <telerik:ContainerBinding PropertyName="Source" Binding="{Binding Source}" />
                                        <telerik:ContainerBinding PropertyName="IsSelected" Binding="{Binding IsSelected,Mode=TwoWay}" />
                                        <telerik:ContainerBinding PropertyName="Tag" Binding="{Binding}" />
                                        <telerik:ContainerBinding PropertyName="IsStreamingSource" Binding="{Binding IsStreamingSource}" />
                                        <telerik:ContainerBinding PropertyName="VideoWidth" Binding="{Binding VideoWidth}" />
                                        <telerik:ContainerBinding PropertyName="VideoHeight" Binding="{Binding VideoHeight}" />
                                        <telerik:ContainerBinding PropertyName="VideoStretch" Binding="{Binding VideoStretch}" />
                                    </telerik:ContainerBindingCollection>
                                </telerik:ContainerBinding.ContainerBindings>
                                <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
                                    <Image Source="{Binding Image}" Stretch="Uniform" Margin="4" />
                                    <TextBlock Text="{Binding Title}" VerticalAlignment="Center" Margin="4" />
                                </StackPanel>
                            </DataTemplate>
                        </Template:MediaTemplateSelector.VideoMediaItemTemplate>
                    </Template:MediaTemplateSelector>
                </telerik:RadMediaPlayer.ItemTemplateSelector>
       
            </telerik:RadMediaPlayer>

    A ViewModel is used to set the RadMediaPlayer's properties .

    The ViewModel contains some features to add and remove MediaItems from the RadMediaPlayer.
    public ObservableCollection<MediaItem> Items { get; internal set; }
      
     public void AddMediaItem( MediaItem item )
     {
           Items.Add(item);
     }
      
    public void RemoveMediaItem( MediaItem item )
            {
                if (Items.Contains(item))
                {
                      Items.Remove(item);
                }
            }
      
    public void Clear()
            {
                Items.Clear();
            }

    Finally, I try to open the window that contains the usercontrol.
    It works for 3 or 4 times (this is random) then I got the error
    // Template declared as a resource
    <DataTemplate x:Key="PlayerVideoTemplate">
            <Grid>
                <Player:PlayerControl x:Name="PlayerControl" HorizontalAlignment="Left" Controller="{Binding Controller}"/>
            </Grid>
        </DataTemplate>
      
    // The code
      
    // ViewModel for the PlayControl usercontrol
    Controller = new PlayerController
                {
                    SelectedIndex = 0,
                    AutoPlay = true,
                    IsAutoPlaylistAdvance = true,
                    ShowCommandsPanel = true,
                    MediaEnded = LectureMediaTerminee,
                    MediaOpened = LectureMedia
                };
      
    // add  a video to the playlist             
    _playList.AddLast(new VideoMediaItem
                            {
                                Source = videoSource,
                                Title = SelectedAppointment.ElpTitreDiff,
                                VideoStretch = Stretch.Fill
                            });
      
    // add the first value
     Controller.AddMediaItem(_playList.First.Value);
      
    // diplay the window
     var window = new RadWindow
                        {
                            DataContext = this,
                            Content = ((DataTemplate)Application.Current.Resources["PlayerVideoTemplate"]).LoadContent(),
                            MaxWidth = 1024,
                            MaxHeight = 768,
                            Header = "Player vidéo",
                            WindowStartupLocation = WindowStartupLocation.CenterScreen
                        };
     window.Show();

    Can you help me.

    Best ragards,
    C.Leoni.
  2. Catherine
    Catherine avatar
    21 posts
    Member since:
    Mar 2009

    Posted 31 May 2011 Link to this post

    Hi again,

    I solved my problem.
    I created a single instance of the usercontrol and the window and this fixes the problem.
    Now it works.

    Best regards.
    C.Léoni.
  3. DevCraft banner
Back to Top