I'm having trouble with WaveOutEvent. I'm not sure if it is a bug or just my code, so for now I'm just raising a Discussion, not an Issue.
I'm using WaveOutEvent in an ASP.Net web application. It's a simple application that plays a list of songs. When one song ends, WaveOutEvent throws a PlayBackStopped event. I catch that, dispose the WaveOutEvent object; then create a new WaveOutEvent object,
filereader, etc. It usually works, but about 1 time in 5, I get a NullReferenceException in WaveOutEvent.cs. See the offending statement (in yellow hightlight) on the attached screen shot. NB - I can make it happen at will, just by putting a breakpoint in
RaisePlaybackStoppedEvent(). So, just by hitting the breakpoint and continuing, I get the error; or about 1 time in 5, without the breakpoint, I get the error. I can't figure out which variable is triggering the null reference, and I will concede that I don't
understand the lambda operator, but my hypothesis is that 'this' is from another thread, which sometimes goes null depending on which thread runs first.
Also, this all arose because I am trying to Dispose of the object and create a new one, for each song. When I keep the same object, the problem does not occur (but as Mark pointed out, it will leak memory).
If anyone can help me sort this out, I'd be grateful.
Jake
Class usage:
Public Sub PlayFile(ByRef fileName As String) ' play one file
Dim fileExt As String
waveOutDevice = New WaveOutEvent()
fileExt = fileName.Substring(fileName.LastIndexOf("."c))
Select Case fileExt.ToLower ' pick the appropriate file reader
Case ".aif", ".aiff"
fReader = New AiffFileReader(fileName)
Case ".mp3"
fReader = New Mp3FileReader(fileName)
Case ".wma"
fReader = New WMAFileReader(fileName)
Case Else
Debug.WriteLine("WebAudio: Invalid File Extension: " + fileExt)
Return
End Select
mainOutputStream = New WaveChannel32(fReader) ' tie the file reader to the output stream
mainOutputStream.PadWithZeroes = False ' need this to detect eof
waveOutDevice.Init(mainOutputStream) ' tie output stream to output device
waveOutDevice.Play()
End Sub
Exception Detail:
System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=System.Web
StackTrace:
at System.Web.HttpApplication.ThreadContext.Enter(Boolean setImpersonationContext)
at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
at System.Web.AspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state)
at System.Web.AspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state)
at System.Web.AspNetSynchronizationContext.Post(SendOrPostCallback callback, Object state)
at NAudio.Wave.WaveOutEvent.RaisePlaybackStoppedEvent() in C:\Users\Gavin\Documents\Visual Studio Projects\NAudio\NAudio\Wave\WaveOutputs\WaveOutEvent.cs:line 280
at NAudio.Wave.WaveOutEvent.PlaybackThread() in C:\Users\Gavin\Documents\Visual Studio Projects\NAudio\NAudio\Wave\WaveOutputs\WaveOutEvent.cs:line 130
at NAudio.Wave.WaveOutEvent.b__0(Object state) in C:\Users\Gavin\Documents\Visual Studio Projects\NAudio\NAudio\Wave\WaveOutputs\WaveOutEvent.cs:line 94
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
InnerException:
Screen shot:
Ooops - can't do attachments in Discussion.
Offending statement is line #280 in WaveOutEvent.cs
Breakpoint at line #271 in WaveOutEvent.cs