AudioPlayer is syntax and feature sugar over AVPlayer. It plays your audio files (local & remote).

Overview

AudioPlayer

Build Status Carthage compatible CocoaPods Compatible Platform iOS | tvOS Contact

AudioPlayer is a wrapper around AVPlayer. It also offers cool features such as:

  • Quality control based on number of interruption (buffering) and time delay
  • Retry if player fails
  • Connection handling
  • Audio item enqueuing
  • Player mode (Repeat, Repeat all, Shuffle)
  • MPNowPlayingInfoCenter
  • A high level of customization

Installation

  • CocoaPods: pod 'KDEAudioPlayer'
  • Carthage: github "delannoyk/AudioPlayer"

Usage

Basics

let delegate: AudioPlayerDelegate = ...

let player = AudioPlayer()
player.delegate = delegate
let item = AudioItem(mediumQualitySoundURL: track.streamURL)
player.playItem(item)

Delegate

In order to alert about status change or other events, AudioPlayer uses delegation.

State

When AudioPlayer’s state changes, the method

func audioPlayer(audioPlayer: AudioPlayer, didChangeStateFrom from: AudioPlayerState, toState to: AudioPlayerState)

is called.

Duration & progression

When AudioPlayer’s current item found duration of its content

func audioPlayer(audioPlayer: AudioPlayer, didFindDuration duration: NSTimeInterval, forItem item: AudioItem)

is called.

This method is called regularly to notify progression update.

func audioPlayer(audioPlayer: AudioPlayer, didUpdateProgressionToTime time: NSTimeInterval, percentageRead: Float)

percentageRead is a Float value between 0 & 100 so that you can easily update an UISlider for example.

Queue

func audioPlayer(audioPlayer: AudioPlayer, willStartPlayingItem item: AudioItem)

Control Center / Lockscreen

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    application.beginReceivingRemoteControlEvents()
}

//Then in your UIResponder (or your AppDelegate if you will)
override func remoteControlReceivedWithEvent(event: UIEvent?) {
    if let event = event {
        yourPlayer.remoteControlReceivedWithEvent(event)
    }
}

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Todo list

  • Add a fully working example project
  • Integrate with SPM
  • Add more unit tests
  • Add Objective-C support if possible (#67)
  • Refactor current state handling

License

The MIT License (MIT)

Copyright (c) 2015 Kevin Delannoy

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Comments
  • resume() does not work if playback was paused for some time in lockscreen

    resume() does not work if playback was paused for some time in lockscreen

    I have discovered the following problem:

    • call player.playItem() wait until it is playing
    Stopped -> Buffering
    Buffering -> Playing
    
    • lock phone
    • pause stream Playing -> Paused
    • wait some time
    • try to play again
    Paused -> Playing
    Playing -> Buffering
    

    It will stay in buffering, but does not play audio.

    I think this is a problem with AVPlayer, because resume() looks like this:

        public func resume() {
            player?.play()
            state = .Playing
        }
    

    player.play() is not enough to recover the stream because I think AVPlayer cuts the connection very fast when the phone is locked. This behaviour does not happen when the phone is not locked.

    I realised calling player.playItem() instead of player.resume() does actually work from lockscreen.

    I do not know what's the best way to address this issue. The player seems to "realize" that it has to start buffering as it moves into .Buffering immediately. However it does not handle the case that AVPlayer does not actually start playing.

    bug 
    opened by funkenstrahlen 40
  • tvOS - AudioPlayer's state doesn't change after loosing connection.

    tvOS - AudioPlayer's state doesn't change after loosing connection.

    This issue has been observed in a tvOS app, I don't know if it is the case in iOS too.

    While playing an audio item you disable the WiFi to test a reconnect scenario, the player's state doesn't change (is still Playing). Therefore, when Reachabilty detects that there is Internet connection again and performs the retrying, the method returns at the beginning, assuming that the audio is still being played.

    bug 
    opened by dani-mp 15
  • force exact seeking when zero before/after seek tolerance is specified

    force exact seeking when zero before/after seek tolerance is specified

    We were having some issues with the code not seeking exactly when we needed exact seeking (toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero) because the player?.seekToTime(_:toleranceBefore:toleranceAfter:) was hidden away in a few conditional calls and never reached when we needed exact seeking. This appears to fix it while preserving your existing performance enhancements for when exact seeking is not crucial.

    opened by dennislysenko 14
  • Sometimes playback is automatically paused

    Sometimes playback is automatically paused

    Playback is getting paused automatically without any interruptions or any actions. The network connectivity is good and the playback has to be manually resumed by hitting play again.

    Observed this happening with recent updates.

    opened by han9over 13
  • Audio ID3 information not being pulled

    Audio ID3 information not being pulled

    I'm not sure if this is issue with the file I have, but I tried to host a MP3 file remotely with all the ID3 information on it. I'm also not sure if I need to do something for the player to pull that information, but I don't see that being pulled and the observer getting fired.

    Can you please direct me in right path?

    Thanks in advance.

    opened by han9over 13
  • add range check for seeking, add seekToLive and seekToStart

    add range check for seeking, add seekToLive and seekToStart

    Currently seeking on a livestream crashes playback (nothing to hear anymore, no real code crash) if you seek to a time out of the seekable range. Therefore I added a range check to the seek method call.

    I also added seekToLive() and seekToStart() as helpers. They are used by the default seek method if the time to seek to is out of range.

    Currently seekToLive() seeks to the latest seekable posistion in the data stream minus 5 seconds of buffer time. The 5 seconds are quite randomly picked and should be changed to a the value of the buffer configuration which might be added in a future release.

    opened by funkenstrahlen 13
  • Suggestion: queue items should be public read

    Suggestion: queue items should be public read

    I think it is not easy to track audioItem index outside of the AudioPlayer

    because you have public function to use the index for removing audioItem, so it is necessary to know the index of the audioItem.

        removeItemAtIndex(index: Int)
    

    so I think the queuedItems should be at least public read from outside. And it is good for developer to show users what items are in the queue.

    Thanks!

    opened by frogcjn 11
  • Bugfix: Interruption ended

    Bugfix: Interruption ended

    This fixes #66

    • Receiving audio interruptions from un-resumable sources like Spotify and YouTube would leave the player in a very bad state with the pausedForInterruption flag set indefinitely.
    • pausedForInterruption is now reset on resume, currentItem set, and on any interruptionEnded callback, whether it has the shouldResume option or not.

    @delannoyk this commit I need feedback on, am I correct that the check on pausedForInterruption flag is unnecessary in shouldResumePlaying?

    opened by ddfreiling 9
  • Seek callback

    Seek callback

    Hi! Playing with a UISlider and the seek to time operation, I thought that having a callback when it has ended was a good idea, and as Apple already provides this functionality in AVPlayer, adding it to AudioPlayer has been easy.

    opened by dani-mp 8
  • Player takes a while to start

    Player takes a while to start

    I'm trying to stream audio from a server and your player has been a great blessing in that direction. It used to work great before I tried to implement a singleton. Now, when the singleton works, the player takes a long time to start... say about 30 seconds. I noticed in this duration, the control centre receives audio and stops many times (about 14) before finally deciding to play the audio. So it will start the timeline (only in the control centre) and go up to 4-6 seconds then collapse to 0 only to again repeat this ... till it finally plays.

    Is there a solution to this?

    opened by mhmmdmz 8
  • Adding a way to disable items in the queue

    Adding a way to disable items in the queue

    This PR is an implementation of what is discussed in #61. The goal here is to define at runtime - and before an item is played - if an AudioItem should be played or not.

    This allows the original request to disable remote items when WiFi connection is down, but also allows other use cases.

    Here's the todo list:

    • [x] Added a delegate method to check if an item is playable or not
    • [x] Use that method in AudioItemQueue to return the correct values in nextItem(), hasNextItem, previousItem() and hasPreviousItem.
    • [x] Verify the implementation with new unit tests.
    enhancement 
    opened by delannoyk 7
  • Background app 30 sec after that music not playing always player state show paused.

    Background app 30 sec after that music not playing always player state show paused.

    Background Task 128 ("Called by KDEAudioPlayer, from $sSo13UIApplicationC19beginBackgroundTask17expirationHandlerSo012UIBackgroundD10IdentifierayycSg_tFTO"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this.

    opened by maulik3107 5
  • Turn on after stop

    Turn on after stop

    After playing the last audio, the player goes into Stopped mode and does not turn on anymore. for example, reached the last audio and after the end the last audio will automatically skip to the beginning and if you need to start it again.

    How can i do this?

    opened by rastaman111 0
Owner
Kevin Delannoy
Kevin Delannoy
An iOS and macOS audio visualization framework built upon Core Audio useful for anyone doing real-time, low-latency audio processing and visualizations.

A simple, intuitive audio framework for iOS and OSX. Deprecated EZAudio has recently been deprecated in favor of AudioKit. However, since some people

Syed Haris Ali 4.9k Jan 2, 2023
A very simple soundboard that plays the first 5 seconds of the CSI Miami theme (YEAAAAAAAAAA)

MiamiSunglasses This app is a single-sound soundboard that plays the first few seconds of the CSI Miami theme song when you press the sunglasses. Disc

Adrian Edwards 4 Feb 10, 2022
The Amazing Audio Engine is a sophisticated framework for iOS audio applications, built so you don't have to.

Important Notice: The Amazing Audio Engine has been retired. See the announcement here The Amazing Audio Engine The Amazing Audio Engine is a sophisti

null 523 Nov 12, 2022
AudiosPlugin is a Godot iOS Audio Plugin that resolves the audio recording issue in iOS for Godot Engine.

This plugin solves the Godot game engine audio recording and playback issue in iOS devices. Please open the Audios Plugin XCode Project and compile the project. You can also use the libaudios_plugin.a binary in your project.

null 3 Dec 22, 2022
App for adding and listening audio files

SomeSa SomeSa (самса) – приложение, позволяющее загружать и воспроизводить произвольные аудиофайлы. Протестировано на форматах файлов .wav и .mp3, раз

Yegor Dobrodeyev 0 Nov 7, 2021
ModernAVPlayer is a persistence AVPlayer wrapper

ModernAVPlayer is a persistence AVPlayer wrapper ++ Cool features ++ Get 9 nice and relevant player states (playing, buffering, loading

no reason PROJECTS 259 Dec 24, 2022
FDWaveformView is an easy way to display an audio waveform in your app

FDWaveformView is an easy way to display an audio waveform in your app. It is a nice visualization to show a playing audio file or to select a position in a file.

William Entriken 1.1k Dec 21, 2022
AIB indicates for your app users which audio is playing. Just like the Podcasts app.

Audio Indicator Bars for iOS and tvOS Indicates for your app users which audio is playing. Just like the Podcasts app. Index Requirements and Details

Leonardo Cardoso 285 Nov 23, 2022
KeyAudioManager - A swift package to make it a lot easier to play audio in your app

KeyAudioManager A swift package to make it a lot easier to play audio in your ap

Pedro Esli 3 Apr 28, 2022
Background sounds feature from iOS 15 on iOS 11+

Tranquil Background sounds feature from iOS 15 on iOS 11+ Only physically tested on iOS 14.3 and iOS 12.1.2, but it should support iOS 11 - iOS 15.x (

Dana Buehre 4 Dec 15, 2022
AudioKit is an audio synthesis, processing, and analysis platform for iOS, macOS, and tvOS.

AudioKit is an audio synthesis, processing, and analysis platform for iOS, macOS (including Catalyst), and tvOS. Installation To add AudioKit

AudioKit 9.5k Dec 31, 2022
Voice Memos is an audio recorder App for iPhone and iPad that covers some of the new technologies and APIs introduced in iOS 8 written in Swift.

VoiceMemos Voice Memos is a voice recorder App for iPhone and iPad that covers some of the new technologies and APIs introduced in iOS 8 written in Sw

Zhouqi Mo 322 Aug 4, 2022
Swift audio synthesis, processing, & analysis platform for iOS, macOS and tvOS

AudioKit AudioKit is an audio synthesis, processing, and analysis platform for iOS, macOS (including Catalyst), and tvOS. Installation To add AudioKit

AudioKit 8.7k Sep 30, 2021
YiVideoEditor is a library for rotating, cropping, adding layers (watermark) and as well as adding audio (music) to the videos.

YiVideoEditor YiVideoEditor is a library for rotating, cropping, adding layers (watermark) and as well as adding audio (music) to the videos. YiVideoE

coderyi 97 Dec 14, 2022
Radio Streams from all over the world. Free and Open.

RadioBrowserKit - The Swift SDK for Radio Browser Radio Streams from all over the world. Free and Open. RadioBrowserKit is a Swift package which lets

Frank Gregor 5 Oct 17, 2022
Painless high-performance audio on iOS and Mac OS X

An analgesic for high-performance audio on iOS and OSX. Really fast audio in iOS and Mac OS X using Audio Units is hard, and will leave you scarred an

Alex Wiltschko 2.2k Nov 23, 2022
Audio Filters on iOS and OSX

Audio Filters on iOS and OSX Implement high quality audio filters with just a few lines of code and Novocaine, or your own audio library of choice. NV

Bart Olsthoorn 411 Dec 16, 2022
Simple command line utility for switching audio inputs and outputs on macOS

Switch Audio Simple command line utility for switching audio inputs and outputs

Daniel Hladík 3 Nov 22, 2022
This app demonstrates how to use the Google Cloud Speech API and Apple on-device Speech library to recognize speech in live recorded audio.

SpeechRecognitionIOS This app demonstrates how to use Google Cloud Speech API and Apple on-device Speech library to recognize speech in live audio rec

Josh Uvi 0 Mar 11, 2022