Question

I have an ASHX handler and I want to stream an audio (wav) file with it to the client. I use BASS.net to apply an effect on the audio before streaming it.

The problem is that reading from the BASS stream takes some time.

Here is the code:

Stream outStream = context.Response.OutputStream;
int length = 0;
context.Response.Buffer = true;
context.Response.ContentType = "audio/mpeg";
short[] data = new short[32768];
WaveWriterStream WW = new WaveWriterStream(outStream, bassStream);
do {
  length = Bass.BASS_ChannelGetData(bassStream, data, 32768);
  if (length > 0)
    WW.Write(data, length);
} while (length > 0);

The loop takes about 8 seconds to complete... A pretty long time if you click in the browser on the audioplayer and want to listen to something.

I added a Flush() after each Write operation but that did not change anything.

On the clientside i use soundmanger2 to play the audio Stream. This is the configuration:

soundManager.setup({
  url: 'soundmanagerv297a-20130512/swf/',
  waitForWindowLoad: true,
  ontimeout: function () {
    soundManager.useHTML5Audio = true;
    soundManager.preferFlash = false;
    soundManager.reboot();
  },
  flashVersion: 9, // optional: shiny features (default = 8)
  // optional: ignore Flash where possible, use 100% HTML5 mode
  // preferFlash: false,
  preferFlash: false,
  onready: function () {
    player = soundManager.createSound({
      id: 'mySound',
      url: 'http://localhost:4261/AudioHandler.ashx',
      autoLoad: true,
      autoPlay: true,
      onload: function () {
        alert('The sound ' + this.id + ' loaded!');
      },
      volume: 50
    });
  }
});

So is there any way to listen to the audio stream "immediately" after the ASHX handler starts writing to it?

Was it helpful?

Solution

I solved it with the Method BeginFlush() of the Outputstream

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top