Created
July 1, 2018 06:33
-
-
Save HackingGate/66f68389d7b233e904b06bdbff484f60 to your computer and use it in GitHub Desktop.
Silent Camera for iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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