Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to set audio session playback category #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions Sources/Piano.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down