程序的部分結(jié)構(gòu)是這樣的:錄制語音的線程每得到960字節(jié)的語音數(shù)據(jù),就發(fā)給處理線程的Handler,在handleMessage()中對語音數(shù)據(jù)進(jìn)行處理但是,就在發(fā)給handler到從handler取出,語音就發(fā)生了劣化。下面給出代碼與代碼解釋。在錄制語音的線程中,對AudioRecord對象設(shè)置OnRecordPositionUpdateListener,在 onPeriodicNotification()取出數(shù)據(jù):recorder.setPositionNotificationPeriod(480);//480frames = 960 bytes recorder.setRecordPositionUpdateListener(new MyUpdateListener());OnRecordPositionUpdateListener實(shí)現(xiàn)是這樣的:class MyUpdateListener implements OnRecordPositionUpdateListener{ byte[][] samples = new byte[10][960]; int i = 0; int bytesRead; Bundle data; Message msg; @Override public void onPeriodicNotification(AudioRecord recorder) { bytesRead = recorder.read(samples[i], 0, 960); if (bytesRead != 960) { Log.e(TAG, "bytesRead不是960"); } try { fos.write(samples[i], 0, bytesRead); } catch (IOException e) { Log.e(TAG,"File Write Error"); e.printStackTrace(); } data = new Bundle(); data.putByteArray(CommonConfig.lock, samples[i]); msg = handler.obtainMessage(); msg.setData(data); msg.sendToTarget(); i = i++ % 10;//錯誤,這樣寫和i = i % 10一樣 } }fos是一個FileOutputStream對象,用來保存錄得的語音;CommonConfig.lock是一個static final String。handler的handleMessage()是這樣的:public void handleMessage(Message msg) { byte[] buffer = null; buffer = msg.getData().getByteArray(CommonConfig.lock); if(buffer.length != 960){ Log.e(TAG,"buffer.length != 960"); } try { fos.write(buffer, 0, buffer.length); } catch (IOException e) { Log.e(TAG,"File Write Error"); e.printStackTrace(); } //...其他處理... }此處的fos是另一個FileOutputStream對象,與上一個fos一樣,保存的文件都為.pcm。在PC上Cool Edit Pro打開兩個PCM文件,發(fā)現(xiàn)音質(zhì)已經(jīng)發(fā)生了劣化,具體表現(xiàn)為聲音有些發(fā)顫。有人能解釋下這是為什么么?還是我的實(shí)現(xiàn)哪里有問題?
開發(fā)語音通信APP時遇到的音質(zhì)劣化問題
慕田峪4524236
2019-03-20 22:18:39