bytedeco/javacv

Audio delayed in grabber

Open

#1,081 创建于 2018年11月5日

在 GitHub 查看
 (9 评论) (0 反应) (0 负责人)Java (1,583 fork)batch import
help wantedquestion

仓库指标

Star
 (6,985 star)
PR 合并指标
 (30 天内没有已合并 PR)

描述

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();
                }

            }```

贡献者指南