PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/VBSL3MediaElement/VBSL3MediaElement/MainPage.xaml.vb

#
Visual Basic | 174 lines | 117 code | 25 blank | 32 comment | 0 complexity | 4f1b5c234801e2af678751504656b15f MD5 | raw file
  1'***************************** Module Header ******************************\
  2'* Module Name:  MainPage.xaml.cs
  3'* Project:      VBSL3MediaElement
  4'* Copyright (c) Microsoft Corporation.
  5'* 
  6'* This example illustrates basic usage of MediaElement.
  7'* 
  8'* This source is subject to the Microsoft Public License.
  9'* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 10'* All other rights reserved.
 11'* 
 12'* History:
 13'* * 10/12/2009 03:00 PM Allen Chen Created
 14'\**************************************************************************
 15
 16
 17Imports System
 18Imports System.Collections.Generic
 19Imports System.Linq
 20Imports System.Net
 21Imports System.Windows
 22Imports System.Windows.Controls
 23Imports System.Windows.Documents
 24Imports System.Windows.Input
 25Imports System.Windows.Media
 26Imports System.Windows.Media.Animation
 27Imports System.Windows.Shapes
 28Imports System.Windows.Threading
 29
 30Partial Public Class MainPage
 31    Inherits UserControl
 32
 33    Private _timer As DispatcherTimer = New DispatcherTimer()
 34
 35    Public Sub New()
 36        InitializeComponent()
 37        AddHandler Loaded, AddressOf MainPage_Loaded
 38        _timer.Interval = TimeSpan.FromMilliseconds(100)
 39        AddHandler _timer.Tick, AddressOf _timer_Tick
 40    End Sub
 41
 42
 43    Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
 44        ' Attach events of SilverlightHost to subscribe the 
 45        ' FullScreenChanged event.
 46        AddHandler App.Current.Host.Content.FullScreenChanged, AddressOf Content_FullScreenChanged
 47    End Sub
 48
 49
 50    Private Sub Content_FullScreenChanged(ByVal sender As Object, ByVal e As EventArgs)
 51        If (Not App.Current.Host.Content.IsFullScreen) Then
 52            ' When shifting back to normal mode, reset width/height of MediaElement.
 53            Me.MyMediaElement.Width = Double.NaN
 54            Me.MyMediaElement.Height = Double.NaN
 55            ' When shifting back to normal mode, hide FullSreenPanel and show MenuPanel
 56            Me.MenuPanel.Visibility = Visibility.Visible
 57            Me.FullScreenPanel.Visibility = Visibility.Collapsed
 58        Else
 59            ' In full-screen mode, set width/height of MediaElement to a big value
 60            Me.MyMediaElement.Width = App.Current.Host.Content.ActualWidth
 61            Me.MyMediaElement.Height = App.Current.Host.Content.ActualHeight
 62            ' In full-screen mode, hide MenuPanel and show FullSreenPanel
 63            Me.MenuPanel.Visibility = Visibility.Collapsed
 64            Me.FullScreenPanel.Visibility = Visibility.Visible
 65        End If
 66    End Sub
 67
 68    Private Sub _timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
 69        If Me.MyMediaElement.CurrentState = MediaElementState.Playing Then
 70            ' Update Status of controls
 71            Dim ts As TimeSpan = Me.MyMediaElement.Position
 72            Me.CurrentPositionTextBlock.Text = String.Format("{0}:{1}:{2}/", ts.Hours, ts.Minutes, ts.Seconds)
 73            Me.CurrentPositionSlider.Value = ts.TotalMilliseconds
 74        ElseIf Me.MyMediaElement.CurrentState = MediaElementState.Stopped Then
 75            ' Reset status of controls
 76            Me.CurrentPositionTextBlock.Text = "0:0:0/"
 77            Me.CaptionTextBlock.Text = String.Empty
 78            Me.CurrentPositionSlider.Value = 0
 79        End If
 80    End Sub
 81
 82    Private Sub PlayButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
 83        Me.MyMediaElement.Play()
 84        Me._timer.Start()
 85    End Sub
 86
 87    Private Sub PauseButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
 88        Me.MyMediaElement.Pause()
 89    End Sub
 90    Private Sub StopButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
 91        Me.MyMediaElement.Stop()
 92    End Sub
 93
 94    Private Sub MyMediaElement_CurrentStateChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
 95        ' Show current state of MediaElement. Note only 4 states are catched here.
 96        ' You can add more when creating your own media player.
 97        Select Case Me.MyMediaElement.CurrentState
 98            Case MediaElementState.Stopped
 99                Me.StatusTextBlock.Text = "Stopped"
100                Exit Select
101            Case MediaElementState.Paused
102                Me.StatusTextBlock.Text = "Paused"
103                Exit Select
104            Case MediaElementState.Playing
105                Me.StatusTextBlock.Text = "Playing"
106                Exit Select
107            Case MediaElementState.Buffering
108                Me.StatusTextBlock.Text = "Buffering"
109                Exit Select
110        End Select
111    End Sub
112
113    Private Sub MyMediaElement_MediaOpened(ByVal sender As Object, ByVal e As RoutedEventArgs)
114        Dim ts As TimeSpan = Me.MyMediaElement.NaturalDuration.TimeSpan
115        Me.DurationTextBlock.Text = String.Format("{0}:{1}:{2}", ts.Hours, ts.Minutes, ts.Seconds)
116        ' Init CurrentPosition Slider
117        Me.CurrentPositionSlider.Maximum = ts.TotalMilliseconds
118        Me.CurrentPositionSlider.Minimum = 0
119        ' Init VolumeSlider
120        Me.VolumeSlider.Value = Me.MyMediaElement.Volume
121        ' Add a marker by code
122        Dim timelinemarker As TimelineMarker = New TimelineMarker()
123        timelinemarker.Text = "This marker is added by code!"
124        timelinemarker.Type = "MyMarker"
125        timelinemarker.Time = TimeSpan.FromSeconds(8)
126        Me.MyMediaElement.Markers.Add(timelinemarker)
127
128        ' Enable progress bar
129        Me.CurrentPositionSlider.IsEnabled = True
130    End Sub
131
132    Private Sub MyMediaElement_MediaEnded(ByVal sender As Object, ByVal e As RoutedEventArgs)
133        Me.MyMediaElement.Stop()
134    End Sub
135
136    Private Sub HorizontalThumb_DragStarted(ByVal sender As Object, ByVal e As System.Windows.Controls.Primitives.DragStartedEventArgs)
137        Me.MyMediaElement.Pause()
138    End Sub
139
140    Private Sub HorizontalThumb_DragCompleted(ByVal sender As Object, ByVal e As System.Windows.Controls.Primitives.DragCompletedEventArgs)
141        Me.MyMediaElement.Position = TimeSpan.FromMilliseconds(Me.CurrentPositionSlider.Value)
142        Me.MyMediaElement.Play()
143    End Sub
144
145    Private Sub MyMediaElement_MarkerReached(ByVal sender As Object, ByVal e As TimelineMarkerRoutedEventArgs)
146        If e.Marker.Type = "MyMarker" Then
147            ' Show marker text made by code
148            Me.CaptionTextBlock.FontSize = 22
149            Me.CaptionTextBlock.Text = e.Marker.Text
150
151        Else
152            ' Show marker text made by Expression Blend 3
153            ' Because the caption is "out of" the video you get more flexibility to customizing it
154            Me.CaptionTextBlock.FontSize = 12
155            Me.CaptionTextBlock.Text = e.Marker.Text
156        End If
157    End Sub
158
159    Private Sub FullScreenButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
160        ' Shift to full-screen mode
161        App.Current.Host.Content.IsFullScreen = Not App.Current.Host.Content.IsFullScreen
162    End Sub
163
164    Private Sub VolumeSlider_ValueChanged(ByVal sender As Object, ByVal e As RoutedPropertyChangedEventArgs(Of Double))
165        Me.MyMediaElement.Volume = Me.VolumeSlider.Value
166    End Sub
167
168    Private Sub MyMediaElement_MediaFailed(ByVal sender As Object, ByVal e As ExceptionRoutedEventArgs)
169        Me.CurrentPositionSlider.IsEnabled = False
170        MessageBox.Show(e.ErrorException.Message & ". Please make sure you're viewing CSSL3MediaElementTestPage.aspx")
171
172    End Sub
173
174End Class