0001 // 0002 // Sound.swift 0003 // AudioPlayer Sample 0004 // 0005 // Created by Tom Baranes on 15/01/16. 0006 // Copyright © 2016 tbaranes. All rights reserved. 0007 // 0008 0009 import Foundation 0010 import AVFoundation 0011 0012 public enum AudioPlayerError: ErrorType { 0013 case FileExtension
AudioPlayer.swift:94 throw AudioPlayerError.FileExtension0014 } 0015 0016 public class AudioPlayer
AudioPlayer.swift:94 throw AudioPlayerError.FileExtension: NSObject { 0017 0018 public static let SoundDidFinishPlayingNotification
AudioPlayer.swift:181 extension AudioPlayer: AVAudioPlayerDelegate {AudioPlayer.swift:189 private extension AudioPlayer {AudioPlayer.swift:200 NSNotificationCenter.defaultCenter().postNotificationName(AudioPlayer.SoundDidFinishPlayingNotification, object: self)= "SoundDidFinishPlayingNotification" 0019 public typealias SoundCompletionHandler
AudioPlayer.swift:200 NSNotificationCenter.defaultCenter().postNotificationName(AudioPlayer.SoundDidFinishPlayingNotification, object: self)= (didFinish: Bool) -> Void 0020 0021 /// Name of the used to initialize the object 0022 public let name
AudioPlayer.swift:28 internal var completionHandler: SoundCompletionHandler?: String? 0023 0024 /// URL of the used to initialize the object 0025 public let URL
AudioPlayer.swift:103 name = fileURL.lastPathComponent: NSURL? 0026 0027 /// A callback closure that will be called when the audio finishes playing, or is stopped. 0028 internal var completionHandler
AudioPlayer.swift:102 URL = fileURL: SoundCompletionHandler? 0029 0030 /// is it playing or not? 0031 public var playing
AudioPlayer.swift:196 if let nonNilCompletionHandler = completionHandler {: Bool { 0032 get { 0033 if let nonNilsound = sound { 0034 return nonNilsound.playing 0035 } 0036 return false 0037 } 0038 } 0039 0040 /// the duration of the sound. 0041 public var duration: NSTimeInterval { 0042 get { 0043 if let nonNilsound = sound { 0044 return nonNilsound.duration 0045 } 0046 return 0.0 0047 } 0048 } 0049 0050 /// currentTime is the offset into the sound of the current playback position. 0051 public var currentTime: NSTimeInterval { 0052 get { 0053 if let nonNilsound = sound { 0054 return nonNilsound.currentTime 0055 } 0056 return 0.0 0057 } 0058 set { 0059 sound?.currentTime = newValue 0060 } 0061 } 0062 0063 /// The volume for the sound. The nominal range is from 0.0 to 1.0. 0064 public var volume
AudioPlayer.swift:117 if playing == false {AudioPlayer.swift:123 if playing {: Float = 1.0 { 0065 didSet { 0066 volume = min(1.0, max(0.0, volume)); 0067 targetVolume = volume 0068 } 0069 } 0070 0071 /// "numberOfLoops" is the number of times that the sound will return to the beginning upon reaching the end. 0072 /// A value of zero means to play the sound just once. 0073 /// A value of one will result in playing the sound twice, and so on.. 0074 /// Any negative number will loop indefinitely until stopped. 0075 public var numberOfLoops
AudioPlayer.swift:66 volume = min(1.0, max(0.0, volume));AudioPlayer.swift:66 volume = min(1.0, max(0.0, volume));AudioPlayer.swift:67 targetVolume = volumeAudioPlayer.swift:140 volume = 0.0: Int = 0 { 0076 didSet { 0077 sound?.numberOfLoops = numberOfLoops 0078 } 0079 } 0080 0081 /// set panning. -1.0 is left, 0.0 is center, 1.0 is right. 0082 public var pan
AudioPlayer.swift:77 sound?.numberOfLoops = numberOfLoops: Float = 0.0 { 0083 didSet { 0084 sound?.pan = pan 0085 } 0086 } 0087 0088 // MARK: Init 0089 0090 public convenience init(fileName: String) throws { 0091 let fixedFileName = fileName.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet()) 0092 var soundFileComponents = fixedFileName.componentsSeparatedByString(".") 0093 if soundFileComponents.count == 1 { 0094 throw AudioPlayerError.FileExtension 0095 } 0096 let path = NSBundle.mainBundle().pathForResource(soundFileComponents[0], ofType: soundFileComponents[1]) 0097 try self.init(contentsOfPath: path!) 0098 } 0099 0100 public init
AudioPlayer.swift:84 sound?.pan = pan(contentsOfPath path: String) throws { 0101 let fileURL = NSURL(fileURLWithPath: path) 0102 URL = fileURL 0103 name = fileURL.lastPathComponent 0104 sound = try? AVAudioPlayer(contentsOfURL: fileURL) 0105 super.init() 0106 0107 sound?.delegate = self 0108 } 0109 0110 deinit { 0111 timer?.invalidate() 0112 } 0113 0114 // MARK: Play / Stop 0115 0116 public func play() { 0117 if playing == false { 0118 sound?.play() 0119 } 0120 } 0121 0122 public func stop
AudioPlayer.swift:97 try self.init(contentsOfPath: path!)() { 0123 if playing { 0124 soundDidFinishPlayingSuccessfully(false) 0125 } 0126 } 0127 0128 // MARK: Fade 0129 0130 public func fadeTo
AudioPlayer.swift:160 stop()(volume: Float, duration: NSTimeInterval = 1.0) { 0131 startVolume = volume; 0132 fadeTime = duration; 0133 fadeStart = NSDate().timeIntervalSinceReferenceDate 0134 if timer == nil { 0135 self.timer = NSTimer.scheduledTimerWithTimeInterval(0.015, target: self, selector: "handleFadeTo", userInfo: nil, repeats: true) 0136 } 0137 } 0138 0139 public func fadeIn(duration: NSTimeInterval = 1.0) { 0140 volume = 0.0 0141 fadeTo(1.0, duration: duration) 0142 } 0143 0144 public func fadeOut(duration: NSTimeInterval = 1.0) { 0145 fadeTo(0.0, duration: duration) 0146 } 0147 0148 // MARK: Private 0149 0150 private func handleFadeTo() { 0151 let now = NSDate().timeIntervalSinceReferenceDate 0152 let delta: Float = (Float(now - fadeStart) / Float(fadeTime) * (targetVolume - startVolume)) 0153 sound?.volume = startVolume + delta 0154 if delta > 0.0 && sound?.volume >= targetVolume || 0155 delta < 0.0 && sound?.volume <= targetVolume { 0156 sound?.volume = targetVolume 0157 timer?.invalidate() 0158 timer = nil 0159 if sound?.volume == 0 { 0160 stop() 0161 } 0162 } 0163 } 0164 0165 // MARK: Private properties 0166 0167 private let sound
AudioPlayer.swift:141 fadeTo(1.0, duration: duration)AudioPlayer.swift:145 fadeTo(0.0, duration: duration): AVAudioPlayer? 0168 private var startVolume
AudioPlayer.swift:33 if let nonNilsound = sound {AudioPlayer.swift:43 if let nonNilsound = sound {AudioPlayer.swift:53 if let nonNilsound = sound {AudioPlayer.swift:59 sound?.currentTime = newValueAudioPlayer.swift:77 sound?.numberOfLoops = numberOfLoopsAudioPlayer.swift:84 sound?.pan = panAudioPlayer.swift:104 sound = try? AVAudioPlayer(contentsOfURL: fileURL)AudioPlayer.swift:107 sound?.delegate = selfAudioPlayer.swift:118 sound?.play()AudioPlayer.swift:153 sound?.volume = startVolume + deltaAudioPlayer.swift:154 if delta > 0.0 && sound?.volume >= targetVolume ||AudioPlayer.swift:155 delta < 0.0 && sound?.volume <= targetVolume {AudioPlayer.swift:156 sound?.volume = targetVolumeAudioPlayer.swift:159 if sound?.volume == 0 {AudioPlayer.swift:171 sound?.volume = targetVolumeAudioPlayer.swift:192 sound?.stop(): Float = 1.0 0169 private var targetVolume
AudioPlayer.swift:131 startVolume = volume;AudioPlayer.swift:152 let delta: Float = (Float(now - fadeStart) / Float(fadeTime) * (targetVolume - startVolume))AudioPlayer.swift:153 sound?.volume = startVolume + delta: Float = 1.0 { 0170 didSet { 0171 sound?.volume = targetVolume 0172 } 0173 } 0174 0175 private var fadeTime
AudioPlayer.swift:67 targetVolume = volumeAudioPlayer.swift:152 let delta: Float = (Float(now - fadeStart) / Float(fadeTime) * (targetVolume - startVolume))AudioPlayer.swift:154 if delta > 0.0 && sound?.volume >= targetVolume ||AudioPlayer.swift:155 delta < 0.0 && sound?.volume <= targetVolume {AudioPlayer.swift:156 sound?.volume = targetVolumeAudioPlayer.swift:171 sound?.volume = targetVolume: NSTimeInterval = 0.0 0176 private var fadeStart
AudioPlayer.swift:132 fadeTime = duration;AudioPlayer.swift:152 let delta: Float = (Float(now - fadeStart) / Float(fadeTime) * (targetVolume - startVolume)): NSTimeInterval = 0.0 0177 private var timer
AudioPlayer.swift:133 fadeStart = NSDate().timeIntervalSinceReferenceDateAudioPlayer.swift:152 let delta: Float = (Float(now - fadeStart) / Float(fadeTime) * (targetVolume - startVolume)): NSTimer? 0178 0179 } 0180 0181 extension AudioPlayer: AVAudioPlayerDelegate { 0182 0183 public func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) { 0184 soundDidFinishPlayingSuccessfully(flag) 0185 } 0186 0187 } 0188 0189 private extension AudioPlayer { 0190 0191 private func soundDidFinishPlayingSuccessfully
AudioPlayer.swift:111 timer?.invalidate()AudioPlayer.swift:134 if timer == nil {AudioPlayer.swift:135 self.timer = NSTimer.scheduledTimerWithTimeInterval(0.015, target: self, selector: "handleFadeTo", userInfo: nil, repeats: true)AudioPlayer.swift:157 timer?.invalidate()AudioPlayer.swift:158 timer = nilAudioPlayer.swift:193 timer?.invalidate()AudioPlayer.swift:194 timer = nil(didFinishSuccessfully: Bool) { 0192 sound?.stop() 0193 timer?.invalidate() 0194 timer = nil 0195 0196 if let nonNilCompletionHandler = completionHandler { 0197 nonNilCompletionHandler(didFinish: didFinishSuccessfully) 0198 } 0199 0200 NSNotificationCenter.defaultCenter().postNotificationName(AudioPlayer.SoundDidFinishPlayingNotification, object: self) 0201 } 0202 0203 } 0204
AudioPlayer.swift:124 soundDidFinishPlayingSuccessfully(false)AudioPlayer.swift:184 soundDidFinishPlayingSuccessfully(flag)