Using an AudioStream

I am trying to use an AudioStream to play audio supply via an OutputStream feeding an InputStream connected to the AudioStream connected to an AudioNode.

My init code look like so:

    [java]ByteArrayOutputStream outputStream = new ByteArrayOutputStream(2048);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    AudioStream audioStream = new AudioStream();
    int chans = 1;
    int bits = 16;
    int rate = 8000;
    audioStream.setupFormat(chans, bits, rate);
    audioStream.updateData(inputStream, -1f);
    AudioNode audioNode = new AudioNode(audioStream, null);


I’d like to be able to programmatically just write() PCM data into outputStream and have the AudioNode play it.

Nothing is happening, which leads me to believe I have failed to understand something important. What am I missing?

Also,I do have some questions:

  1. Do I have to at any time tell the AudioNode to play()?
  2. What happens if the data I supply falls idle? Do I need to do anything to restart it, or do I merely write() more data into outputStream? I’m hoping it just falls to a zero sample when starved of data.
1 Like

I presume you will be filling this buffer from another thread?

This is not nearly as simple as you hope. If you are feeding the buffer from another thread then you will need to handle thread safe data transfer. You also have to keep providing data from the stream even when there is no data left or the sound will stop playing. So you will somehow have to manufacture “silence” while your buffers are empty.

The basic design is to pump the data into a ConcurrentLinkedQueue. When data is requested from the AudioStream then you read what you have and give it. If there is no data to give then you fake it with “silence”.

And of course you need to call play() on AudioNode sometime. How else will it know to play the sound?