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

Background Audio Playback

Jan 22, 2018

Download

Technology

Korhan Bircan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Background Audio Playback

iOS Background Audio Playback

Korhan Bircan

@CocoaConf San Jose, 2015

Page 2: Background Audio Playback

Available Frameworks

• MediaPlayer

• AV Foundation

• Audio Toolbox

• Audio Unit

• OpenAL

Page 3: Background Audio Playback

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?) ... }

Page 4: Background Audio Playback

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 } } }

Page 5: Background Audio Playback

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

Page 6: Background Audio Playback

Backgrounding

Page 7: Background Audio Playback

Data Protection

Page 8: Background Audio Playback

App Transport Security

Page 9: Background Audio Playback

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

Page 10: Background Audio Playback

Swifty PodcastsQuestions&Comments: [email protected]