Skip to content

Instantly share code, notes, and snippets.

@HackingGate
Created July 1, 2018 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HackingGate/66f68389d7b233e904b06bdbff484f60 to your computer and use it in GitHub Desktop.
Save HackingGate/66f68389d7b233e904b06bdbff484f60 to your computer and use it in GitHub Desktop.
Silent Camera for iOS
import MediaPlayer
class ShutterSoundCanceller {
static func playShutterInvertSound() {
if let soundURL = Bundle.main.url(forResource: "photoShutterAntiSound", withExtension: "caf") {
do {
// Play sound even in silent mode
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeMeasurement, options: [])
} catch {
print(error)
}
do {
// Temporarily changes the current audio route to speaker
// TODO: For J and KH only
try AVAudioSession.sharedInstance().overrideOutputAudioPort(.speaker)
} catch {
// If category wasn't playAndRecord, route to speaker will fail.
print(error)
}
do {
// Active
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
do {
let player = try AVAudioPlayer(contentsOf: soundURL)
player.play()
} catch {
print(error)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment