PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

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