使用字节数组创建 AudioInputStream
Using a byte array to create an AudioInputStream
我仍在努力在 JAVA 中创建一个调音台的小项目,但我遇到了新障碍。
我正在尝试从 byteArray 创建一个 audioInputStream,然后读取它来听声音。但是我的代码有问题:我什么也没听到。
这是我的代码:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class SoudTest {
private SourceDataLine sLine;
private AudioFormat audioFormat;
private AudioInputStream audioInputStream;
private AudioInputStream audioInputStream2;
private ByteArrayOutputStream byteArrayOutputStream= new ByteArrayOutputStream();;
private File file = new File ("test.wav");
private File file2 = new File ("test2.wav");
SoudTest () {
/*
* The aim of this code is to test the possibility or creating a mixing console in JAVA like the physical mixing consoles
* First step : Create an inputstream from a file, a microphone or anything else
* This is like plugging a source to a line-in of a stripe of a mixing console
* Second : Reading this input stream and create a byte array
* This byte array will be sent to the "master output" of the mixing console
* Question to answer later : how to create a byte array that will act like a buffer for the next steps...
* Third : Using this byte array to create an audioInputStream
* This step is like having the "master volume" getting what is sent by the stripe.
* Fourth : reading this audioInputStream
* The sound should flow out to the speaker
*/
//1st step
try {
audioInputStream=AudioSystem.getAudioInputStream(file);
} catch (Exception e) {
e.printStackTrace();
}
AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
//2nd step
try {
AudioSystem.write(audioInputStream, targetType, byteArrayOutputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Finally...");
//tLine.close();
System.out.println("Line closed");
try {
audioInputStream.close();
System.out.println("Stream closed.");
} catch (Exception e) {
e.printStackTrace();
}
}
/////////////////////////////////////////////////////////////
//3rd step
System.out.println("Size of the outputStream : "+byteArrayOutputStream.size());
byte[] byteArray = byteArrayOutputStream.toByteArray();
System.out.println("Size of byte array : "+byteArray.length);
ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
audioFormat = new AudioFormat(44100, 16, 2, true, false);
audioInputStream2=new AudioInputStream(bis, audioFormat, 1024);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
//System.out.println(info);
sLine=(SourceDataLine) AudioSystem.getLine(info);
System.out.println(sLine.getLineInfo());
} catch (Exception e) {
e.printStackTrace();
}
try {
sLine.open(audioFormat);
} catch (Exception e){
e.printStackTrace();
}
sLine.start();
System.out.println("Line Started");
//4th step
try {
byte bytes[] = new byte[1024];
int bytesRead=0;
int loop=0;
while ((bytesRead=audioInputStream2.read(bytes, 0, bytes.length))!= -1) {
//getVolumeLevel(bytes);
try {
sLine.write(bytes, 0, bytesRead);
System.out.println(loop);
} catch (Exception e) {
e.printStackTrace();
}
loop+=1;
}
System.out.println("No bytes anymore !");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Line stopped");
}
}
这是我控制台的输出:
终于……
线路关闭
流已关闭。
输出流的大小:35527724
字节数组的大小:35527724
接口 SourceDataLine 支持格式 PCM_SIGNED 未知采样率、16 位、立体声、4 bytes/frame、小端字节序和 0 到 1000000 字节的缓冲区
行开始
0
1个
2个
3
没有字节了!
线路停止
我的原始文件的大小为 35.5 Mo,我的字节数组的长度为 35.527.727,所以看起来是可以的。然后,我的 "chunk" 数组的大小为 1024,因此,我预计大约有 37700 个循环。那么为什么我的 "loop count" 停在 "3" 处?
我希望我的代码在注释中足够清楚。感谢您的帮助 ;-)
好吧,我想:我很蠢。当我创建 audioInputStream2 时,我只是没有充分注意最后一个参数。
而不是:audioInputStream2=new AudioInputStream(bis, audioFormat, 1024);
我应该写成:audioInputStream2=new AudioInputStream(bis, audioFormat, byteArray.length);
我仍在努力在 JAVA 中创建一个调音台的小项目,但我遇到了新障碍。 我正在尝试从 byteArray 创建一个 audioInputStream,然后读取它来听声音。但是我的代码有问题:我什么也没听到。
这是我的代码:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class SoudTest {
private SourceDataLine sLine;
private AudioFormat audioFormat;
private AudioInputStream audioInputStream;
private AudioInputStream audioInputStream2;
private ByteArrayOutputStream byteArrayOutputStream= new ByteArrayOutputStream();;
private File file = new File ("test.wav");
private File file2 = new File ("test2.wav");
SoudTest () {
/*
* The aim of this code is to test the possibility or creating a mixing console in JAVA like the physical mixing consoles
* First step : Create an inputstream from a file, a microphone or anything else
* This is like plugging a source to a line-in of a stripe of a mixing console
* Second : Reading this input stream and create a byte array
* This byte array will be sent to the "master output" of the mixing console
* Question to answer later : how to create a byte array that will act like a buffer for the next steps...
* Third : Using this byte array to create an audioInputStream
* This step is like having the "master volume" getting what is sent by the stripe.
* Fourth : reading this audioInputStream
* The sound should flow out to the speaker
*/
//1st step
try {
audioInputStream=AudioSystem.getAudioInputStream(file);
} catch (Exception e) {
e.printStackTrace();
}
AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
//2nd step
try {
AudioSystem.write(audioInputStream, targetType, byteArrayOutputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Finally...");
//tLine.close();
System.out.println("Line closed");
try {
audioInputStream.close();
System.out.println("Stream closed.");
} catch (Exception e) {
e.printStackTrace();
}
}
/////////////////////////////////////////////////////////////
//3rd step
System.out.println("Size of the outputStream : "+byteArrayOutputStream.size());
byte[] byteArray = byteArrayOutputStream.toByteArray();
System.out.println("Size of byte array : "+byteArray.length);
ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
audioFormat = new AudioFormat(44100, 16, 2, true, false);
audioInputStream2=new AudioInputStream(bis, audioFormat, 1024);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
//System.out.println(info);
sLine=(SourceDataLine) AudioSystem.getLine(info);
System.out.println(sLine.getLineInfo());
} catch (Exception e) {
e.printStackTrace();
}
try {
sLine.open(audioFormat);
} catch (Exception e){
e.printStackTrace();
}
sLine.start();
System.out.println("Line Started");
//4th step
try {
byte bytes[] = new byte[1024];
int bytesRead=0;
int loop=0;
while ((bytesRead=audioInputStream2.read(bytes, 0, bytes.length))!= -1) {
//getVolumeLevel(bytes);
try {
sLine.write(bytes, 0, bytesRead);
System.out.println(loop);
} catch (Exception e) {
e.printStackTrace();
}
loop+=1;
}
System.out.println("No bytes anymore !");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Line stopped");
}
}
这是我控制台的输出:
终于…… 线路关闭
流已关闭。
输出流的大小:35527724
字节数组的大小:35527724
接口 SourceDataLine 支持格式 PCM_SIGNED 未知采样率、16 位、立体声、4 bytes/frame、小端字节序和 0 到 1000000 字节的缓冲区
行开始
0 1个 2个 3
没有字节了! 线路停止
我的原始文件的大小为 35.5 Mo,我的字节数组的长度为 35.527.727,所以看起来是可以的。然后,我的 "chunk" 数组的大小为 1024,因此,我预计大约有 37700 个循环。那么为什么我的 "loop count" 停在 "3" 处?
我希望我的代码在注释中足够清楚。感谢您的帮助 ;-)
好吧,我想:我很蠢。当我创建 audioInputStream2 时,我只是没有充分注意最后一个参数。
而不是:audioInputStream2=new AudioInputStream(bis, audioFormat, 1024);
我应该写成:audioInputStream2=new AudioInputStream(bis, audioFormat, byteArray.length);