其实,Camera 拍照/摄像提示音是为了防止偷拍,业内有不成文规定,手机公司在做camera时,点击拍照和录像键的时候,必须要有提示音。因此,google也就非常人性化的将播放拍照声音的函数,放到了cameraService中,防止开发者能开发出不响的camera,从而只要调用拍照函数,一定会响,这是写死在framework中的。
Camera真正播放提示音的是在CameraService.cpp中的playSound()方法,这个方法提供给拍照/摄像时调用。
// /frameworks/base/services/camera/libcameraservice/CameraService.cpp
void CameraService::playSound(sound_kind kind) {
LOG1("playSound(%d)", kind);
Mutex::Autolock lock(mSoundLock);
sp<MediaPlayer> player = mSoundPlayer[kind];
if (player != 0) {
player->seekTo(0);
player->start();
}
}
下面就是在开始摄像时会调用playSound()方法播放提示音:
status_t CameraService::Client::startRecordingMode() {
LOG1("startRecordingMode");
status_t result = NO_ERROR;
// if recording has been enabled, nothing needs to be done
if (mHardware->recordingEnabled()) {
return NO_ERROR;
}
// if preview has not been started, start preview first
if (!mHardware->previewEnabled()) {
result = startPreviewMode();
if (result != NO_ERROR) {
return result;
}
}
// start recording mode
enableMsgType(CAMERA_MSG_VIDEO_FRAME);
mCameraService->playSound(SOUND_RECORDING);
result = mHardware->startRecording();
if (result != NO_ERROR) {
LOGE("mHardware->startRecording() failed with status %d", result);
}
return result;
}
有时,摄像的同时会将这个提示音也录入,要解决这个问题,可以在mCameraService->playSOund(SOUND_RECORDING)后加一个延时,但这个延时不能用sleep()方法(会报一些莫名其妙的问题,直接就stopReCording),可以用usleep()实现延时。
原文参考:http://blog.csdn.net/dyfleoo/article/details/7912021
来源:oschina
链接:https://my.oschina.net/u/811255/blog/145193