This project has moved and is read-only. For the latest updates, please go here.

Play audio from a database blob

Feb 27, 2014 at 8:51 PM
Edited Feb 27, 2014 at 8:52 PM
I'm having problems to play a sound file stored in a database.

When I read the table and get the column blob (is a MySQL 5.6 db), I put it in a byte array, since it's how it's being saved in the db.

And here start the problem: no matter how I tried to use this array, I cannot play it! I tried converting the byte array to a MemoryStream, using this example, as a base, but when it tries to convert the memory stream to a WaveStream through Mp3FileReader(), it raises a NullReferenceException:


This is just a proof of concept program, that's why I didn't signed the Playback event.

Can someone help me with this?


Alberto T. Simon
Feb 27, 2014 at 10:17 PM
is the byte array null?
Can you show us the full stack trace from the exception?
Are you sure it's an MP3 file (do File.WriteAllBytes and try to play it with MediaPlayer)
Feb 27, 2014 at 10:23 PM
Perhaps byte[] bytes parsed is null or empty?
Feb 27, 2014 at 11:23 PM
Edited Feb 27, 2014 at 11:29 PM
The byte array is not null:

This already works with SoundPlayer(), except that I have some compressed files that does not work with SoundPlayer(): that's why I'm trying to use NAudio.

In the example above all the files I try to play (PCM or compressed) are raising the same error.

This is the exception detail:
System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
       at NAudio.Wave.Mp3FileReader..ctor(Stream inputStream, FrameDecompressorBuilder frameDecompressorBuilder)
       at NAudio.Wave.Mp3FileReader..ctor(Stream inputStream)
       at TestNAudio.Form1.PlayFromMemoryStream(Byte[] bytes) in c:\Users\Alberto\Documents\Visual Studio 2012\Projects\TestNAudio\TestNAudio\Form1.cs:line 131
       at TestNAudio.Form1.button3_Click(Object sender, EventArgs e) in c:\Users\Alberto\Documents\Visual Studio 2012\Projects\TestNAudio\TestNAudio\Form1.cs:line 86
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at TestNAudio.Program.Main() in c:\Users\Alberto\Documents\Visual Studio 2012\Projects\TestNAudio\TestNAudio\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
Feb 28, 2014 at 1:51 AM
What NAudio version are you using?

Have you tried the latest source?
Feb 28, 2014 at 5:19 PM
Version, I got it last week.
Mar 1, 2014 at 12:43 AM
could you dump the blob of bytes to a file and upload it?

the code looks fine so it is possible that the byte array has an issue