字节数组音频加密

时间:2015-11-28 07:31:05

标签: java arrays audio encryption

我有一个字节数组,其中的行包含麦克风音频的字节。 我想逐行加密,我不确定它是否逐行加密字节数组,因为当我播放音频时它会发出清晰的声音

这是加密方法

    if (AudioSystem.isLineSupported(Port.Info.SPEAKER)) {

        try {
            final AudioFormat format = getFormat();
            DataLine.Info info = new DataLine.Info(
                    TargetDataLine.class, format);
            final TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
            line.open(format);
            line.start();

            Runnable runner = new Runnable() {
                int bufferSize = (int) format.getSampleRate()
                        * format.getFrameSize();
                byte buffer[] = new byte[bufferSize];

                public void run() {

                    out = new ByteArrayOutputStream();

                    running = true;
                    System.out.println("running..........");
                    int lines = 0;
                    try {
                        while (running) {
                            int count
                                    = line.read(buffer, 0, buffer.length);
                            if (count > 0) {
                                out.write(buffer, 0, count);
                                lines++;
                                System.out.println("lines written =" + lines);
                                encryptBuff();

                            }

                        }

                        out.close();
                    } catch (IOException e) {
                        System.err.println("I/O problems: " + e);
                        System.exit(-1);
                    } catch (Exception ex) {
                        Logger.getLogger(SC.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            };
            Thread captureThread = new Thread(runner);
            captureThread.start();
        } catch (LineUnavailableException e) {
            System.err.println("Line unavailable: " + e);
            System.exit(-2);
        }    

这是他们播放音频代码

private void playAudio() {
    try {
        byte audio[] = out.toByteArray();
        InputStream input
                = new ByteArrayInputStream(audio);

        final AudioFormat format = getFormat();
        final AudioInputStream ais
                = new AudioInputStream(input, format,
                        audio.length / format.getFrameSize());
        DataLine.Info info = new DataLine.Info(
                SourceDataLine.class, format);
        final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(format);
        line.start();

        Runnable runner = new Runnable() {
            int bufferSize = (int) format.getSampleRate()
                    * format.getFrameSize();
            byte buffer[] = new byte[bufferSize];

            public void run() {
                try {
                    int count;
                    while ((count = ais.read(
                            buffer, 0, buffer.length)) != -1) {
                        if (count > 0) {
                            line.write(buffer, 0, count);
                        }
                    }
                    line.drain();
                    line.close();
                } catch (IOException e) {
                    System.err.println("I/O problems: " + e);
                    System.exit(-3);
                }
            }
        };
        Thread playThread = new Thread(runner);
        playThread.start();
    } catch (LineUnavailableException e) {
        System.err.println("Line unavailable: " + e);
        System.exit(-4);
    }

}    

0 个答案:

没有答案