AKPlayer

public class AKPlayer : AKNode

AKPlayer is meant to be a simple yet powerful audio player that just works. It supports scheduling of sounds, looping, fading, time-stretching, pitch-shifting and reversing. Players can be locked to a common clock as well as video by using hostTime in the various play functions. By default the player will buffer audio if needed, otherwise stream from disk. Reversing the audio will cause the file to buffer. For seamless looping use buffered playback.

There are a few options for syncing to external objects.

A locked video function would resemble:

func videoPlay(at time: TimeInterval = 0, hostTime: UInt64 = 0 ) {
let cmHostTime = CMClockMakeHostTimeFromSystemUnits(hostTime)
let cmVTime = CMTimeMakeWithSeconds(time, 1000000)
let futureTime = CMTimeAdd(cmHostTime, cmVTime)
videoPlayer.setRate(1, time: kCMTimeInvalid, atHostTime: futureTime)
}

Basic usage looks like:

guard let player = AKPlayer(url: url) else { return }
player.completionHandler = { AKLog("Done") }

// Loop Options
player.loop.start = 1
player.loop.end = 3
player.isLooping = true
player.buffer = true // if seamless is desired

player.play()

Please note that pre macOS 10.13 / iOS 11 the completionHandler isn’t sample accurate. It’s pretty close though.

  • How the player should handle audio. If buffering, it will load the audio data into an internal buffer and play from RAM. If not, it will play the file from disk. Dynamic buffering will only load the audio if it needs to for processing reasons such as Perfect Looping or Reversing

    See more

    Declaration

    Swift

    public enum BufferingType
  • Declaration

    Swift

    public struct Loop
  • Declaration

    Swift

    public struct Fade
  • Holds characteristics about the fade options.

    Declaration

    Swift

    public var fade: AKPlayer.Fade
  • The underlying player node

    Declaration

    Swift

    public let playerNode: AVAudioPlayerNode
  • The main output

    Declaration

    Swift

    public let mixer: AVAudioMixerNode
  • The underlying gain booster which controls fades as well. Created on demand.

    Declaration

    Swift

    public var faderNode: AKBooster?
  • Declaration

    Swift

    public var rate: Double
  • Completion handler to be called when Audio is done playing. The handler won’t be called if stop() is called while playing or when looping from a buffer.

    Declaration

    Swift

    public var completionHandler: AKCallback?
  • Used with buffering players

    Declaration

    Swift

    public var buffer: AVAudioPCMBuffer?
  • Sets if the player should buffer dynamically (as needed) or always. Not buffering means streaming from disk (best for long files), buffering is playing from RAM (best for shorter sounds you might want to loop). For seamless looping you should load the sound into RAM. While this creates a perfect loop, the downside is that you can’t easily scrub through the audio. If you need to be able to be able to scan around the file, keep this .dynamic and stream from disk.

    Declaration

    Swift

    public var buffering: AKPlayer.BufferingType { get set }
  • The internal audio file

    Declaration

    Swift

    public private(set) var audioFile: AVAudioFile?
  • The duration of the loaded audio file

    Declaration

    Swift

    public var duration: Double { get }
  • Looping Parameters

    Declaration

    Swift

    public var loop: AKPlayer.Loop
  • Volume 0.0 -> 1.0, default 1.0

    Declaration

    Swift

    public var volume: Double { get set }
  • Amplification Factor, in the range of 0.0002 to ~

    Declaration

    Swift

    public var gain: Double { get set }
  • pan

    Left/Right balance -1.0 -> 1.0, default 0.0

    Declaration

    Swift

    public var pan: Double { get set }
  • Declaration

    Swift

    public var rampType: AKSettings.RampType { get set }
  • Get or set the start time of the player.

    Declaration

    Swift

    public var startTime: Double { get set }
  • Get or set the end time of the player.

    Declaration

    Swift

    public var endTime: Double { get set }
  • Declaration

    Swift

    public internal(set) var frameCount: AVAudioFrameCount

    Return Value

    The total frame count that is being playing. Differs from the audioFile.length as this will be updated with the edited amount of frames based on startTime and endTime

  • Declaration

    Swift

    public var currentFrame: AVAudioFramePosition { get }

    Return Value

    The current frame while playing

  • Declaration

    Swift

    public var currentTime: Double { get }

    Return Value

    Current time of the player in seconds while playing.

  • Declaration

    Swift

    public var pauseTime: Double?
  • Declaration

    Swift

    public var processingFormat: AVAudioFormat? { get }
  • true if the player is buffering audio rather than playing from disk

    Declaration

    Swift

    public var isBuffered: Bool { get }
  • Will automatically normalize on buffer updates if enabled

    Declaration

    Swift

    public var isNormalized: Bool { get set }
  • Declaration

    Swift

    public var isLooping: Bool
  • Declaration

    Swift

    public var isPaused: Bool { get }
  • Reversing the audio will set the player to buffering

    Declaration

    Swift

    public var isReversed: Bool { get set }
  • Declaration

    Swift

    public var isPlaying: Bool { get }
  • true if any fades have been set

    Declaration

    Swift

    public var isFaded: Bool { get }
  • Declaration

    Swift

    public override init()
  • Create a player from a URL

    Declaration

    Swift

    public convenience init?(url: URL)
  • Create a player from an AVAudioFile (or AKAudioFile)

    Declaration

    Swift

    public convenience init(audioFile: AVAudioFile)
  • Replace the contents of the player with this url

    Declaration

    Swift

    @objc
    public func load(url: URL) throws
  • Declaration

    Swift

    @objc
    public func load(audioFile: AVAudioFile)
  • Mostly applicable to buffered players, this loads the buffer and gets it ready to play. Otherwise it just sets the startTime and endTime

    Declaration

    Swift

    @objc
    public func preroll(from startingTime: Double = 0, to endingTime: Double = 0)
  • Play using full options. Last in the convenience play chain, all play() commands will end up here Placed in main class to be overriden in subclasses if needed.

    Declaration

    Swift

    public func play(from startingTime: Double, to endingTime: Double, at audioTime: AVAudioTime?, hostTime: UInt64?)
  • Disconnect the node and release resources

    Declaration

    Swift

    public override func detach()
  • Play entire file right now

    Declaration

    Swift

    @objc
    public func play()
  • Play segments of a file

    Declaration

    Swift

    @objc
    public func play(from startingTime: Double, to endingTime: Double = 0)
  • Play file using previously set startTime and endTime at some point in the future

    Declaration

    Swift

    @objc
    public func play(at audioTime: AVAudioTime?)
  • Play file using previously set startTime and endTime at some point in the future with a hostTime reference

    Declaration

    Swift

    public func play(at audioTime: AVAudioTime?, hostTime: UInt64?)
  • Play file using previously set startTime and endTime at some point in the future specified in seconds with a hostTime reference

    Declaration

    Swift

    public func play(when scheduledTime: Double, hostTime: UInt64?)
  • Declaration

    Swift

    public func play(from startingTime: Double,
                     to endingTime: Double,
                     when scheduledTime: Double,
                     hostTime: UInt64? = nil)
  • Declaration

    Swift

    @objc
    public func pause()
  • Declaration

    Swift

    @objc
    public func resume()
  • Stop playback and cancel any pending scheduled playback or completion events

    Declaration

    Swift

    @objc
    public func stop()
  • Declaration

    Swift

    @objc
    public func start(at audioTime: AVAudioTime?)
  • Declaration

    Swift

    public var isStarted: Bool { get }
  • Declaration

    Swift

    @objc
    public func setPosition(_ position: Double)
  • Declaration

    Swift

    @objc
    public func position(at audioTime: AVAudioTime?) -> Double
  • Declaration

    Swift

    @objc
    public func audioTime(at position: Double) -> AVAudioTime?
  • Declaration

    Swift

    open func prepare()