Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiovinotti committed Mar 25, 2022
1 parent a777ee6 commit 6a1c03b
Showing 1 changed file with 90 additions and 1 deletion.
91 changes: 90 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BasicAudioPlayer

BasicAudioPlayer is an easy to customize AVAudioEngine-based player. It implements all the basic functions an audio player should have, so you can focus on adding AVAudioEngineNodes and custom features.
BasicAudioPlayer is a library that makes it easier to create AVAudioEngine-based audio players.

## Installation

Expand All @@ -11,6 +11,95 @@ BasicAudioPlayer is a Swift package. To add it to your Xcode project:
<li>Choose a dependency rule. "Up to Next Major Version" is the suggested setting.</li>
</ol>

## 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.

```Swift
// 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.

```Swift
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.

```Swift
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 <code>status</code> 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.

```Swift
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 (<code>AVAudioUnit</code>) to a BAPlayer to manipulate its audio output. They will be connected in the order they were added.

```Swift
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.

0 comments on commit 6a1c03b

Please sign in to comment.