Building a Video Player in IronPython / Silverlight

DON'T post new tutorials here! Please use the "Pending Submissions" board so the staff can review them first.
Post Reply
User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Building a Video Player in IronPython / Silverlight

Post by maboroshi »

In my Past Article I discussed what Silverlight was and a rather impractical application of it using IronPython. In this article we go further and develop a Video Player. :-)

So open your favourite Python Code editor and lets begin

First some credits because I couldn't do this myself, there was so little information and converting from C# to IronPython was a difficult task.... at least for me

So cheers to Lundis of suck-o IRC who solved a problem for me and cheers to the people on the IronPython Mailing list

lets begin

Needed import statements

Code: Select all

from System import *
from System.Windows import Application, Thickness
from System.Windows.Controls import MediaElement
from System.Windows.Controls import Button, Orientation, StackPanel, Slider, Image, TextBlock
from System import Uri, UriKind   
from System.Windows.Media.Imaging import BitmapImage
from System.Windows.Threading import *
from System import TimeSpan
from System.Windows.Media import MediaElementState

Code: Select all


class Gui():
    def __init__(self):
        #Main StackPanel
        self.main = StackPanel()

        #Sub Stackpanel
        self.layer1 = StackPanel()
        self.layer1.Orientation = Orientation.Horizontal

        #Buttons
        self.stopB = Button(
            Width = 25,
            Height = 20,
            Content = Image(
                Source = BitmapImage(Uri("images/stop.jpg", UriKind.Relative))
                )
            )
        self.pauseB = Button(
            Width = 25,
            Height = 20,
            Content = Image(
                Source = BitmapImage(Uri("images/pause.jpg", UriKind.Relative))
                )
            )
        self.playB = Button(
            Width = 25,
            Height = 20,
            Content = Image(
                Source = BitmapImage(Uri("images/play.jpg", UriKind.Relative))
                )
            )
         
        # Assign Buttons to Functions
        self.stopB.Click += self.StopPlay
        self.pauseB.Click += self.PausePlay
        self.playB.Click += self.StartPlay


        #add a video slider
        self.myslide = Slider()
        self.myslide.Width = 190
        #set min/max of slider
        self.myslide.Minimum = 0
        self.myslide.Maximum = 380
        #status text of video
        self.txtvidstatus = TextBlock()
        self.txtvidstatus.Margin = Thickness(20)

        #add a volume slider
        self.volumeslide = Slider()
        self.volumeslide.Width = 100
        #set max value of volume slider
        self.volumeslide.Maximum = 50
        #assign volume slider to a function
        self.volumeslide.ValueChanged += self.vidvol
        
        # Add elements to sub stack panel
        self.layer1.Children.Add(self.stopB)
        self.layer1.Children.Add(self.pauseB)
        self.layer1.Children.Add(self.myslide)
        self.layer1.Children.Add(self.playB)
        self.layer1.Children.Add(self.volumeslide)
        self.layer1.Children.Add(self.txtvidstatus) 

        # create new sub stack panel
        self.layer2 = StackPanel()
        self.layer2.Orientation = Orientation.Vertical

        
        #create Media Element
        self.video = MediaElement()
        self.source = Uri("counter.wmv", UriKind.Relative)
        self.video.Volume = 0.4
        self.video.Source = self.source
        self.video.Width = 450
        self.video.Height = 400
        self.video.CurrentStateChanged += self.videoChanged
        self.video.AutoPlay = True

        #start threading
        self.timer = DispatcherTimer()
        self.timer.Interval = TimeSpan.FromMilliseconds(20)
        self.timer.Tick += self.MyTimeToTick
        self.timer.Start()

        # Add media Element to Sub Stack panel
        self.layer2.Children.Add(self.video)

        # Add sub stack panels to place holde stack panel
        self.main.Children.Add(self.layer1)
        self.main.Children.Add(self.layer2)

        # Load the UI
        Application.Current.RootVisual = self.main
If you followed my last tutorial you probably understand most of this. This code is pretty easy to follow just read the comments

Code: Select all

    def vidvol(self, s, e):
        #set video volume
        self.video.Volume = self.volumeslide.Value
        
    def MyTimeToTick(self, s, e):
        #function calculating time and position of video
        if self.video.NaturalDuration.TimeSpan.TotalSeconds > 0:
            self.txtvidstatus.Text = String.Format("{0:00}:{1:00}",
                                                self.video.Position.Minutes,
                                                self.video.Position.Seconds)
            
            self.myslide.Value = (self.video.Position.TotalSeconds / self.video.NaturalDuration.TimeSpan.TotalSeconds) * self.myslide.Maximum            

    def videoChanged(self, s, e):
        #get current state of video
        if self.video.CurrentState == MediaElementState.Playing:
            self.timer.Start()
        else:
            self.timer.Stop()
##
##       
    def StopPlay(self, s, e):
        # stop the video
        self.video.Stop()
       
    def PausePlay(self, s, e):
        #pause the video
        self.video.Pause()
       
    def StartPlay(self, s, e):
        #play the video
        self.video.Play()
These are our functions controlling the video, the video volume and the slider of our video application.

Code: Select all

# Run Class
gui = Gui()
gui
And this just starts the GUI

Well that is pretty much it not so hard, I made a big deal to comment my code so it is fairly straight forward :D

Hope you enjoy

*cheers

Maboroshi

Post Reply