Note that there are some explanatory texts on larger screens.

plurals
  1. POnullreferenceexception was unhandled NAudio MP3 streaming
    primarykey
    data
    text
    <p>I am using Naudios Demo app to demostrait a litle bit. I found out that when i play some mp3 file, in the end om the sound/mp3 i get a </p> <blockquote> <p>error nullreferenceexception was unhandled nullreferenceexception object reference not set to an instance of an object</p> </blockquote> <p>in the file Mp3FrameDecompressor.cs</p> <pre><code>using System; using System.Collections.Generic; using System.Text; using NAudio.Wave; using NAudio.Wave.Compression; using System.Diagnostics; namespace NAudio.Wave { /// &lt;summary&gt; /// MP3 Frame Decompressor using ACM /// &lt;/summary&gt; public class AcmMp3FrameDecompressor : IDisposable, IMp3FrameDecompressor { private AcmStream conversionStream; private WaveFormat pcmFormat; /// &lt;summary&gt; /// Creates a new ACM frame decompressor /// &lt;/summary&gt; /// &lt;param name="sourceFormat"&gt;The MP3 source format&lt;/param&gt; public AcmMp3FrameDecompressor(WaveFormat sourceFormat) { this.pcmFormat = AcmStream.SuggestPcmFormat(sourceFormat); conversionStream = new AcmStream(sourceFormat, pcmFormat); } /// &lt;summary&gt; /// Output format (PCM) /// &lt;/summary&gt; public WaveFormat OutputFormat { get { return pcmFormat; } } /// &lt;summary&gt; /// Decompresses a frame /// &lt;/summary&gt; /// &lt;param name="frame"&gt;The MP3 frame&lt;/param&gt; /// &lt;param name="dest"&gt;destination buffer&lt;/param&gt; /// &lt;param name="destOffset"&gt;Offset within destination buffer&lt;/param&gt; /// &lt;returns&gt;Bytes written into destination buffer&lt;/returns&gt; public int DecompressFrame(Mp3Frame frame, byte[] dest, int destOffset) { Array.Copy(frame.RawData, conversionStream.SourceBuffer, frame.FrameLength); int sourceBytesConverted = 0; int converted = conversionStream.Convert(frame.FrameLength, out sourceBytesConverted); if (sourceBytesConverted != frame.FrameLength) { throw new InvalidOperationException(String.Format("Couldn't convert the whole MP3 frame (converted {0}/{1})", sourceBytesConverted, frame.FrameLength)); } Array.Copy(conversionStream.DestBuffer, 0, dest, destOffset, converted); return converted; } /// &lt;summary&gt; /// Disposes of this MP3 frame decompressor /// &lt;/summary&gt; public void Dispose() { if (this.conversionStream != null) { this.conversionStream.Dispose(); this.conversionStream = null; } } } } </code></pre> <p>This is the Form i use </p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using NAudio.Wave; using System.Net; using System.Threading; using System.Net.Sockets; using System.IO; using System.Diagnostics; using System.ComponentModel.Composition; namespace NAudioDemo { public partial class MP3StreamingPanel : Form { public MP3StreamingPanel() { InitializeComponent(); this.volumeSlider1.VolumeChanged += new EventHandler(volumeSlider1_VolumeChanged); this.Disposed += this.MP3StreamingPanel_Disposing; } enum StreamingPlaybackState { Stopped, Playing, Buffering, Paused } void volumeSlider1_VolumeChanged(object sender, EventArgs e) { if (this.volumeProvider != null) { this.volumeProvider.Volume = this.volumeSlider1.Volume; } } private BufferedWaveProvider bufferedWaveProvider; private IWavePlayer waveOut; private volatile StreamingPlaybackState playbackState; private volatile bool fullyDownloaded; private HttpWebRequest webRequest; private VolumeWaveProvider16 volumeProvider; delegate void ShowErrorDelegate(string message); private void ShowError(string message) { if (this.InvokeRequired) { this.BeginInvoke(new ShowErrorDelegate(ShowError), message); } else { MessageBox.Show(message); } } private void StreamMP3(object state) { this.fullyDownloaded = false; string url = (string)state; webRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse resp = null; try { resp = (HttpWebResponse)webRequest.GetResponse(); } catch(WebException e) { if (e.Status != WebExceptionStatus.RequestCanceled) { ShowError(e.Message); } return; } byte[] buffer = new byte[16384 * 4]; // needs to be big enough to hold a decompressed frame IMp3FrameDecompressor decompressor = null; try { using (var responseStream = resp.GetResponseStream()) { var readFullyStream = new ReadFullyStream(responseStream); do { if (bufferedWaveProvider != null &amp;&amp; bufferedWaveProvider.BufferLength - bufferedWaveProvider.BufferedBytes &lt; bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 4) { Debug.WriteLine("Buffer getting full, taking a break"); Thread.Sleep(500); } else { Mp3Frame frame = null; try { frame = Mp3Frame.LoadFromStream(readFullyStream); } catch (EndOfStreamException) { this.fullyDownloaded = true; // reached the end of the MP3 file / stream break; } catch (WebException) { // probably we have aborted download from the GUI thread break; } if (decompressor == null) { // don't think these details matter too much - just help ACM select the right codec // however, the buffered provider doesn't know what sample rate it is working at // until we have a frame WaveFormat waveFormat = new Mp3WaveFormat(frame.SampleRate, frame.ChannelMode == ChannelMode.Mono ? 1 : 2, frame.FrameLength, frame.BitRate); decompressor = new AcmMp3FrameDecompressor(waveFormat); this.bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat); this.bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(20); // allow us to get well ahead of ourselves //this.bufferedWaveProvider.BufferedDuration = 250; } int decompressed = decompressor.DecompressFrame(frame, buffer, 0); //Debug.WriteLine(String.Format("Decompressed a frame {0}", decompressed)); bufferedWaveProvider.AddSamples(buffer, 0, decompressed); } } while (playbackState != StreamingPlaybackState.Stopped); Debug.WriteLine("Exiting"); // was doing this in a finally block, but for some reason // we are hanging on response stream .Dispose so never get there decompressor.Dispose(); } } finally { if (decompressor != null) { decompressor.Dispose(); } } } private void buttonPlay_Click(object sender, EventArgs e) { if (playbackState == StreamingPlaybackState.Stopped) { playbackState = StreamingPlaybackState.Buffering; this.bufferedWaveProvider = null; ThreadPool.QueueUserWorkItem(new WaitCallback(StreamMP3), textBoxStreamingUrl.Text); timer1.Enabled = true; } else if (playbackState == StreamingPlaybackState.Paused) { playbackState = StreamingPlaybackState.Buffering; } } private void StopPlayback() { if (playbackState != StreamingPlaybackState.Stopped) { if (!fullyDownloaded) { webRequest.Abort(); } this.playbackState = StreamingPlaybackState.Stopped; if (waveOut != null) { waveOut.Stop(); waveOut.Dispose(); waveOut = null; } timer1.Enabled = false; // n.b. streaming thread may not yet have exited Thread.Sleep(500); ShowBufferState(0); } } private void ShowBufferState(double totalSeconds) { } private void timer1_Tick(object sender, EventArgs e) { if (playbackState != StreamingPlaybackState.Stopped) { if (this.waveOut == null &amp;&amp; this.bufferedWaveProvider != null) { Debug.WriteLine("Creating WaveOut Device"); this.waveOut = CreateWaveOut(); waveOut.PlaybackStopped += waveOut_PlaybackStopped; this.volumeProvider = new VolumeWaveProvider16(bufferedWaveProvider); this.volumeProvider.Volume = this.volumeSlider1.Volume; waveOut.Init(volumeProvider); progressBarBuffer.Maximum = (int)bufferedWaveProvider.BufferDuration.TotalMilliseconds; } else if (bufferedWaveProvider != null) { var bufferedSeconds = bufferedWaveProvider.BufferedDuration.TotalSeconds; ShowBufferState(bufferedSeconds); // make it stutter less if we buffer up a decent amount before playing if (bufferedSeconds &lt; 0.5 &amp;&amp; this.playbackState == StreamingPlaybackState.Playing &amp;&amp; !this.fullyDownloaded) { this.playbackState = StreamingPlaybackState.Buffering; waveOut.Pause(); Debug.WriteLine(String.Format("Paused to buffer, waveOut.PlaybackState={0}", waveOut.PlaybackState)); } else if (bufferedSeconds &gt; 1 &amp;&amp; this.playbackState == StreamingPlaybackState.Buffering) { waveOut.Play(); Debug.WriteLine(String.Format("Started playing, waveOut.PlaybackState={0}", waveOut.PlaybackState)); this.playbackState = StreamingPlaybackState.Playing; } else if (this.fullyDownloaded &amp;&amp; bufferedSeconds == 0) { Debug.WriteLine("Reached end of stream"); StopPlayback(); } } } } private IWavePlayer CreateWaveOut() { return new WaveOut(); //return new DirectSoundOut(); } private void MP3StreamingPanel_Disposing(object sender, EventArgs e) { StopPlayback(); } private void buttonPause_Click(object sender, EventArgs e) { if (playbackState == StreamingPlaybackState.Playing || playbackState == StreamingPlaybackState.Buffering) { waveOut.Pause(); Debug.WriteLine(String.Format("User requested Pause, waveOut.PlaybackState={0}", waveOut.PlaybackState)); playbackState = StreamingPlaybackState.Paused; } } private void buttonStop_Click(object sender, EventArgs e) { StopPlayback(); } private void waveOut_PlaybackStopped(object sender, StoppedEventArgs e) { Debug.WriteLine("Playback Stopped"); if (e.Exception != null) { MessageBox.Show(String.Format("Playback Error {0}", e.Exception.Message)); } } } [Export(typeof(INAudioDemoPlugin))] public class MP3StreamingPanelPlugin : INAudioDemoPlugin { public string Name { get { return "MP3 Streaming"; } } public Control CreatePanel() { return new MP3StreamingPanel(); } } } </code></pre> <p>i get the error on this line </p> <pre><code>Array.Copy(frame.RawData, conversionStream.SourceBuffer, frame.FrameLength); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload