3. Setupfunc 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?)
...
}
4. 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
}
}
}
5. 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
9. 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