public void surfaceCreated(SurfaceHolder holder) {
Log.d(SopCastConstant.TAG, “SurfaceView created”);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.d(SopCastConstant.TAG, “SurfaceView width:” + width + " height:" + height);
CameraHolder.instance().openCamera();
CameraHolder.instance().startPreview();
}
};
5. 停止预览
停止预览只需要释放掉相机资源即可:
public synchronized void releaseCamera() {
if (mState == State.PREVIEW) {
stopPreview();
}
if (mState != State.OPENED) {
return;
}
if (mCameraDevice == null) {
return;
}
mCameraDevice.release();
mCameraDevice = null;
mCameraData = null;
mState = State.INIT;
}
音频编码
AudioRecord 采集完之后需要对 PCM 数据进行实时的编码 (软编利用 libfaac 通过 NDK 交叉编译静态库、硬编使用 Android SDK MediaCodec 进行编码)。
软编
语音软编这里们用主流的编码库 libfaac 进行编码 AAC 语音格式数据。
1. 编译 libfaac
1.1 下载 libfaac
wget https://sourceforge.net/projects/faac/files/faac-src/faac-1.29/faac-1.29.9.2.tar.gz
1.2 编写交叉编译脚本
#!/bin/bash
#打包地址
PREFIX=pwd
/android/armeabi-v7a
#配置NDK 环境变量
NDK_ROOT=KaTeX parse error: Expected 'EOF', got '#' at position 10: NDK_HOME #̲指定 CPU CPU=arm-…NDK_ROOT/toolchains/$CPU-4.9/prebuilt/linux-x86_64
FLAGS=“-isysroot $NDK_ROOT/sysroot -isystem KaTeX parse error: Expected group after '_' at position 54: …-androideabi -D_̲_ANDROID_API__=ANDROID_API -U_FILE_OFFSET_BITS -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Wa,–noexecstack -Wformat -Werror=format-security -O0 -fPIC”
CROSS_COMPILE= T O O L C H A I N / b i n / a r m − l i n u x − a n d r o i d e a b i e x p o r t C C = " TOOLCHAIN/bin/arm-linux-androideabi export CC=" TOOLCHAIN/bin/arm−linux−androideabiexportCC="CROSS_COMPILE-gcc --sysroot= N D K R O O T / p l a t f o r m s / a n d r o i d − 17 / a r c h − a r m " e x p o r t C F L A G S = " NDK_ROOT/platforms/android-17/arch-arm" export CFLAGS=" NDKROOT/platforms/android−17/arch−arm"exportCFLAGS="FLAGS"
./configure
–prefix=$PREFIX
–host=arm-linux
–with-pic
–enable-shared=no
make clean
make install
2. CMakeLists.txt 配置
cmake_minimum_required(VERSION 3.4.1)
#语音编码器
set(faac KaTeX parse error: Expected 'EOF', got '#' at position 26: …RCE_DIR}/faac) #̲加载 faac 头文件目录 i…{faac}/include)
#指定 faac 静态库文件目录
set(CMAKE_CXX_FLAGS “ C M A K E C X X F L A G S − L {CMAKE_CXX_FLAGS} -L CMAKECXXFLA