AVFoundation,如何在captureStillImageAsynchronouslyFromConnection时关闭快门声音?
我正在尝试通过 AVFoundation captureStillImageAsynchronouslyFromConnection< 在相机实时预览期间捕获图像/a>.到目前为止,该程序按预期运行。但是,如何才能静音快门声音呢?
I am trying to capture an image during a live preview from the camera, by AVFoundation captureStillImageAsynchronouslyFromConnection. So far the program works as expected. However, how can I mute the shutter sound?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我使用此代码一次来捕获iOS默认快门声音(这里是声音文件名列表https://github.com/TUNER88/iOSSystemSoundsLibrary):
然后我使用第三方应用程序从 Documents 目录(DiskAid for Mac)中提取
photoShutter.caf
。下一步,我在 Audacity 音频编辑器中打开photoShutter.caf
并应用反转效果,在高变焦下看起来像这样:然后我将此声音保存为
photoShutter2.caf
并尝试在captureStillImageAsynchronouslyFromConnection
之前播放此声音:这确实有效!我运行了几次测试,每次都没有听到快门声:)
您可以通过此链接获取在 iPhone 5S iOS 7.1.1 上捕获的倒置声音:https://www.dropbox.com/s/1echsi6ivbb85bv/photoShutter2.caf
I used this code once to capture iOS default shutter sound (here is list of sound file names https://github.com/TUNER88/iOSSystemSoundsLibrary):
Then I used third-party app to extract
photoShutter.caf
from Documents directory (DiskAid for Mac). Next step I openedphotoShutter.caf
in Audacity audio editor and applied inversion effect, it looks like this on high zoom:Then I saved this sound as
photoShutter2.caf
and tried to play this sound right beforecaptureStillImageAsynchronouslyFromConnection
:And this really works! I runs test several times, every time I hear no shutter sound :)
You can get already inverted sound, captured on iPhone 5S iOS 7.1.1 from this link: https://www.dropbox.com/s/1echsi6ivbb85bv/photoShutter2.caf
我在 Swift 中的解决方案
当您调用 AVCapturePhotoOutput.capturePhoto 方法来捕获图像时,如下面的代码所示。
将调用 AVCapturePhotoCaptureDelegate 方法。
并且系统会在
willCapturePhotoFor
调用后尝试播放快门声音。因此您可以在
willCapturePhotoFor
方法中处理系统声音。另请参阅
My Solution in Swift
When you call
AVCapturePhotoOutput.capturePhoto
method to capture an image like the below code.AVCapturePhotoCaptureDelegate methods will be invoked.
And the system tries to play shutter sound after
willCapturePhotoFor
invoked.So you can dispose of system sound in
willCapturePhotoFor
method.See also
方法 1: 不确定这是否有效,但请尝试在发送捕获事件之前播放空白音频文件。
要播放剪辑,请添加
Audio Toolbox
框架,#include
并在拍照之前立即播放音频文件:如果您需要,这里有一个空白音频文件。
https://d1sz9tkli0lfjq.cloudfront.net/items/0Y3Z0A1j1H2r1c0z3n3t/blank.wav
________________________________________________________________________________________________________________________________________
方法 2: 如果这不起作用,还有一种替代方法。只要您不需要很好的分辨率,就可以 从视频流中抓取一帧,从而完全避免图片声音。
_________________________________________________________________________________________________________________________
方法 3: 另一种方法是对您的应用程序进行“屏幕截图”。这样做:
如果您希望视频流预览充满整个屏幕,以便您的屏幕截图看起来不错:
Method 1: Not sure if this will work, but try playing a blank audio file right before you send the capture event.
To play a clip, add the
Audio Toolbox
framework,#include <AudioToolbox/AudioToolbox.h>
and play the audio file like this immediately before you take the picture:Here is a blank audio file if you need it.
https://d1sz9tkli0lfjq.cloudfront.net/items/0Y3Z0A1j1H2r1c0z3n3t/blank.wav
________________________________________________________________________________________________________________________________________
Method 2: There's also an alternative if this doesn't work. As long as you don't need to have a good resolution, you can grab a frame from the video stream, thus avoiding the picture sound altogether.
________________________________________________________________________________________________________________________________________
Method 3: Another way to do this would be to take a "screenshot" of your application. Do it this way:
If you're wanting this to fill the whole screen with a preview of the video stream so that your screenshot looks good:
我能够通过在 snapStillImage 函数中使用此代码来实现此功能,并且它在 iOS 8.3 iPhone 5 上非常适合我。我还确认,如果您使用此功能,Apple 不会拒绝您的应用程序(他们没有拒绝我的)
只要记住在你的应用程序返回时保持友善并取消静音即可!
I was able to get this to work by using this code in the snapStillImage function and it works perfectly for me on iOS 8.3 iPhone 5. I have also confirmed that Apple won't reject your app if you use this (they didn't reject mine)
Just remember to be nice and unmute it when your app returns!
我住在日本,所以出于安全原因我不能在拍照时将音频静音。但在视频中,音频会关闭。我不明白为什么。
我拍摄没有快门声音的照片的唯一方法是使用 AVCaptureVideoDataOutput 或 AVCaptureMovieFileOutput。对于分析静态图像 AVCaptureVideoDataOutput 是唯一的方法。在 AVFoundation 示例代码中,
在我的 3GS 中,当我设置 CMTimeMake(1, 1); 时,它非常重。 // 每秒一帧。
在 WWDC 2010 示例代码 FindMyiCone 中,我发现以下代码,
当使用此 API 时,不授予计时,但按顺序调用 API。我这是最好的解决方案。
I live in in Japan, so I can not mute the audio when we take photos for security reason. In video, however audio turns off. I don't understand why.
The only way I take a photo without shutter sound is using AVCaptureVideoDataOutput or AVCaptureMovieFileOutput. For analyze still image AVCaptureVideoDataOutput is only way. In AVFoundatation sample code,
In my 3GS it is very heavy when I set CMTimeMake(1, 1); // One frame per second.
In WWDC 2010 Sample code, FindMyiCone, I found following code,
When this API is used, the timing is not granted, but API is called sequentially. I this it is best solutions.
请参阅这篇文章以获得不同类型的答案:从图像缓冲区捕获图像。 视频预览期间的屏幕捕获失败
See this post for a different kind of answer: Capture the image from the image buffer. Screen capture during video preview fails
您还可以从视频流中获取一帧来捕获(非全分辨率)图像。
此处使用它来捕获图像短时间间隔:
You can also take a frame from a video stream to capture a (not full resolution) image.
It is used here to capture images at short intervals:
我能想到的唯一可能的解决方法是当他们按下“拍照”按钮时将 iPhone 声音静音,然后在一秒钟后取消静音。
The only possible work-around I can think of would be to mute the iphone sound when they press the "take picture" button, and then un-mute it a second later.
在这种情况下,一个常见的技巧是查明框架是否为此事件调用了某个方法,然后暂时覆盖该方法,从而使其无效。
很抱歉,但我不是一个足够好的黑客,无法立即告诉您在这种情况下这是否有效。您可以在框架可执行文件上尝试使用“nm”命令来查看是否有一个具有合适名称的命名函数,或者将 gdb 与模拟器一起使用来跟踪它的去向。
我相信,一旦您知道要覆盖什么,您就可以使用那些低级 ObjC 调度函数来重定向函数查找。我想我以前做过一次,但不记得细节了。
希望您可以使用我的提示来谷歌一些解决方案路径。祝你好运。
A common trick in such cases is to find out if the framework invokes a certain method for this event, and then to overwrite that method temporarily, thereby voiding its effect.
I'm sorry but I am not a good enough hack to tell you right away if that works in this case. You could try the "nm" command on the framework executables to see if there's a named function that has a suitatable name, or use gdb with the Simulator to trace where it goes.
Once you know what to overwrite, there are those low level ObjC dispatching functions that you can use to redirect the lookup for functions, I believe. I think I've done that once a while ago, but can't remember the details.
Hopefully, you can use my hints to google a few solutions paths to this. Good luck.