Java 字节数组音频加密
我有一个字节数组,其中有一行包含麦克风音频字节。 我想逐行加密它,我不确定它是否逐行加密字节数组,因为当我播放音频时,它会播放清晰的声音 这是加密方法Java 字节数组音频加密,java,arrays,audio,encryption,Java,Arrays,Audio,Encryption,我有一个字节数组,其中有一行包含麦克风音频字节。 我想逐行加密它,我不确定它是否逐行加密字节数组,因为当我播放音频时,它会播放清晰的声音 这是加密方法 if (AudioSystem.isLineSupported(Port.Info.SPEAKER)) { try { final AudioFormat format = getFormat(); DataLine.Info info = new DataLine.Inf
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);
}
}
嗯……难道你不应该在写入流之前进行加密吗?@jaket fo你的意思是如果(计数>0){lines++;System.out.println(“lines writed=“+lines”);encryptBuff()的if语句应该是这样的;out.write(缓冲区,0,计数);}是的,这就是我的意思。当您写出缓冲区时,被调用方将自己复制它。然后您正在加密原始缓冲区,但现在太迟了。@RWEQX我看到了您关于Java录制麦克风到字节数组和播放声音的问题。您能提供源代码吗??