AKAudioPlayer

open class AKAudioPlayer: AKNode, AKToggleable

Not so simple audio playback class

  • Buffer to be palyed

    Declaration

    Swift

    open dynamic var audioFileBuffer: AVAudioPCMBuffer?
  • Will be triggered when AKAudioPlayer has finished to play. (will not as long as loop is on)

    Declaration

    Swift

    open dynamic var completionHandler: AKCallback?
  • Boolean indicating whether or not to loop the playback (Default false)

    Declaration

    Swift

    open dynamic var looping: Bool
  • Boolean indicating to play the buffer in reverse

    Declaration

    Swift

    open dynamic var reversed: Bool = false
  • Fade in duration

    Declaration

    Swift

    open dynamic var fadeInTime: Double = 0
  • Fade out duration

    Declaration

    Swift

    open dynamic var fadeOutTime: Double = 0
  • The current played AKAudioFile

    Declaration

    Swift

    open dynamic var audioFile: AKAudioFile
  • Path to the currently loaded AKAudioFile

    Declaration

    Swift

    open dynamic var path: String
  • Total duration of one loop through of the file

    Declaration

    Swift

    open dynamic var duration: Double
  • Output Volume (Default 1)

    Declaration

    Swift

    open dynamic var volume: Double = 1.0
  • Whether or not the audio player is currently started

    Declaration

    Swift

    open dynamic var isStarted: Bool
  • Current playback time (in seconds)

    Declaration

    Swift

    open dynamic var currentTime: Double
  • Time within the audio file at the current time

    Declaration

    Swift

    open dynamic var playhead: Double
  • pan

    Pan (Default Center = 0)

    Declaration

    Swift

    open dynamic var pan: Double = 0.0
  • sets the start time, If it is playing, player will restart playing from the start time each time end time is set

    Declaration

    Swift

    open dynamic var startTime: Double
  • sets the end time, If it is playing, player will restart playing from the start time each time end time is set

    Declaration

    Swift

    open dynamic var endTime: Double
  • Sets the time in the future when playback will commence. Recommend using play(from:to:avTime) instead. this will be deprecated

    Declaration

    Swift

    open dynamic var scheduledTime: Double = 0
  • Sheduled time

    Declaration

    Swift

    open dynamic var scheduledAVTime: AVAudioTime?
  • Initialize the audio player

    Notice that completionCallBack will be triggered from a background thread. Any UI update should be made using:

    Dispatch.main.async {
       // UI updates...
    }
    

  • Parameters:

    • file: the AKAudioFile to play
    • looping: will loop play if set to true, or stop when play ends, so it can trig the completionHandler callback. Default is false (non looping)
    • completionHandler: AKCallback that will be triggered when the player end playing (useful for refreshing UI so we’re not playing anymore, we stopped playing…)
  • Returns

    an AKAudioPlayer if init succeeds, or nil if init fails. If fails, errors may be caught.

    Declaration

    Swift

    public init(file: AKAudioFile,
                    looping: Bool = false,
                    completionHandler: AKCallback? = nil) throws

    Parameters

    file

    the AKAudioFile to play

    looping

    will loop play if set to true, or stop when play ends, so it can trig the completionHandler callback. Default is false (non looping)

    completionHandler

    AKCallback that will be triggered when the player end playing (useful for refreshing UI so we’re not playing anymore, we stopped playing…)

    Return Value

    an AKAudioPlayer if init succeeds, or nil if init fails. If fails, errors may be caught.

  • Start playback

    Declaration

    Swift

    open func start()
  • Stop playback

    Declaration

    Swift

    open func stop()
  • Pause playback

    Declaration

    Swift

    open func pause()
  • Restart playback from current position

    Declaration

    Swift

    open func resume()
  • resets in and out times for playing

    Declaration

    Swift

    open func reloadFile() throws
  • Replace player’s file with a new AKAudioFile file

    Declaration

    Swift

    open func replace(file: AKAudioFile) throws
  • Default play that will use the previously set startTime and endTime properties or the full file if both are 0

    Declaration

    Swift

    open func play()
  • Play from startTime to endTime

    Declaration

    Swift

    open func play(from startTime: Double, to endTime: Double)
  • Play the file back from a certain time, to an end time (if set). You can optionally set a scheduled time to play (in seconds).

    • Parameters:
      • from startTime: Time into the file at which to start playing back
      • to endTime: Time into the file at which to playing back will stop / Loop
      • when scheduledTime: use this when scheduled playback doesn’t need to be in sync with other players otherwise use the avTime signature.

    Declaration

    Swift

    open func play(from startTime: Double, to endTime: Double, when scheduledTime: Double)

    Parameters

    from startTime

    Time into the file at which to start playing back

    to endTime

    Time into the file at which to playing back will stop / Loop

    when scheduledTime

    use this when scheduled playback doesn’t need to be in sync with other players otherwise use the avTime signature.

  • Play the file back from a certain time, to an end time (if set). You can optionally set a scheduled time to play (in seconds).

    • Parameters:
      • from startTime: Time into the file at which to start playing back
      • to endTime: Time into the file at which to playing back will stop / Loop
      • avTime: an AVAudioTime object specifying when to schedule the playback. You can create this using the helper function AKAudioPlayer.secondToAVAudioTime(hostTime:time). hostTime is a call to mach_absolute_time(). When you have a group of players which you want to sync together it’s important that this value be the same for all of them as a reference point.

    Declaration

    Swift

    open func play(from time: Double, to endTime: Double, avTime: AVAudioTime? )

    Parameters

    from startTime

    Time into the file at which to start playing back

    to endTime

    Time into the file at which to playing back will stop / Loop

    avTime

    an AVAudioTime object specifying when to schedule the playback. You can create this using the helper function AKAudioPlayer.secondToAVAudioTime(hostTime:time). hostTime is a call to mach_absolute_time(). When you have a group of players which you want to sync together it’s important that this value be the same for all of them as a reference point.

  • Stop playback after next loop completes

    Declaration

    Swift

    open func stopAtNextLoopEnd()
  • Declaration

    Swift

    override open func disconnect()