Demo :github.com/CarGuo/Reco…javascript
先看看效果┑( ̄Д  ̄)┍html
···
try {
mAudioRecord.startRecording();
} catch (Exception ex) {
ex.printStackTrace();
}
···
while (mIsRecording) {
int readSize = mAudioRecord.read(mPCMBuffer, 0, mBufferSize);
if (readSize == AudioRecord.ERROR_INVALID_OPERATION ||
readSize == AudioRecord.ERROR_BAD_VALUE) {
if (errorHandler != null && !sendError) {
sendError = true;
errorHandler.sendEmptyMessage(ERROR_TYPE);
mIsRecording = false;
isError = true;
}
} else {
if (readSize > 0) {
mEncodeThread.addTask(mPCMBuffer, readSize);
calculateRealVolume(mPCMBuffer, readSize);
sendData(mPCMBuffer, readSize);
} else {
if (errorHandler != null && !sendError) {
sendError = true;
errorHandler.sendEmptyMessage(ERROR_TYPE);
mIsRecording = false;
isError = true;
}
}
}
}複製代碼
其實就是音頻數據進行縮減,由於手機每次讀上來的音頻數據是比較大的,這和咱們配置的錄製參數也有關係,這裏使用的是44100HZ的參數,那麼咱們直接降頻就行了。(這波裝的能夠啊~)java
以下面所看到的,其實就一個for循環,首先把音頻數據的長度除了300(哈哈,一會兒少了好多),以後每300個裏面取出最大和最小值(音頻數據是正負數的),目前咱們用的是最大值,這樣把每300個裏面的最大值都存起來,就有了一個降頻後的音頻數據列表了。git
private void sendData(short[] shorts, int readSize) {
if (dataList != null) {
int length = readSize / 300;
short resultMax = 0, resultMin = 0;
for (short i = 0, k = 0; i < length; i++, k += 300) {
for (short j = k, max = 0, min = 1000; j < k + 300; j++) {
if (shorts[j] > max) {
max = shorts[j];
resultMax = max;
} else if (shorts[j] < min) {
min = shorts[j];
resultMin = min;
}
}
if (dataList.size() > maxSize) {
dataList.remove(0);
}
dataList.add(resultMax);
}
}
}複製代碼
是否是很簡單呢?是啊~確實很簡單( ̄o ̄) .那到這裏就結束88咯。github
咦,下面還有啊 (ˉ▽ ̄~) 切~~api
@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility == VISIBLE && mBackgroundBitmap == null) {
ViewTreeObserver vto = getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (getWidth() > 0 && getHeight() > 0) {
mWidthSpecSize = getWidth();
mHeightSpecSize = getHeight();
mBaseLine = mHeightSpecSize / 2;
mBackgroundBitmap = Bitmap.createBitmap(mWidthSpecSize, mHeightSpecSize, Bitmap.Config.ARGB_8888);
mBitmap = Bitmap.createBitmap(mWidthSpecSize, mHeightSpecSize, Bitmap.Config.ARGB_8888);
mBackCanVans.setBitmap(mBackgroundBitmap);
mCanvas.setBitmap(mBitmap);
ViewTreeObserver vto = getViewTreeObserver();
vto.removeOnPreDrawListener(this);
}
return true;
}
});
}
}複製代碼
/** * 根據當前塊數據來判斷縮放音頻顯示的比例 * * @param list 音頻數據 */
private void resolveToWaveData(ArrayList
list) { short allMax =
0;
for (Short sh : list) {
if (sh > allMax) { allMax = sh; } } int curScale = allMax / mBaseLine;
if (curScale > mScale) { mScale = ((curScale ==
0) ?
1 : curScale); } }
複製代碼
if (mBackCanVans != null) {
//洗白白
mBackCanVans.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
mBackCanVans.drawLine(0, mBaseLine, mWidthSpecSize, mBaseLine, mPaint);
int drawBufsize = dataList.size();
//判斷大小,是否改變顯示的比例
for (int i = 0, j = 0; i < drawBufsize; i++, j += mOffset) {
Short sh = dataList.get(i);
short max = (short) (mBaseLine - sh / mScale);
short min;
if (mWaveCount == 2) {
min = (short) (sh / mScale + mBaseLine);
} else {
min = (short) (mBaseLine);
}
mBackCanVans.drawLine(j, mBaseLine, j, max, mPaint);
mBackCanVans.drawLine(j, min, j, mBaseLine, mPaint);
}
synchronized (mLock) {
mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
mCanvas.drawBitmap(mBackgroundBitmap, 0, 0, mPaint);
}
Message msg = new Message();
msg.what = 0;
handler.sendMessage(msg);
}
···
@Override
protected void onDraw(Canvas c) {
super.onDraw(c);
if (mIsDraw && mBitmap != null) {
synchronized (mLock) {
c.drawBitmap(mBitmap, 0, 0, mViewPaint);
}
}
}複製代碼
最後~歡迎來我的的Github網絡