bytedeco/javacv

Audio delayed in grabber

Open

#1,081 opened on Nov 5, 2018

View on GitHub
 (9 comments) (0 reactions) (0 assignees)Java (1,583 forks)batch import
help wantedquestion

Repository metrics

Stars
 (6,985 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

Hello I am having an issue trying to get the grabber playing nicely. When I open a local file, the audio seems to be delayed for about 500ms to 1000ms. THis is my code what must I change?

if (frame.image != null) {
                bi = frameConverter.convert(frame);
                // optional: we scale down to half the size
                if (inputRealtime && tElapsed < ts) {
                    try {
                        Thread.sleep((ts - tElapsed) / 1000);
                    } catch (Exception e) {
                    }
                }
                final BufferedImage biScaled = getScaledImage(bi, 640, 320);
                vidLabel.setText(null);// REMOVES THE ADD VIDEO TEXT
                vidLabel.setIcon(new ImageIcon(biScaled));
                vidLabel.revalidate();
                vidLabel.repaint();
                // filebased input to be read at realtime
            }
            // if audio frame, write to soundLine
            if (frame.samples != null) {
                ShortBuffer channelSamplesShortBuffer = (ShortBuffer) frame.samples[0];
                channelSamplesShortBuffer.rewind();
                final ByteBuffer outBuffer = ByteBuffer.allocate(channelSamplesShortBuffer.capacity() * 2);
                for (int i = 0; i < channelSamplesShortBuffer.capacity(); i++) {
                    short val = channelSamplesShortBuffer.get(i);
                    outBuffer.putShort(val);
                }
                try {
                    audioLineExecutor.submit(() -> {
                    soundLine.write(outBuffer.array(), 0, outBuffer.capacity());
                    outBuffer.clear();
                    }).get();
                } catch (Exception exc) {
                    Thread.currentThread().interrupt();
                }

            }```

Contributor guide