Exception with RadMediaPlay

5 posts, 1 answers
  1. Barry
    Barry avatar
    11 posts
    Member since:
    Dec 2008

    Posted 16 Apr 2012 Link to this post

    Hi,
    I am receiving the following exception when I try to load a RadmediaPlayer on control load:

       at Telerik.Windows.Controls.ItemsControlExtensions.<GetContainers>d__0`1.MoveNext()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
       at Telerik.Windows.Controls.RadMediaPlayer.SetTickMarksOnTimeSlider()
       at Telerik.Windows.Controls.RadMediaPlayer.StartInternal()
       at Telerik.Windows.Controls.RadMediaPlayer.MediaElement_MediaOpened(Object sender, RoutedEventArgs e)
       at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

    The code that loads the control looks like this:

                         byte[] sng = SoundTrackLoad.OrderBy(a => a.OrderNbr).ToArray()[0].TrackBinary;
                         radMediaPlayer1.MediaElement.Stop();
                         radMediaPlayer1.MediaElement.AutoPlay = false;
                            radMediaPlayer1.MediaElement.SetSource(
                                new MemoryStream(sng));
                            radMediaPlayer1.SelectedIndex = 0;

    When the control is finally rendered the play button on the RMP does not work. Any help would be appreciated,
    thanks
    Baz
  2. Barry
    Barry avatar
    11 posts
    Member since:
    Dec 2008

    Posted 16 Apr 2012 Link to this post

    So,

    looks like this exact stack trace was logged just over 2 years ago by Helen under a different thread Loading RMP from local file system.

    How frustrating. This could be such a useful control but it seems to be compromised by a few simple bugs.

    Seems the loaded event is being called a couple of times. The first time the MediaElement is still null, the second time the MediaElement is there but the control still hasn't full initialized.

    The interesting thing is that if I don't set the control to AutoPlay=false and MediaPlayer.Stop prior to setting the Source to a stream value it will work.

    thanks in advance for any help.
    Baz
  3. DevCraft banner
  4. Miro Miroslavov
    Admin
    Miro Miroslavov avatar
    588 posts

    Posted 18 Apr 2012 Link to this post

    Hi Barry,

     In order to use the SelectedIndex property, you need first to define some Items (RadMediaItem) which are children of the RadMediaPlayer. So the right approach here is to define an item, sets its properties (Source and so on) and then set the player SelectedIndex = 0 - this will select and play the first video. 
    On a side note is not a good idea to manipulate the internal MediaElement that plays the video, but use the items' properties. 
    Please let us know if you have further problems. 

    Kind regards,
    Miro Miroslavov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  5. Barry
    Barry avatar
    11 posts
    Member since:
    Dec 2008

    Posted 18 Apr 2012 Link to this post

    Hi Miro,

    thanks for the response. I have actually removed the selectedIndex setting because I am actually setting the MediaElement directly from a byte stream from a database. I don't have Uri's that I can set up as items.

                         byte[] sng = SoundTrackLoad.OrderBy(a => a.OrderNbr).ToArray()[0].TrackBinary;
                         radMediaPlayer1.MediaElement.Stop();
                         radMediaPlayer1.MediaElement.AutoPlay = false;

                         radMediaPlayer1.MediaElement.SetSource(
                                new MemoryStream(sng));


                            radMediaPlayer1.SelectedIndex = 0; (removed)

    If I put this code inside the RdamediaPlayers Loaded event the program crashes because the MediaElement is still null. This work around was suggested to Helen by Tina over 2 years ago. I can get it to work on a simple form it fails when the silverlight control is performing more complex tasks.

    I see that the bug fix in PIT is scheduled for Q2. Are you able to find out if this is still on track. Any other advice on loading from a database would be greatly appreciated,

    regards
    Baz

  6. Answer
    Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 23 Apr 2012 Link to this post

    Hello Barry,

    The RadMediPlayer.MediaElement is initialized only after the control Template is fully initialized and in the Loaded event, the ControlTemplate's initialization hasn't completed. This is why the MediaElement is null.

    In order to make sure that the MediaElement property isn't null, you can handle the SizeChanged event of the RadMediaPlayer as it will indicate that the template of the control is already initialized. Once you implement your MediaElement.SetSource() logic, you can detach from the event:

    private void xMediaPlayer_SizeChanged(object sender, SizeChangedEventArgs e)
    {
            if (fileStream != null)
            {
                xMediaPlayer.MediaElement.Stop();
                xMediaPlayer.MediaElement.AutoPlay = false;
                xMediaPlayer.MediaElement.SetSource(fileStream);
                xMediaPlayer.SizeChanged -= xMediaPlayer_SizeChanged;
            }
    }

    As for the exception, it is caused by a bug in the MediaPlayer's implementation as it expects a selected MediaItem while initializing a media - when there isn't a selected item, an exception is raised. And we scheduled the fix for this issue for the Q2 2012 release. However this fix will only get over the exception when there is no selected item in the control. At the moment we aren't be able to provide a new way for setting the RadMediaPlayer media content from a stream.

    And as you have a database with data, we usually recommend keeping URLs of videos in the database, while hosting the videos on a server, so that the RadMediaPlayer can display them in its MediaItems following the approach in this demo solution.

    However using your approach, even though I am not sure which workaround you tried, on my side as long as I make sure that the MediaPlayer.SelectedItem is set, I get no exceptions. I attached my sample project, but if it doesn't work for you, please modify it to better illustrate your scenario so that we can look for a more suitable solution for you. And if possible I'd really recommend implementing the databinding approach described above.

    Regards,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top
DevCraft banner