Created
August 6, 2016 20:49
-
-
Save plaudev/db3582794e16ae134cb7a27cfe25e220 to your computer and use it in GitHub Desktop.
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
// does NOT resume play at all | |
// what's different = no .MixWithOthers at all | |
/* console output: | |
AVAudioSession Category Playback OK | |
AVAudioSession is Active | |
handleInterruption | |
began | |
audioPlayer.playing false | |
AVAudioSession is inactive | |
handleInterruption | |
ended | |
should resume | |
The operation couldn’t be completed. (OSStatus error 560557684.) | |
*/ | |
// | |
// ViewController.swift | |
// test audio interruption | |
// | |
import UIKit | |
import AVFoundation | |
class ViewController: UIViewController { | |
var player = AVAudioPlayer() | |
let audioPath = NSBundle.mainBundle().pathForResource("rachmaninov-romance-sixhands-alianello", ofType: "mp3")! | |
let theSession = AVAudioSession.sharedInstance() | |
/* | |
func handleInterruption(notification: NSNotification) { | |
//guard let interruptionType = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? AVAudioSessionInterruptionType else { print("wrong type"); return } | |
if notification.name != AVAudioSessionInterruptionNotification | |
|| notification.userInfo == nil{ | |
return | |
} | |
var info = notification.userInfo! | |
var intValue: UInt = 0 | |
(info[AVAudioSessionInterruptionTypeKey] as! NSValue).getValue(&intValue) | |
if let interruptionType = AVAudioSessionInterruptionType(rawValue: intValue) { | |
switch interruptionType { | |
case .Began: | |
print("began") | |
// player is paused and session is inactive. need to update UI) | |
player.pause() | |
print("audio paused") | |
default: | |
print("ended") | |
/** / | |
if let option = notification.userInfo?[AVAudioSessionInterruptionOptionKey] as? AVAudioSessionInterruptionOptions where option == .ShouldResume { | |
// ok to resume playing, re activate session and resume playing | |
// need to update UI | |
player.play() | |
print("audio resumed") | |
} | |
/ **/ | |
player.play() | |
print("audio resumed") | |
} | |
} | |
} | |
*/ | |
/* | |
func handleInterruption(notification: NSNotification) { | |
// new from Leo Dabus http://stackoverflow.com/a/38800403/1827488 | |
print("handleInterruption") | |
guard let value = (notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? NSNumber)?.unsignedIntegerValue, | |
let interruptionType = AVAudioSessionInterruptionType(rawValue: value) | |
else { | |
print("notification.userInfo?[AVAudioSessionInterruptionTypeKey]", notification.userInfo?[AVAudioSessionInterruptionTypeKey]) | |
return } | |
switch interruptionType { | |
case .Began: | |
print("began") | |
print("audioPlayer.playing", player.playing) | |
// player is paused and session is inactive. need to update UI) | |
default : | |
print("ended") | |
if let optionValue = (notification.userInfo?[AVAudioSessionInterruptionOptionKey] as? NSNumber)?.unsignedIntegerValue where AVAudioSessionInterruptionOptions(rawValue: optionValue) == .ShouldResume { | |
print("should resume") | |
// ok to resume playing, re activate session and resume playing | |
// need to update UI | |
do { | |
try theSession.setActive(true) | |
player.play() | |
} catch let error as NSError { | |
print(error.localizedDescription) | |
} | |
} | |
} | |
} | |
*/ | |
func handleInterruption(notification: NSNotification) { | |
print("handleInterruption") | |
guard let value = (notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? NSNumber)?.unsignedIntegerValue, | |
let interruptionType = AVAudioSessionInterruptionType(rawValue: value) | |
else { | |
print("notification.userInfo?[AVAudioSessionInterruptionTypeKey]", notification.userInfo?[AVAudioSessionInterruptionTypeKey]) | |
return } | |
switch interruptionType { | |
case .Began: | |
print("began") | |
player.pause() | |
print("audioPlayer.playing", player.playing) | |
/**/ | |
do { | |
try theSession.setActive(false) | |
print("AVAudioSession is inactive") | |
} catch let error as NSError { | |
print(error.localizedDescription) | |
} | |
/**/ | |
// player is paused and session is inactive. need to update UI) | |
default : | |
print("ended") | |
if let optionValue = (notification.userInfo?[AVAudioSessionInterruptionOptionKey] as? NSNumber)?.unsignedIntegerValue where AVAudioSessionInterruptionOptions(rawValue: optionValue) == .ShouldResume { | |
print("should resume") | |
// ok to resume playing, re activate session and resume playing | |
/**/ | |
do { | |
try theSession.setActive(true) | |
print("AVAudioSession is Active again") | |
player.play() | |
} catch let error as NSError { | |
print(error.localizedDescription) | |
} | |
/**/ | |
// need to update UI | |
//player.play() | |
} | |
} | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
do { | |
try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath)) | |
player.numberOfLoops = -1 // play indefinitely | |
player.prepareToPlay() | |
//player.delegate = player | |
} catch { | |
// process error here | |
} | |
// enable play in background http://stackoverflow.com/a/30280699/1827488 but this audio still gets interrupted by alerts | |
do { | |
try theSession.setCategory(AVAudioSessionCategoryPlayback) | |
//try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) | |
print("AVAudioSession Category Playback OK") | |
do { | |
try theSession.setActive(true) | |
//try AVAudioSession.sharedInstance().setActive(true) | |
print("AVAudioSession is Active") | |
} catch let error as NSError { | |
print(error.localizedDescription) | |
} | |
} catch let error as NSError { | |
print(error.localizedDescription) | |
} | |
// will this resume play? http://stackoverflow.com/a/32635113/1827488 | |
/** / | |
do { | |
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.MixWithOthers) | |
//try theSession.setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.MixWithOthers) | |
} catch { | |
} | |
/ **/ | |
// add observer to handle audio interruptions | |
// using 'object: nil' does not have a noticeable effect | |
//let theSession = AVAudioSession.sharedInstance() | |
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleInterruption(_:)), name: AVAudioSessionInterruptionNotification, object: theSession) | |
// start playing audio | |
player.play() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment