Skip to content

Instantly share code, notes, and snippets.

@ubik2
Created February 5, 2015 20:57
Show Gist options
  • Save ubik2/7b718c9ce6796197e0f4 to your computer and use it in GitHub Desktop.
Save ubik2/7b718c9ce6796197e0f4 to your computer and use it in GitHub Desktop.
func getSampleRate() {
var session: AVAudioSession = AVAudioSession.sharedInstance()
var audioSessionError: NSError?
session.setCategory(AVAudioSessionCategoryPlayback, error:&audioSessionError)
if (audioSessionError != nil) {
println("Error \(audioSessionError?.code), (audioSessionError?.localizedDescription)")
}
var bufferDuration = NSTimeInterval(0.005)
session.setPreferredIOBufferDuration(bufferDuration, error:&audioSessionError)
if (audioSessionError != nil) {
println("Error \(audioSessionError?.code), (audioSessionError?.localizedDescription)")
}
var sampleRate = 88200.0
session.setPreferredSampleRate(sampleRate, error:&audioSessionError)
if (audioSessionError != nil) {
println("Error \(audioSessionError?.code), (audioSessionError?.localizedDescription)")
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("handleRouteChange:"), name:AVAudioSessionRouteChangeNotification, object: session)
session.setActive(true, error:&audioSessionError)
if (audioSessionError != nil) {
println("Error \(audioSessionError?.code), (audioSessionError?.localizedDescription)")
}
sampleRate = session.sampleRate
bufferDuration = session.IOBufferDuration
println("Sample Rate: \(sampleRate)HZ I/O Buffer Duration:\(bufferDuration)")
}
func handleRouteChange(object:AnyObject) {
println("Got route change for playback")
getSampleRate()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment