您的位置:首页 > 其它

录音及播放时常见bug

2017-05-08 11:33 281 查看
直接上代码:

class RenderThread extends Thread {
private int mCurrentFrame = 0;
private boolean isAudioFrameNeedIgnore() {
return mCurrentFrame < AUDIO_FRAMES_TO_IGNORE_COUNT;
}

@Override
public void run() {
try {
byte[] buffer = new byte[RECORD_BUF_SIZE];
Log.e(TAG, "RenderThread, interrupted = " + Thread.interrupted());
while (!Thread.interrupted()) {
Log.i(TAG, "RenderThread: run, isRender = " + isRender());
if (isRender()) {
// Speaker mode or BT a2dp mode will come here and keep reading and writing.
// If we want FM sound output from speaker or BT a2dp, we must record data
// to AudioRecrd and write data to AudioTrack.
//start modify by wqtest,topwise for bug 21942 ,2017.03.16
if (mAudioRecord == null){
initAudioRecordSink();
}
if (buffer == null){
buffer = new byte[RECORD_BUF_SIZE];
}
//end modify by wqtest,topwise for bug 21942 ,2017.03.16
if (mAudioRecord.getState() == AudioRecord.STATE_INITIALIZED
&& mAudioRecord.getRecordingState() ==
AudioRecord.RECORDSTATE_STOPPED) {
mAudioRecord.startRecording();
}

if (mAudioTrack.getState() == AudioTrack.STATE_INITIALIZED
&& mAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_STOPPED) {
mAudioTrack.play();
}
int size = mAudioRecord.read(buffer, 0, RECORD_BUF_SIZE);
// check whether need to ignore first 3 frames audio data from AudioRecord
// to avoid pop noise.
if (isAudioFrameNeedIgnore()) {
mCurrentFrame += 1;
synchronized (mRenderingLock) {
Log.i(TAG, "RenderThread: notifying for mRenderingLock");
mRenderingLock.notify();
}
continue ;
}
if (size <= 0) {
Log.e(TAG, "RenderThread read data from AudioRecord "
+ "error size: " + size);
synchronized (mRenderingLock) {
Log.i(TAG, "RenderThread: notifying for mRenderingLock");
mRenderingLock.notify();
}
continue;
}
byte[] tmpBuf = new byte[size];
System.arraycopy(buffer, 0, tmpBuf, 0, size);

// Check again to avoid noises, because mIsRender may be changed
// while AudioRecord is reading.
if (isRender()) {
mAudioTrack.write(tmpBuf, 0, tmpBuf.length);
}
synchronized (mRenderingLock) {
Log.i(TAG, "RenderThread: notifying for mRenderingLock");
mRenderingLock.notify();
}
} else {
// Earphone mode will come here and wait.
mCurrentFrame = 0;
try {
if (mAudioTrack != null &&
mAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
mAudioTrack.stop();

c183
}

if (mAudioRecord != null &&
mAudioRecord.getRecordingState() ==
AudioRecord.RECORDSTATE_RECORDING) {
mAudioRecord.stop();
}
} catch (IllegalStateException e) {
Log.e(TAG, "RenderThread.run, IllegalStateException");
} finally {
synchronized (mRenderLock) {
Log.i(TAG, "RenderThread: waiting for mRenderLock");
mRenderLock.wait();
}
}
}
}
} catch (InterruptedException e) {
Log.d(TAG, "RenderThread.run, thread is interrupted, need exit thread");
//start modify by wqtest ,topwise for bug 21942 ,2017.03.16
}catch (NullPointerException e1){
Log.i(TAG, "initAudioRecordSink: fail");
//end modify by wqtest ,topwise for bug 21942 ,2017.03.16
} finally {
if (mAudioRecord != null &&
(mAudioRecord.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING)) {
mAudioRecord.stop();
}

if (mAudioTrack != null &&
(mAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING)) {
mAudioTrack.stop();
}
}
}
}


private synchronized boolean initAudioRecordSink() {
mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.RADIO_TUNER,
SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, RECORD_BUF_SIZE);
if (mAudioRecord == null) {
return false;
}
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, RECORD_BUF_SIZE, AudioTrack.MODE_STREAM);
Log.d(TAG, "initAudioRecordSink, mAudioRecord = " + mAudioRecord + ",mAudioTrack = "
+ mAudioTrack);

if (mAudioTrack == null) {
if (mAudioRecord != null) {
mAudioRecord = null;
}
return false;
}
return true;
}

 1)int size = mAudioRecord.read(buffer, 0, RECORD_BUF_SIZE);报Nullpointerxception错误是因为mAudioRecord
== null或buffer
== null的出现,现做了为空时的重新初始化处理及Nullpointerxception错误异常捕获处理。

2)mAudioTrack.play();报illegalStateException错误是因为mAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_STOPPED时不能执行mAudioTrack.play()已做判断处理。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: