AKPlayer

public class AKPlayer : AKAbstractPlayer

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 you will need to provide your own completionHandler if needed.

  • 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
  • The underlying player node

    Declaration

    Swift

    @objc
    public var playerNode: AVAudioPlayerNode
  • The main output

    Declaration

    Swift

    @objc
    public var mixer: AVAudioMixerNode
  • 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. Requires iOS 11, macOS 10.13.

    Declaration

    Swift

    @objc
    public var completionHandler: AKCallback? { get set }
  • Completion handler to be called when Audio has looped. The handler won’t be called if stop() is called while playing.

    Declaration

    Swift

    @objc
    public var loopCompletionHandler: AKCallback?
  • Used with buffering players

    Declaration

    Swift

    @objc
    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, but… 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 }
  • Will return whether the engine is rendering offline or realtime Requires iOS 11, macOS 10.13 for offline rendering

    Declaration

    Swift

    public override var renderingMode: RenderingMode { get }
  • The internal audio file

    Declaration

    Swift

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

    Declaration

    Swift

    @objc
    public override var duration: Double { get }
  • Declaration

    Swift

    @objc
    public override var sampleRate: Double { get }
  • Volume 0.0 -> 1.0, default 1.0 This is different than gain

    Declaration

    Swift

    @objc
    public var volume: Double { get set }
  • pan

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

    Declaration

    Swift

    @objc
    public var pan: Double { get set }
  • Declaration

    Swift

    @objc
    public var rampType: AKSettings.RampType { get set }
  • Declaration

    Swift

    @objc
    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

    @objc
    public var currentFrame: AVAudioFramePosition { get }

    Return Value

    The current frame while playing

  • Declaration

    Swift

    @objc
    public var currentTime: Double { get }

    Return Value

    Current time of the player in seconds while playing.

  • Declaration

    Swift

    public var pauseTime: Double? { get set }
  • Returns the audioFile’s internal processingFormat

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

    @objc
    public var isNormalized: Bool { get set }
  • returns if the player is currently paused

    Declaration

    Swift

    @objc
    public internal(set) var isPaused: Bool
  • Reversing the audio will set the player to buffering

    Declaration

    Swift

    @objc
    public var isReversed: Bool { get set }
  • Declaration

    Swift

    @objc
    public var isBufferFaded: Bool { get }
  • Declaration

    Swift

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

    Declaration

    Swift

    @objc
    public convenience init?(url: URL)
  • Create a player from an AVAudioFile (or AKAudioFile). If a file has previously been opened for writing, you can reset it to readOnly with the reopenFile flag. This is necessary in cases where AKMicrophone may of had access to the file.

    Declaration

    Swift

    @objc
    public convenience init(audioFile: AVAudioFile, reopenFile: Bool = true)
  • Declaration

    Swift

    open override func initialize(restartIfPlaying: Bool = true)
  • 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 entire file right now

    Declaration

    Swift

    @objc
    public override func play()
  • Play using full options. Last in the convenience play chain, all play() commands will end up here

    Declaration

    Swift

    public func play(from startingTime: Double, to endingTime: Double, at audioTime: AVAudioTime?, hostTime: UInt64?)
  • Stop playback and cancel any pending scheduled playback or completion events

    Declaration

    Swift

    @objc
    public override func stop()
  • Dispose the audio file, buffer and nodes and release resources. Only call when you are totally done with this class.

    Declaration

    Swift

    @objc
    public override func detach()
  • 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. If the audioTime is in the past it will be played now.

    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? = nil)
  • 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()
  • Provides a convenience method for a quick fade out for when a user presses stop.

    Declaration

    Swift

    public func fadeOutAndStop(time: TimeInterval)
  • Declaration

    Swift

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

    Swift

    @objc
    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

    @objc
    open func prepare()