Pause playback SourceDataLine

I want to play a WAV audio (soundtrack of some custom video format) in Java, however I have problems with Clip for this: it seems that only one instance is heard at a time. So I switch to the plain old SourceDataLine path.

In this context, I want to pause and resume audio since the video is paused and paused. Unfortunately. When I call stop () on SDL, the playback thread ends completely and the sound buffer is empty:

sdl.open();
sdl.start();
sdl.write(dataBuffer);
sdl.drain();
sdl.stop();

      

Throwing an asynchronous stop () when the audio stream is blocked while recording () or draining () will practically lose the playback position.

How can I pause the SourceDataLine in a blocking way and / or how can I find out how much audio has been played through it to make a summary using write (databuffer, skip, len)?

+2


source to share


1 answer


The suggested way is to call stop()

the SourceDataLine to suspend it and start()

to resume it. If you stop feeding the SourceDataLine audio data when it starts, it will cause a buffer flaw, which is usually considered an error condition. Just stop feeding data when it is stopped.

drain()

should be called at the end when you want all the data to be played that you entered into the SourceDataLine. Don't call it for "pause"!



It is correct that you should write small buffers to the raw data string for better control. A good size is equivalent to 50ms buffers - use the method open(AudioFormat, bufferSize)

to specify the size of the buffer in bytes (for example, 8820 bytes for 44100 Hz, 16-bit stereo).

Also I would say that using a clip is the preferred solution. Use start () and stop () on Clip and it shouldn't swallow hard. Most Java Sound implementations use SourceDataLine inside Clip, so there should be no functional difference. Java Sound needs to make sure you can play multiple clips at the same time.

+4


source







All Articles