Latest 1.0.2
License MIT
Platforms ios 10.0
Authors ,


Swift based audio player that is able to both stream remote audio and play locally saved audio, while performing audio manipulations in real-time. Underlying using AVAudioEngine, and you can change the rate of audio (up to 32x), change pitch, and other audio enhancements.

This player was originally developed to be used in a podcast player. We had originally used AVPlayer for playing audio but we wanted to manipulate audio that was being streamed. We set up AVAudioEngine at first just to play a file saved on the phone and it worked great, but AVAudioEngine on its own doesn’t support streaming audio as easily as AVPlayer.

Thus, using AudioToolbox, we are able to stream audio and convert the downloaded data into usable data for the AVAudioEngine to play. For an overview of our solution check out our blog post.


SwiftAudioPlayer is only available for iOS 10.0 and higher.

Getting Started

Example Project

To run the example project, clone the repo, and run pod install from the Example directory first.


SwiftAudioPlayer is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'SwiftAudioPlayer'


To play remote audio:

let url = URL(string: "")!
SAPlayer.shared.initializeAudio(withRemoteUrl: url)

To set the display information for the lockscreen:

let info = SALockScreenInfo(title: "Random audio", artist: "Foo", artwork: UIImage(), releaseDate: 123456789)
SAPlayer.shared.mediaInfo = info

To receive streaming progress:

@IBOutlet weak var bufferProgress: UIProgressView!

override func viewDidLoad() {

    _ = SAPlayer.Updates.StreamingBuffer.subscribe{ [weak self] (url, buffer) in
        guard let self = self else { return }
        guard url == self.selectedAudioUrl else { return }

        let progress = Float((buffer.totalDurationBuffered + buffer.startingBufferTimePositon) / self.duration)

        self.bufferProgress.progress = progress

        self.isPlayable = buffer.isReadyForPlaying

Look at the Updates section to see usage details and other updates to follow.

Important: For app in background downloading please refer to note.

For more details and specifics look at the API documentation below.



Submit any issues or requests on the Github repo.

Any questions?

Feel free to reach out to either of us:

tanhakabir, [email protected]
JonMercer, [email protected]


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

API in detail


Use functionaity from Downloader to save audio files from remote locations for future offline playback.

Audio files are saved under custom naming scheme on device and are recoverable with original remote URL for file.

Important step for background downloads

To ensure that your app will keep downloading audio in the background be sure to add the following to AppDelegate.swift:

func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {


Downloads will be held on pause when active stream is started, and will resume downloads when streaming is done.

Use the following to start downloading audio in the background:

func downloadAudio(withRemoteUrl url: URL, completion: @escaping (_ savedUrl: URL) -> ())

It will call the completion handler you pass after successful download with the location of the downloaded file on the device.

And use the following to stop any active or prevent future downloads of the corresponding remote URL:

func cancelDownload(withRemoteUrl url: URL)

Manage Downloaded

Use the following to manage downloaded audio files.

Checks if downloaded already:

func isDownloaded(withRemoteUrl url: URL) -> Bool

Get URL of audio file saved on device corresponding to remote location:

func getSavedUrl(forRemoteUrl url: URL) -> URL?

Delete downloaded audio if it exists:

func deleteDownloaded(withSavedUrl url: URL)


Receive updates for changing values from the player, such as the duration, elapsed time of playing audio, download progress, and etc.

All subscription functions for updates take the form of:

func subscribe(_ closure: @escaping (_ url: URL, _ payload:  <Payload>) -> ()) -> UInt
  • closure: The closure that will receive the updates. It’s recommended to have a weak reference to a class that uses these functions.
  • url: The corresponding remote URL for the update. In the case there might be multiple files observed, such as downloading many files at once or switching over from playing one audio to another and the updates corresponding to the previous aren’t silenced on switch-over.
  • payload: The updated value.
  • Returns: the id for the subscription in the case you would like to unsubscribe to updates for the closure.

Similarily unsubscribe takes the form of:

func unsubscribe(_ id: UInt)
  • id: The closure with this id will stop receiving updates.


Payload = Double

Changes in the timestamp/elapsed time of the current initialized audio. Aka, where the scrubber’s pointer of the audio should be at.

Subscribe to this to update views on changes in position of which part of audio is being played.


Payload = Double

Changes in the duration of the current initialized audio. Especially helpful for audio that is being streamed and can change with more data.


Payload = Bool

Changes in the playing/paused status of the player.


Payload = SAAudioAvailabilityRange

Changes in the progress of downloading audio for streaming. Information about range of audio available and if the audio is playable. Look at SAAudioAvailabilityRange for more information.

For progress of downloading audio that saves to the phone for playback later, look at AudioDownloading instead.


Payload = Double

Changes in the progress of downloading audio in the background. This does not correspond to progress in streaming downloads, look at StreamingBuffer for streaming progress.

Latest podspec

    "name": "SwiftAudioPlayer",
    "version": "1.0.2",
    "summary": "SwiftAudioPlayer is a Swift based audio player that can handle streaming from a remote location and audio manipulation.",
    "description": "SwiftAudioPlayer is a Swift based audio player that can handle streaming from a remote location and audio manipulation. It can perform actions like playing audio up to 32x playback rate on streamed audio.",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "tanhakabir": "[email protected]",
        "JonMercer": "[email protected]"
    "source": {
        "git": "",
        "tag": "1.0.2"
    "platforms": {
        "ios": "10.0"
    "source_files": "Source/**/*",
    "swift_version": "4.0"

Pin It on Pinterest

Share This