Swiftpack.co - fabiovinotti/BasicAudioPlayer as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by fabiovinotti.
fabiovinotti/BasicAudioPlayer v3.1.3
A Swift library that makes it easier to create AVAudioEngine-based audio players
⭐️ 9
🕓 27 weeks ago
iOS macOS
.package(url: "https://github.com/fabiovinotti/BasicAudioPlayer.git", from: "v3.1.3")

BasicAudioPlayer

BasicAudioPlayer is a Swift library that makes it easier to create AVAudioEngine-based audio players.

Installation

BasicAudioPlayer is a Swift package. To add it to your Xcode project:

  1. Go to File -> Add Packages...
  2. Enter https://github.com/fabiovinotti/BasicAudioPlayer in the search bar.
  3. Choose a dependency rule. "Up to Next Major Version" is the suggested setting.

Getting Started

BAPlayer

BAPlayer is an object that plays audio from a file. It features all the basic playback control methods an audio player should have, and lets you modulate its audio output by adding audio units to its effects chain. BAPlayer manages all of its audio nodes behind the hood, so you don't have to deal with that.

Creating a BAPlayer

Creating a new BAPlayer is straightforward. If you already know which audio file the player will play, you can load it at creation time using the appropriate initializer.

// Create a player without loading an audio file
let p = BAPlayer()

// Create a player and load an audio file at a certain URL
let audioFileURL = URL(fileURLWithPath: "/Some/Audio/file.m4a")
let p1 = try BAPlayer(url: audioFileURL)

// Create a player and load an audio file
let audioFile = try AVAudioFile(forReading: audioFileURL)
let p2 = BAPlayer(file: audioFile)

Loading an Audio File

You can load an audio file after creating the player. A BAPlayer instance can handle the playback of only one audio file at a time. Therefore, if another file has already been loaded, it will be replaced with the new one.

let player = BAPlayer()

// Load an audio file at a certain URL
let audioFileURL = URL(fileURLWithPath: "/Path/To/Audio/file.m4a")
try player.load(url: audioFileURL)

// Load an audio file
let audioFile = try AVAudioFile(forReading: audioFileURL)
player.load(file: audioFile)

// Get the loaded audio file, if any
let loadedFile: AVAudioFile? = player.file

Controlling Playback

BAPlayer makes it easy to control audio playback. Here are the basic methods available.

let player = BAPlayer(url: URL(fileURLWithPath: "/Some/Audio/file.m4a"))

// Play audio
player.play()

// Pause audio
player.pause()

// Stop the playback and reset the elapsed time back to 0
player.stop()

Handling Status Changes

BAPlayer features a property named status that can be used to check what the player is doing. You can also provide a closure to execute when the player's status changes.

let player = BAPlayer()

// Get the status of the player
let status = player.status

// Add a closure to run when the player's status changes.
player.onStatusChange { newStatus in
    if newStatus == .playing {
        print("The player is now playing")
    }
}

Adding Audio Units

You can add audio units (AVAudioUnit) to a BAPlayer to manipulate its audio output. They will be connected in the order they were added.

let player = BAPlayer()

// Add a time pitch unit to the player's audio unit chain.
let timePitchUnit = AVAudioUnitTimePitch()
player.addAudioUnit(timePitchUnit)

License

BasicAudioPlayer is available under the MIT license. See the LICENSE file for more info.

GitHub

link
Stars: 9
Last commit: 27 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

3.1.3
27 weeks ago
  • The action performed when the player status changes can be set back to nil
  • Fix: Stopping the player does not remove scheduled events

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics