AudioPlayer Documentation

repository·master·Indexed 20 days ago

https://github.com/delannoyk/audioplayer

A Swift wrapper around AVPlayer providing advanced features including quality control based on buffering, automatic retries, connection handling, audio item enqueuing, and playback modes such as Repeat and Shuffle.

Tokens
790
Snippets
3
Records
4
Agent score
23%

What's inside AudioPlayer

  1. Enable Control Center and Lockscreen support

    master

    To allow users to control playback from the Lockscreen or Control Center, you must enable remote control events in your application delegate and forward events to the AudioPlayer instance.

    1. In application(_:didFinishLaunchingWithOptions:), call application.beginReceivingRemoteControlEvents().
    2. In your UIResponder (or AppDelegate), override remoteControlReceivedWithEvent(_:) and pass the event to your AudioPlayer instance using remoteControlReceivedWithEvent(event:).
    // 1. In AppDelegate
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        application.beginReceivingRemoteControlEvents()
        return true
    }
    
    // 2. In your UIResponder or AppDelegate
    override func remoteControlReceivedWithEvent(event: UIEvent?) {
        if let event = event {
            yourPlayer.remoteControlReceivedWithEvent(event)
        }
    }
  2. Basic usage of AudioPlayer

    master

    To play audio, initialize an AudioPlayer instance, assign a delegate to handle events, create an AudioItem with a sound URL, and call playItem(_:).

    Note: AudioItem can be initialized with a mediumQualitySoundURL.

    let delegate: AudioPlayerDelegate = ...
    
    let player = AudioPlayer()
    player.delegate = delegate
    let item = AudioItem(mediumQualitySoundURL: track.streamURL)
    player.playItem(item)
  3. Implement the AudioPlayerDelegate

    master

    Use the AudioPlayerDelegate protocol to respond to player state changes, track duration, progression updates, and queue events.

    State Changes

    Triggered when the player's state changes: func audioPlayer(audioPlayer: AudioPlayer, didChangeStateFrom from: AudioPlayerState, toState to: AudioPlayerState)

    Duration & Progression

    Triggered when the duration of a new item is discovered: func audioPlayer(audioPlayer: AudioPlayer, didFindDuration duration: NSTimeInterval, forItem item: AudioItem)

    Triggered regularly to provide playback progress. The percentageRead parameter is a Float between 0 and 100, suitable for updating UI elements like UISlider: func audioPlayer(audioPlayer: AudioPlayer, didUpdateProgressionToTime time: NSTimeInterval, percentageRead: Float)

    Queue Events

    Triggered before an item in the queue starts playing: func audioPlayer(audioPlayer: AudioPlayer, willStartPlayingItem item: AudioItem)