View Single Post
 
Old 09-17-2009, 11:10 AM
therealtomlapp therealtomlapp is offline
Freshman
 
Join Date: Jan 2009
Location: New Jersey
Posts: 40
Default

Here is my code:
Code:
Imports Microsoft.DirectX.AudioVideoPlayback

Public Class VideoPanel

#Region "Internal Variables"

    Private Internal_VideoMode As VideoPlaystateModes = VideoPlaystateModes.Stopped
    Private Internal_IdleVideo As String = Me.Internal_IdleVideo
    Private Internal_VideoFileName As String = ""
    Private Internal_Trailers As New System.Collections.ObjectModel.Collection(Of String)

#End Region

#Region "Class Object Creations"

    Public WithEvents VideoPlayer As Video
    Public WithEvents IdleVideoPlayer As Video

#End Region

#Region "External Events"

    Public Event PlayStateChange(ByVal NewPlaystate As VideoPlaystateModes, ByVal CurrentPosition As Double, ByVal VideoFileName As String)
    Public Event OutputDebugMessage(ByVal strMessage As String)

#End Region

#Region "Property Assignments"

    Public ReadOnly Property CurrentVideoFile() As String
        Get
            Return Internal_VideoFileName
        End Get
    End Property

    Public ReadOnly Property CurrentVideoMode() As VideoPlaystateModes
        Get
            Return Internal_VideoMode
        End Get
    End Property

    Public Property IdleVideo() As String
        Get
            Return Internal_IdleVideo
        End Get
        Set(ByVal value As String)
            Internal_IdleVideo = value
            If Internal_VideoMode = VideoPlaystateModes.Stopped Then GoIdle()
        End Set
    End Property

    Public Property CurrentPosition() As Double
        Get
            Try
                Return VideoPlayer.CurrentPosition
            Catch ex As Exception
                Return 0
            End Try
        End Get
        Set(ByVal value As Double)
            Try
                VideoPlayer.CurrentPosition = value
            Catch ex As Exception

            End Try
        End Set
    End Property

#End Region




#Region "Video playback control"

    Private Internal_PlayingTrailers As Boolean = False
    Private Internal_StartPosition As Double


    Public Sub PlayVideoWithTrailers(ByVal strFileName As String, ByVal StartPosition As Double)

        'Set the movie info for playing
        Internal_VideoFileName = strFileName
        Internal_StartPosition = StartPosition

        'Check to see first if there are any trailers in the collection
        If Internal_Trailers.Count = 0 Then
            'No Trailers Exist, just play the movie
            PlayVideo(strFileName, StartPosition)
        End If

        'Set the Internal_PlayingTrailers to True
        Internal_PlayingTrailers = True

        Try
            IdleVideoPlayer.Stop()
            IdleVideoPlayer.Dispose()
            VideoRenderingPanel.BringToFront()
        Catch ex As Exception

        End Try

        PlayNextTrailer()

    End Sub

    Private Sub PlayNextTrailer()

        Dim _TrailerToPlay As String

        'Check to see if any trailers are left
        If Internal_Trailers.Count = 0 Then
            'No more trailers, play the movie
            Internal_PlayingTrailers = False
            PlayVideo(Internal_VideoFileName, Internal_StartPosition)
            MsgBox("Now playing movie")
            Exit Sub
        End If

        'Set the next trailer
        _TrailerToPlay = Internal_Trailers(0)
        RaiseEvent OutputDebugMessage("Setting _TrailerToPlay to: " & Internal_Trailers(0))
        Application.DoEvents()

        'Remove that trailer from the list
        Internal_Trailers.RemoveAt(0)
        RaiseEvent OutputDebugMessage("Removing trailer from list")
        Application.DoEvents()

        Try
            'Load the trailer but do not play it
            RaiseEvent OutputDebugMessage("Loading the trailer into the video player")
            Application.DoEvents()
            VideoPlayer = New Video(_TrailerToPlay, False)
        Catch ex As Exception
            MsgBox("Error at: VideoPlayer = New Video(_TrailerToPlay, False)")
        End Try
        
        'Make the VideoRenderingPanel the owner
        RaiseEvent OutputDebugMessage("Setting the owner for the video player")
        Application.DoEvents()
        VideoPlayer.Owner = VideoRenderingPanel

        'Set the aspect ratio factors
        SetVideoAspectFactors()

        'Reset the aspect ratio
        ResetAspectRatio()

        Try
            'Start playback
            RaiseEvent OutputDebugMessage("Playing the video")
            Application.DoEvents()
            VideoPlayer.Play()
        Catch ex As Exception
            MsgBox("Error at: VideoPlayer.Play()")
        End Try

        Internal_VideoMode = VideoPlaystateModes.PlayingTrailers
        RaiseEvent PlayStateChange(VideoPlaystateModes.PlayingTrailers, VideoPlayer.CurrentPosition, Internal_VideoFileName)

    End Sub

    Public Sub PlayVideo(ByVal strFileName As String, ByVal StartPosition As Double)

        Try
            IdleVideoPlayer.Stop()
            IdleVideoPlayer.Dispose()
            VideoRenderingPanel.BringToFront()
        Catch ex As Exception

        End Try

        Try

            Internal_VideoFileName = strFileName

            'Load the video but do not play it yet
            VideoPlayer = New Video(strFileName, False)

            'Make the VideoRenderingPanel the owner
            VideoPlayer.Owner = VideoRenderingPanel

            'Set the aspect ratio factors
            SetVideoAspectFactors()

            'Reset the aspect ratio
            ResetAspectRatio()

            'Set the position
            VideoPlayer.CurrentPosition = StartPosition

            'Start playback
            VideoPlayer.Play()

            Internal_VideoMode = VideoPlaystateModes.Playing
            RaiseEvent PlayStateChange(VideoPlaystateModes.Playing, VideoPlayer.CurrentPosition, Internal_VideoFileName)

        Catch ex As Exception

            'Error detected when trying to load the file
            Internal_VideoMode = VideoPlaystateModes.LoadError
            RaiseEvent PlayStateChange(VideoPlaystateModes.LoadError, 0, strFileName)
            Exit Sub

        End Try

    End Sub

    Public Sub PauseVideo()

        'Toggle pause mode
        With VideoPlayer

            If .Paused = True Then

                .Play()
                Internal_VideoMode = VideoPlaystateModes.Playing
                RaiseEvent PlayStateChange(VideoPlaystateModes.Playing, VideoPlayer.CurrentPosition, Internal_VideoFileName)

            Else

                .Pause()
                Internal_VideoMode = VideoPlaystateModes.Paused
                RaiseEvent PlayStateChange(VideoPlaystateModes.Paused, VideoPlayer.CurrentPosition, Internal_VideoFileName)

            End If

        End With

    End Sub

    Public Sub StopVideo()

        Try
            VideoPlayer.Stop()
            VideoPlayer.Dispose()
        Catch ex As Exception

        End Try

        GoIdle()

    End Sub

    Private Sub VideoPlayer_Ending(ByVal sender As Object, ByVal e As System.EventArgs) Handles VideoPlayer.Ending

        RaiseEvent OutputDebugMessage("Video playback ended")
        Application.DoEvents()

        If Internal_PlayingTrailers = True Then
            'Manage for trailers
            RaiseEvent OutputDebugMessage("Executing VideoPlayer.Stop()")
            Application.DoEvents()
            VideoPlayer.Stop()
            RaiseEvent OutputDebugMessage("Checking to see if VideoPlayer is disposed")
            Application.DoEvents()
            If VideoPlayer.Disposed = False Then
                RaiseEvent OutputDebugMessage("VideoPlayer is not disposed, disposing now")
                Application.DoEvents()
                VideoPlayer.Dispose()
            Else
                RaiseEvent OutputDebugMessage("VideoPlayer is already disposed")
                Application.DoEvents()
            End If
            RaiseEvent OutputDebugMessage("Waiting 5 seconds")
            Application.DoEvents()
            System.Threading.Thread.Sleep(5000)
            PlayNextTrailer()

        Else

            Internal_VideoMode = VideoPlaystateModes.Stopped
            RaiseEvent PlayStateChange(VideoPlaystateModes.Stopped, VideoPlayer.StopPosition, Internal_VideoFileName)
            GoIdle()

        End If

        
    End Sub

#End Region

#Region "Trailer Control"

    Public Sub InjectMovieTrailer(ByVal TrailerFileName As String)

        Internal_Trailers.Add(TrailerFileName)

    End Sub

#End Region

#Region "Idle Video Panel Handling"

    Private Sub GoIdle()

        If My.Computer.FileSystem.FileExists(Internal_IdleVideo) = False Then
            If Internal_IdleVideo = "" Then
            Else
                MsgBox("Cannot find the file: " & Internal_IdleVideo)

            End If
            Exit Sub
        End If



        IdleVideoPlayer = New Video(Internal_IdleVideo)
        IdleVideoPlayer.Owner = IdleVideoPanel
        IdleVideoPanel.Location = New System.Drawing.Point(0, 0)
        IdleVideoPanel.Size = Me.Size
        IdleVideoPanel.BringToFront()
        IdleVideoPlayer.Play()




    End Sub

    Private Sub IdleVideoPlayer_Ending(ByVal sender As Object, ByVal e As System.EventArgs) Handles IdleVideoPlayer.Ending

        'Loop the video if it is idle
        If Internal_VideoMode = VideoPlaystateModes.Stopped Then
            IdleVideoPlayer.Play()
        Else
            IdleVideoPlayer.Dispose()
        End If

    End Sub


#End Region

#Region "Initialization"

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

#End Region

End Class
Reply With Quote