background audio playback

10
iOS Background Audio Playback Korhan Bircan @CocoaConf San Jose, 2015

Upload: korhan-bircan

Post on 22-Jan-2018

590 views

Category:

Technology


0 download

TRANSCRIPT

iOS Background Audio Playback

Korhan Bircan

@CocoaConf San Jose, 2015

Available Frameworks

• MediaPlayer

• AV Foundation

• Audio Toolbox

• Audio Unit

• OpenAL

Setup func setUpWithPath(path : NSURL) { do { player = try AVAudioPlayer(contentsOfURL: path) if let player = player { player.delegate = self // Prepares the audio player for playback by preloading its buffers. player.prepareToPlay() // Communicate to the media services daemon our intent to use audio. try AVAudioSession.sharedInstance().setActive(true) } } catch let error as NSError { print(error.localizedDescription) } }

public class AVAudioPlayer : NSObject { public func play() -> Bool public func playAtTime(time: NSTimeInterval) -> Bool public func pause() public func stop() public var playing: Bool { get } public var numberOfChannels: Int { get } public var duration: NSTimeInterval { get } public var pan: Float public var volume: Float public var currentTime: NSTimeInterval public var rate: Float ... }

public protocol AVAudioPlayerDelegate : NSObjectProtocol { optional public func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) optional public func audioPlayerDecodeErrorDidOccur(player: AVAudioPlayer, error: NSError?) ... }

Lock screen controls MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = [MPMediaItemPropertyTitle : title, MPMediaItemPropertyAlbumTitle : subtitle, MPMediaItemPropertyPlaybackDuration: duration!, MPNowPlayingInfoPropertyPlaybackRate: NSNumber(integer: 1), MPMediaItemPropertyArtwork: MPMediaItemArtwork(image: image)]

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

application.beginReceivingRemoteControlEvents() ... }

override func remoteControlReceivedWithEvent(event: UIEvent?) { super.remoteControlReceivedWithEvent(event)

let player = AudioPlayer.sharedInstance if let event = event { switch event.subtype { case .RemoteControlPlay: ... case .RemoteControlPause: ... case .RemoteControlTogglePlayPause: ... case .RemoteControlStop: ... case .RemoteControlBeginSeekingForward: ... case .RemoteControlEndSeekingForward: ... case .RemoteControlEndSeekingBackward: ... case .RemoteControlBeginSeekingBackward: ... case .RemoteControlNextTrack: ... case .RemoteControlPreviousTrack: ... default: break } } }

Interruptions var mode = AVAudioSessionModeDefault /* AVAudioSessionModeVoiceChat AVAudioSessionModeGameChat AVAudioSessionModeVideoRecording AVAudioSessionModeMoviePlayback AVAudioSessionModeVideoChat AVAudioSessionCategoryOptionDuckOthers ... */ if #available(iOS 9.0, *) { mode = AVAudioSessionModeSpokenAudio } let audioSession = AVAudioSession.sharedInstance() try audioSession.setCategory(AVAudioSessionCategoryPlayback) /* AVAudioSessionCategoryAmbient AVAudioSessionCategoryRecord AVAudioSessionCategoryPlayAndRecord ... */ try audioSession.setMode(mode)

// Registered listeners will be notified when the system has interrupted // the audio session and when the interruption has ended. NSString *const AVAudioSessionInterruptionNotification

Backgrounding

Data Protection

App Transport Security

Summary

• Choose the right technology

• Be ready to be interrupted

• Opt-in to backgrounding and don’t be hostile

• Understand capabilities you’re opting in

• Watch out for App Transport Security

Swifty PodcastsQuestions&Comments: [email protected]