Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video
Go Back  Xtreme Visual Basic Talk > > > > Null Reference Exception error when playing DirectX video


Reply
 
Thread Tools Display Modes
  #1  
Old 09-17-2009, 11:08 AM
therealtomlapp therealtomlapp is offline
Freshman
 
Join Date: Jan 2009
Location: New Jersey
Posts: 40
Default Null Reference Exception error when playing DirectX video


I am creating a program for my theater pc that when I play a movie will play a few trailers before it using DirectX. Basically the video panel i've created is a custom control for me to finally insert into the program itself. When you use the control you can optionally insert a few trailers into a collection, then fire the code to play the movie. It will play all the trailers until the collection is exhausted, then play the movie. It plays the first trailer just fine, then when it goes to play the second one I get a generic "Null Reference Exception" error that does not point to any particular line of code. I ended up putting in an event to literally output messages telling me what line of code is about to be executed beforehand so I could get a better idea of when the error is happening.

Here are the steps to recreate the error:

1. From my test program, I execute the following

With VideoPanel1
.InjectMovieTrailer("E:\Media Folders\Intro Trailers\DOLBY DIGITAL\Canyon.AVI")
.InjectMovieTrailer("E:\Media Folders\Intro Trailers\THX\VTS_17_1.AVI")
End With

VideoPanel1.PlayVideoWithTrailers("e:\Media Folders\Movies\8mm.avi", 1)

2. The first trailer (Canyon.avi) begins to play, then it ends, my code executes the VideoPlayer.dispose() function to clear resources, which is also needed so the video codecs unload.

3. Then it waits 5 seconds, which gives the system enough time to unload all the codecs. This is needed since I have my computer connected via spdif to a Dolby Digital surround system, without the delay the next video ends up playing without sound.

4. Then PlayNextTrailer() is executed, which takes it back to get the next trailer from the list and set up the playback

5. I put code in my test program to output all my custom debug messages into a listbox so I can see what is about to happen before the error occurs.

6. As I watch the list, I see that the error happens after the next VideoPlayer.Play() function is executed, then I get the generic error.

The funny thing is when the error occurs, you can still see that the next trailer is playing, but obviously I still need to stop the crash from happening. I've experimented with different video files, just to be sure I wasn't using a partially corrupt video file. No matter what it always crashes on the second play, and my try/catch block does me no good.

I am fairly new to DirectX, but am well seasoned in vb coding. If anyone who knows more about DirectX has any ideas for me please let me know, I will greatly appreciate it.
Reply With Quote
  #2  
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
  #3  
Old 09-17-2009, 11:11 AM
therealtomlapp therealtomlapp is offline
Freshman
 
Join Date: Jan 2009
Location: New Jersey
Posts: 40
Default

P.S I had to take out some code from the post since I was limited to 10000 characters, but all the pertinent code is in there.
Reply With Quote
  #4  
Old 09-17-2009, 02:24 PM
therealtomlapp therealtomlapp is offline
Freshman
 
Join Date: Jan 2009
Location: New Jersey
Posts: 40
Default

I sort of fixed the problem but now I have another problem.

I implemented this code to load the video file

If VideoPlayer is Nothing then
VideoPlayer = New Video(videofilename, false)
else
VideoPlayer.Open(videofilename, false)
end if

Now it plays the second video without error, but when it get's done playing that second video, it does not fire the VideoPlayer.Ending event, so it's almost like directx does not know the video ended. I know that I can use a timer to monitor the currentposition but I always try to avoid timers whenever I can since eventually they end up slowing my program down. I would rather do this thing the correct way.
Reply With Quote
  #5  
Old 09-17-2009, 11:41 PM
therealtomlapp therealtomlapp is offline
Freshman
 
Join Date: Jan 2009
Location: New Jersey
Posts: 40
Default

No something is wrong here. I've been looking all over the net at different code samples on handling video with DirectX and either they are all wrong, or I am blind. Although I have gotten things to temporarily work using a timer, I just don't feel it's the correct way to handle things since all the pertinent event handlers exist. Another problem is once I play a video in my app, it screws up DirectX for other programs. An example would be a visualization plugin I have for Windows Media Player called Gforce. After I play a video in my program, Gforce will not work, it gives an error=18 message which means that it thinks i do not have sufficient hardware to use it (I have a Nvidia Geforce 9800 GT). I can't use Gforce again until I have restarted the computer.

I strongly feel that the VideoPlayer.dispose event is not doing what it needs to do. I don't know any other way to do this. I know that DirectX can be used for video playback without all these problems. It's almost like it's the biggest secret on the internet, and nobody's sharing it. I wish there was someone who had more DirectX knowledge that could put me in the right direction. I would use the AxWindowsMediaPlayer control, but I am unable to use it since I am running Windows 7 64-bit version, and there is currently an unresolved bug that stops me from using it.
Reply With Quote
  #6  
Old 09-18-2009, 07:42 AM
AtmaWeapon's Avatar
AtmaWeaponNull Reference Exception error when playing DirectX video AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I am very inexperienced with DirectX or any kind of playing videos in applications, but I want to verify that something is as true as you believe:
Quote:
since all the pertinent event handlers exist.
What does this mean? If you mean that you have some method that looks like the following, your assumption is wrong.
Code:
Sub VideoPlayer_Ending(...) Handles VideoPlayer.Ending
The Handles section of a method declaration magically connects an event handler to a control. I say "magically" because you have no control over when it's done and how it works. It only works with variables declared WithEvents. I presume it happens in some auto-generated code in the class constructor, but I don't care to confirm this as it's not relevant to the problem.

The Handles clause was meant to be a convenience for the 90% case: people who don't dynamically destroy and create controls. When you dispose of VideoPlayer, the event handler is unregistered. When you create a new VideoPlayer, the event handler is not registered again because Handles doesn't track dynamically created controls.

To address this, I recommend using the AddHandler keyword to add the event handler manually. I'd recommend getting rid of the Handles clause and creating a method like this:
Code:
Sub CreateVideoPlayer(ByVal videoFilePath As String, ByVal magicBooleanVariable As Boolean) As Video
  VideoPlayer = New Video(videoFilePath, magicBooleanVariable)
  AddHandler VideoPlayer.Ending, AddressOf VideoPlayer_Ending
end Sub
Omitted from this example is code to make sure that if there's already a player, you use RemoveHandler to remove the event handler then destroy that player.

Still, I doubt this is how things are supposed to work. Unfortunately, I don't know much about DirectX and few people on this forum seem to have a working knowledge either. All I can suggest is to consider WPF; it has a MediaPlayer control that is a little weird to use but at least doesn't involve mucking about with DX and/or COM.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #7  
Old 09-18-2009, 08:35 AM
therealtomlapp therealtomlapp is offline
Freshman
 
Join Date: Jan 2009
Location: New Jersey
Posts: 40
Default

Yeah I kind of had a feeling about that. Still there is the problem of it screwing up DirectX for the rest of the system.

I really wish that Microsoft had the whole AxWindowsMediaPlayer bug figured out so I could use that instead, but who knows when that will be fixed.
Reply With Quote
  #8  
Old 09-18-2009, 09:01 AM
therealtomlapp therealtomlapp is offline
Freshman
 
Join Date: Jan 2009
Location: New Jersey
Posts: 40
Default

So im assuming I should first change this:

Public WithEvents VideoPlayer as Video

to:

Public VideoPlayer as Video

and get rid of the Handles clauses on the subs, then add the event handlers manually when creating/initializing the VideoPlayer

if VideoPlayer is Nothing then
VideoPlayer = New Video(strFilename)
else
VideoPlayer.Open(strFilename)
end if

AddHandler VideoPlayer.Ending, AddressOf VideoPlayer_Ending

I believe this will fix my initial problem of having to use a timer, which is awesome.

Still if there is anyone out there that knows about DirectX I still need help on having it not mess up the rest of my system every time I use it.

Just tried it and the event handler won't fire the second time around, even though I am adding it dynamically. So it plays the first video, then when it is over fires the event, which my code then executes VideoPlayer.dispose. Then the second video plays, then nothing happens.

I've read that the dispose function doesn't completely free up the resources for some reason. Basically the best case scenario is to take VideoPlayer back to square one every time, as if every video played was the first one.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video
 
Null Reference Exception error when playing DirectX video
Null Reference Exception error when playing DirectX video
 
-->