SwiftAudioKit is a robust, feature-rich wrapper around AVPlayer
, designed to simplify audio playback on iOS, tvOS, macOS and watchOS.
- Quality Control: Automatically adapts to interruptions (buffering) and adjusts playback based on delay.
- Retry Mechanism: Automatically retries playback if the player encounters an error.
- Connection Handling: Smart handling of connection interruptions and seamless recovery.
- Audio Item Enqueuing: Easily enqueue audio items for uninterrupted playback.
- Playback Modes: Supports repeat, repeat all, and shuffle modes.
- MPNowPlayingInfoCenter Integration: Full support for Control Center and lock screen media controls.
- High Customizability: Flexible and customizable to fit your specific needs.
SwiftAudioKit is vailable exclusively through Swift Package Manager (SPM). To integrate it into your project, follow these steps:
- Open your project in Xcode.
- Go to File > Add Packages...
- Enter the following URL in the search field:
https://github.com/diokaratzas/swift-audio-kit
- Choose the version you want to install and click Add Package.
To use the SwiftAudioKit with your package, first add it as a dependency:
let package = Package(
// name, platforms, products, etc.
dependencies: [
// other dependencies
.package(url: "https://github.com/diokaratzas/swift-audio-kit", from: "1.0.0"),
],
targets: [
// targets
]
)
Please see the package documentation for more detailed usage instructions.
Here’s a quick example of how to get started with SwiftAudioKit:
let delegate: AudioPlayerDelegate = ...
let player = AudioPlayer()
player.delegate = delegate
let item = AudioItem(mediumQualitySoundURL: track.streamURL)
player.playItem(item)
SwiftAudioKit uses delegation to notify about status changes and other events.
This method is called when the player’s state changes:
func audioPlayer(_ audioPlayer: AudioPlayer, didChangeStateFrom from: AudioPlayerState, toState to: AudioPlayerState)
When the duration of the current item is found:
func audioPlayer(_ audioPlayer: AudioPlayer, didFindDuration duration: TimeInterval, forItem item: AudioItem)
This method is regularly called to update playback progression:
func audioPlayer(_ audioPlayer: AudioPlayer, didUpdateProgressionToTime time: TimeInterval, percentageRead: Float)
percentageRead
is a Float
value between 0 and 100, perfect for updating a UISlider
.
This method is called when a new audio item starts playing:
func audioPlayer(_ audioPlayer: AudioPlayer, willStartPlayingItem item: AudioItem)
To handle media controls via the Control Center or lock screen:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
application.beginReceivingRemoteControlEvents()
return true
}
// In your UIResponder (or AppDelegate):
override func remoteControlReceived(with event: UIEvent?) {
if let event = event {
yourPlayer.remoteControlReceived(with: event)
}
}
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new feature branch:
git checkout -b my-new-feature
. - Commit your changes:
git commit -am 'Add some feature'
. - Push to the branch:
git push origin my-new-feature
. - Submit a pull request.
- Increase unit test coverage.
- Refactor the current state handling system.
This project is licensed under the MIT License. See the LICENSE file for more information.
This project is based on the original AudioPlayer by Kevin Delannoy. Special thanks to Kevin for his foundational work, which has been instrumental in the development of SwiftAudioKit.