From 392e2f36cdb22fb980308bbcb84aae311854ae55 Mon Sep 17 00:00:00 2001 From: Andrew Milham Date: Sun, 11 Feb 2018 14:03:09 -0800 Subject: [PATCH] add ability to set audio session playback category --- Sources/Piano.swift | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Sources/Piano.swift b/Sources/Piano.swift index c5830fe..0ff2582 100644 --- a/Sources/Piano.swift +++ b/Sources/Piano.swift @@ -49,6 +49,17 @@ public class Piano { /// Holds all the scheduled Timers with music private var timers = [Timer]() + /// Handle allowing developers to set the sound playback category + public static var sessionCategory: String { + get { + return Piano.default.audioPlayerSessionCategory + } + set { + Piano.default.audioPlayerSessionCategory = newValue + } + } + private var audioPlayerSessionCategory = AVAudioSessionCategoryPlayback + private init() { } /// Wakes the Taptic Engine up from an idle state @@ -102,7 +113,7 @@ public class Piano { return } do { - try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) + try AVAudioSession.sharedInstance().setCategory(audioPlayerSessionCategory) try AVAudioSession.sharedInstance().setActive(true) player = try AVAudioPlayer(data: asset.data, fileTypeHint: nil) if let player = player { @@ -132,7 +143,7 @@ public class Piano { return } do { - try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) + try AVAudioSession.sharedInstance().setCategory(audioPlayerSessionCategory) try AVAudioSession.sharedInstance().setActive(true) player = try AVAudioPlayer(contentsOf: url) if let player = player { @@ -157,7 +168,7 @@ public class Piano { /// - completion: completion handler private func playAudio(from url: URL, completion: (() -> Void)?) { do { - try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) + try AVAudioSession.sharedInstance().setCategory(audioPlayerSessionCategory) try AVAudioSession.sharedInstance().setActive(true) player = try AVAudioPlayer(contentsOf: url) if let player = player {