通过套接字从 android 向 PC 发送语音时出现噪音
Noise in sending voice from android to PC over socket
我正在尝试通过套接字从 android 向 PC 发送语音。我的代码可以运行,但是在PC端我听到很多噪音,我什至听不到任何声音。
这是我正在使用的 android 代码:
byte buffer[]=new byte[1024];
int buffersize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
mic=new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize);
mic.startRecording();
while(running){
count=mic.read(buffer, 0, buffer.length);
if(count>0){
OutputStream.write(buffer, 0, count);
}
}
这是 PC 代码:
float sampleRate=44100;
int sampleSize=16;
int channel=1;
boolean sign=true;
boolean bigendian=true;
AudioFormat format=new AudioFormat(sampleRate, sampleSize, channel, sign, bigendian);
SourceDataLine voiceLine;
DataLine.Info LineInfo=new DataLine.Info(SourceDataLine.class, format);
if(AudioSystem.isLineSupported(LineInfo)){
System.out.println("Line Supported...");
}else{
System.out.println("not supported Line...");
}
voiceLine = (SourceDataLine) AudioSystem.getLine(LineInfo);
voiceLine.open(format);
voiceLine.start();
byte buffer[] = new byte[1024];
inputStream=new BufferedInputStream(connection.getInputStream());
while(true){
count=inputStream.read(buffer,0,buffer.length);
InputStream in;
in=new ByteArrayInputStream(buffer);
AudioInputStream ais=new AudioInputStream(in, format, buffer.length /format.getFrameSize());
ais.read(audio, 0, count);
voiceLine.write(audio, 0, count);
}
到目前为止我测试的内容:
- 用android到android检查时语音清晰。
- 将头phone连接到android phone.
时声音清晰
- 没有头的话声音不清晰phone附上androidphone(噪音很大)
到目前为止我尝试过的:
- 使用较少的采样率 (8000)。
- 使用 UDP 而不是 TCP。
- 尝试不同的缓冲区大小。
但是一无所获
最后我自己解决了这个问题。
原来只好改成bigendian=false;
,现在可以了
我正在尝试通过套接字从 android 向 PC 发送语音。我的代码可以运行,但是在PC端我听到很多噪音,我什至听不到任何声音。
这是我正在使用的 android 代码:
byte buffer[]=new byte[1024];
int buffersize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
mic=new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize);
mic.startRecording();
while(running){
count=mic.read(buffer, 0, buffer.length);
if(count>0){
OutputStream.write(buffer, 0, count);
}
}
这是 PC 代码:
float sampleRate=44100;
int sampleSize=16;
int channel=1;
boolean sign=true;
boolean bigendian=true;
AudioFormat format=new AudioFormat(sampleRate, sampleSize, channel, sign, bigendian);
SourceDataLine voiceLine;
DataLine.Info LineInfo=new DataLine.Info(SourceDataLine.class, format);
if(AudioSystem.isLineSupported(LineInfo)){
System.out.println("Line Supported...");
}else{
System.out.println("not supported Line...");
}
voiceLine = (SourceDataLine) AudioSystem.getLine(LineInfo);
voiceLine.open(format);
voiceLine.start();
byte buffer[] = new byte[1024];
inputStream=new BufferedInputStream(connection.getInputStream());
while(true){
count=inputStream.read(buffer,0,buffer.length);
InputStream in;
in=new ByteArrayInputStream(buffer);
AudioInputStream ais=new AudioInputStream(in, format, buffer.length /format.getFrameSize());
ais.read(audio, 0, count);
voiceLine.write(audio, 0, count);
}
到目前为止我测试的内容:
- 用android到android检查时语音清晰。
- 将头phone连接到android phone. 时声音清晰
- 没有头的话声音不清晰phone附上androidphone(噪音很大)
到目前为止我尝试过的:
- 使用较少的采样率 (8000)。
- 使用 UDP 而不是 TCP。
- 尝试不同的缓冲区大小。
但是一无所获
最后我自己解决了这个问题。
原来只好改成bigendian=false;
,现在可以了