我制作相机,现在我希望它在拍摄或录制时具有开/关声音功能
我可以将其固定为静音或取消静音,但如果我想要按钮切换,则无法正常工作
有声音: 将此代码添加到ShutterCallback:
AudioManager mgr = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
mgr.playSoundEffect(AudioManager.FLAG_PLAY_SOUND);
没有声音:在捕获之前添加此代码:
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);
此代码为Picture callback:
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, false);
答案 0 :(得分:2)
我找到了这种情况的解决方案:
取消静音:如上所述初始化shuter回调
Camera.ShutterCallback shutterCallback = null;
if (hasSound) {
shutterCallback = () -> {
AudioManager mgr = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
mgr.playSoundEffect(AudioManager.FLAG_PLAY_SOUND);
};
}
mCamera.takePicture(shutterCallback, null, mPicture);