录音及播放时常见bug
2017-05-08 11:33
281 查看
直接上代码:
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()已做判断处理。
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()已做判断处理。
相关文章推荐
- bugfix:录音的时候,webview不能播放视频
- IOS 录音的时候 监测声音大小并显示出来当前声音大小 可播放(超级简陋版)
- android studio常见bug
- android播放音频文件(MediaPlayer)和录音(MediaRecorder)--录音
- Unity学习笔记(组件篇2) 视频文件的播放和录音
- RecyclerView开发中不常见bug解决
- HTML-IE6兼容性问题及IE6常见BUG详细汇总
- Leetcode 常见bug总结
- .net简单录音和播放音频文件,不用DirectX ,对于C/S 、B/S都适用
- Web项目添加Maven支持后,常见bug解决
- 常见的一些bug
- iOS录音和播放
- IE6兼容性问题及IE6常见bug
- [转贴]IE6出现BUG的十种常见解决方法
- SoundPool(播放小音频),MediaRecorder(录音),视频播放,开启摄像头
- 常见播放列表文件格式
- Android录音与播放
- 软件测试中UI测试常见BUG汇总
- 浏览器兼容之旅的第五站:IE常见Bug——part3
- 影音视频播放中常见八问题