Adding RadMediaPlayer events programmatically

4 posts, 0 answers
  1. Michael Draisey
    Michael Draisey avatar
    6 posts
    Member since:
    Mar 2005

    Posted 29 Apr 2009 Link to this post

    Hi,

    I'm creating my player programmatically and having problems getting at events due to this. Do you have an examples of programmatically creating a player and then registering events. Also a list of events and signatures would be great! I'm using vb.net but c# examples would be fine.

    Thanks,

    Michael
  2. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 29 Apr 2009 Link to this post

    Hi Michael,

    Strangely, all our examples have the MediaPlayer defined in Xaml, but it should make no difference how  you create it.

    Here are the MediaPlayer specific events and how you register for them in C#:
    using System.Windows;  
    using System.Windows.Controls;  
    using Telerik.Windows;  
    using Telerik.Windows.Controls;  
     
    namespace RoutedEventsSilverlight  
    {  
        public partial class Page : UserControl  
        {  
            public Page()  
            {  
                InitializeComponent();  
     
                RadMediaPlayer mediaPlayer = new RadMediaPlayer();  
     
                //CLR Events, use them when you have access to the MP instance.  
                mediaPlayer.Loaded += new System.Windows.RoutedEventHandler(mediaPlayer_Loaded);  
                mediaPlayer.ChapterReached += new System.EventHandler<RadRoutedEventArgs>(mediaPlayer_ChapterReached);  
                mediaPlayer.CurrentStateChanged += new System.EventHandler<RadRoutedEventArgs>(mediaPlayer_CurrentStateChanged);  
                mediaPlayer.DownloadProgressChanged += new System.EventHandler<RadRoutedEventArgs>(mediaPlayer_DownloadProgressChanged);  
                mediaPlayer.MediaEnded += new System.EventHandler<RadRoutedEventArgs>(mediaPlayer_MediaEnded);  
                mediaPlayer.MediaOpened += new System.EventHandler<RadRoutedEventArgs>(mediaPlayer_MediaOpened);  
     
                //Roued events, can be used when the player is created as a part of a template or   
                //is otherwise inaccessible:  
                this.AddHandler(RadMediaPlayer.ChapterReachedEvent, new RoutedEventHandler(mediaPlayer_ChapterReached));  
                this.AddHandler(RadMediaPlayer.CurrentStateChangedEvent, new RoutedEventHandler(mediaPlayer_CurrentStateChanged));  
                this.AddHandler(RadMediaPlayer.DownloadProgressChangedEvent, new RoutedEventHandler(mediaPlayer_DownloadProgressChanged));  
                this.AddHandler(RadMediaPlayer.MediaEndedEvent, new RoutedEventHandler(mediaPlayer_MediaEnded));  
                this.AddHandler(RadMediaPlayer.MediaOpenedEvent, new RoutedEventHandler(mediaPlayer_MediaOpened));  
     
                //Add the MP to the visual tree:  
                LayoutRoot.Children.Add(mediaPlayer);  
            }  
     
            void mediaPlayer_MediaOpened(object sender, RoutedEventArgs e)  
            {  
            }  
     
            void mediaPlayer_MediaEnded(object sender, RoutedEventArgs e)  
            {  
            }  
     
            void mediaPlayer_DownloadProgressChanged(object sender, RoutedEventArgs e)  
            {  
            }  
     
            void mediaPlayer_CurrentStateChanged(object sender, RoutedEventArgs e)  
            {  
            }  
     
            void mediaPlayer_ChapterReached(object sender, RoutedEventArgs e)  
            {  
            }  
     
            void mediaPlayer_Loaded(object sender, System.Windows.RoutedEventArgs e)  
            {  
            }  
        }  
    }  
     

    And in VB:

    Imports System.Windows   
    Imports System.Windows.Controls   
    Imports Telerik.Windows   
    Imports Telerik.Windows.Controls   
     
    Namespace RoutedEventsSilverlight   
        Public Partial Class Page   
            Inherits UserControl   
            Public Sub New()   
                InitializeComponent()   
                  
                Dim mediaPlayer As New RadMediaPlayer()   
                  
                'CLR Events, use them when you have access to the MP instance.   
                AddHandler mediaPlayer.Loaded, AddressOf mediaPlayer_Loaded   
                AddHandler mediaPlayer.ChapterReached, AddressOf mediaPlayer_ChapterReached   
                AddHandler mediaPlayer.CurrentStateChanged, AddressOf mediaPlayer_CurrentStateChanged   
                AddHandler mediaPlayer.DownloadProgressChanged, AddressOf mediaPlayer_DownloadProgressChanged   
                AddHandler mediaPlayer.MediaEnded, AddressOf mediaPlayer_MediaEnded   
                AddHandler mediaPlayer.MediaOpened, AddressOf mediaPlayer_MediaOpened   
                  
                'Roued events, can be used when the player is created as a part of a template or   
                'is otherwise inaccessible:   
                Me.[AddHandler](RadMediaPlayer.ChapterReachedEvent, New RoutedEventHandler(mediaPlayer_ChapterReached))   
                Me.[AddHandler](RadMediaPlayer.CurrentStateChangedEvent, New RoutedEventHandler(mediaPlayer_CurrentStateChanged))   
                Me.[AddHandler](RadMediaPlayer.DownloadProgressChangedEvent, New RoutedEventHandler(mediaPlayer_DownloadProgressChanged))   
                Me.[AddHandler](RadMediaPlayer.MediaEndedEvent, New RoutedEventHandler(mediaPlayer_MediaEnded))   
                Me.[AddHandler](RadMediaPlayer.MediaOpenedEvent, New RoutedEventHandler(mediaPlayer_MediaOpened))   
                  
                'Add the MP to the visual tree:   
                LayoutRoot.Children.Add(mediaPlayer)   
            End Sub   
              
            Private Sub mediaPlayer_MediaOpened(ByVal sender As ObjectByVal e As RoutedEventArgs)   
            End Sub   
              
            Private Sub mediaPlayer_MediaEnded(ByVal sender As ObjectByVal e As RoutedEventArgs)   
            End Sub   
              
            Private Sub mediaPlayer_DownloadProgressChanged(ByVal sender As ObjectByVal e As RoutedEventArgs)   
            End Sub   
              
            Private Sub mediaPlayer_CurrentStateChanged(ByVal sender As ObjectByVal e As RoutedEventArgs)   
            End Sub   
              
            Private Sub mediaPlayer_ChapterReached(ByVal sender As ObjectByVal e As RoutedEventArgs)   
            End Sub   
              
            Private Sub mediaPlayer_Loaded(ByVal sender As ObjectByVal e As System.Windows.RoutedEventArgs)   
            End Sub   
        End Class   
    End Namespace  

    Hopefully this is what you need,

    All the best,
    Miroslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
  4. Michael Draisey
    Michael Draisey avatar
    6 posts
    Member since:
    Mar 2005

    Posted 29 Apr 2009 Link to this post

    Hi Miroslav,

    That's great thanks. What i'm trying to achive is to have the 'Previous' and 'Next' buttons move the position of the video move by 10 second each way. The events that i need to attach to therefore are the Next and Previous buttons. What are the events for these buttons?

    Thanks for the code, really helpful. I did try defining the RadMediaPlayer in XAML but i got validation errors, even for a basic declaration.

    Thanks,

    Michael
  5. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 01 May 2009 Link to this post

    Hi Michael,

    Sorry for the delayed reply!

    Can you share more details on the error you get when you try to define the MediaPlayer in Xaml? Is this in some kind of View which is loaded dynamically? Can you send us a sample Xaml that crashed for you? I am asking for all this because the MediaPlayer should just work when defined in xaml.

    Unfortunately there are no events that you can handle to implement the skip-forward and skip-backward feature. Currently the buttons will only go to the previous and next chapters. Even if you were able to get to the Click events on the buttons, they would still switch the chapters.

    As a workaround I can suggest to add chapters to the MediaItems.

    We are planning to add commands to the MediaPlayer that will let you customize the behavior of the buttons, but unfortunately I do not know when they will be implemented.

    Greetings,
    Miroslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top