AudioRecorder在Android的错误无效的缓冲区大小 [英] AudioRecorder in Android Error Invalid buffer size
本文介绍了AudioRecorder在Android的错误无效的缓冲区大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要记录并同时播放音频和我使用2线程it.here的帮助是code
I need to record and play audio simultaneously and i use the help of 2 threads for it.here is the code
recorder = new AudioRecord(AudioSource.MIC, 8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize);
if (recorder.getState() == android.media.AudioRecord.STATE_INITIALIZED)
recorder.startRecording();
isRecording = true;
audioPlayer = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize,
AudioTrack.MODE_STREAM);
audioPlayer.play();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
{
readBytes = recorder.read(data, 0, bufferSize);
if (readBytes == AudioRecord.ERROR_INVALID_OPERATION)
System.out.println("ERROR_INVALID_OPERATION");
else if (readBytes == AudioRecord.ERROR_BAD_VALUE)
System.out.println("ERROR_BAD_VALUE");
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (readBytes > 0)
audioPlayer.write(data, 0, readBytes);
}
}).start();
我收到以下错误: 02-23 14:19:59.625:E / AudioTrack(1786):无效的缓冲区大小:minFrameCount 2400,frameCount 2048
I get the error below: 02-23 14:19:59.625: E/AudioTrack(1786): Invalid buffer size: minFrameCount 2400, frameCount 2048
在此先感谢
推荐答案
这在$ C $词用来解决问题的希望它可以帮助
This the code i used to solve the issue hope it helps
公共类聊天延伸活动{
Devc devc;
byte[] bytesent, bytesgot;
private BluetoothSocket mmSocket = null;
private InputStream mmInStream;
private OutputStream mmOutStream;
Handler handler;
Button send;
TextView getmessage;
EditText sentmessage;
TextView nowsentmessage;
TextView you;
int bytes = 0;
Thread thread;
private int audioSource = MediaRecorder.AudioSource.MIC;
private int samplingRate = 44100; /* in Hz */
private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
private int bufferSize = AudioRecord.getMinBufferSize(samplingRate,
channelConfig, audioFormat);
private int sampleNumBits = 16;
private int numChannels = 1;
AudioTrack audioPlayer;
AudioRecord recorder;
Boolean isRecording;
byte[] bytes2;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chat);
getmessage = (TextView) findViewById(R.id.gottext);
sentmessage = (EditText) findViewById(R.id.entertext);
nowsentmessage = (TextView) findViewById(R.id.metext);
you = (TextView) findViewById(R.id.you);
send = (Button) findViewById(R.id.sent);
bufferSize += 2048;
recorder = new AudioRecord(audioSource, samplingRate, channelConfig,
audioFormat, 44100);
audioPlayer = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize,
AudioTrack.MODE_STREAM);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// display each item in a single line
}
};
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mmSocket = Devc.bluetoothSocket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = mmSocket.getInputStream();
tmpOut = mmSocket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
recorder.startRecording();
isRecording = true;
while (true) {
int readBytes = 0;
readBytes = recorder.read(bytesent, 0, bufferSize);
if (readBytes > 0) {
write(bytesent);
bufferSize += 2048;
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub\
while (true) {
int readBytes = 0;
try {
bytes = mmInStream.read(bytesgot);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
audioPlayer.write(bytesgot, 0, bytes);
audioPlayer.play();
}
}
}).start();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
try {
mmInStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mmOutStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cancel();
}
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
mmOutStream.flush();
} catch (IOException e) {
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
cancel();
thread.stop();
Chat.this.finish();
// thread.stop();
}
}
这篇关于AudioRecorder在Android的错误无效的缓冲区大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文