ivsall2012 / AHAudioPlayer

An Audio wrapper with background mode and remote control available

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AHAudioPlayer

AHAudioPlayer has standard audio players' functionalities.
Additionally, it handles most of background related works for you.
The only thing you need to do is to implement its delegate to provide info for background mode.

Usage

In Xcode, turn on background modes for your target first.

If any of tracks you are using is through http, then you need to modify your info.plist. See this https://stackoverflow.com/questions/30731785/how-do-i-load-an-http-url-with-app-transport-security-enabled-in-ios-9

Note: The latest simulators come with Xcode9 seem to have some problem with pause when in background. Try it out on real device if you can.

Play

let url = URL(string: "https://mp3l.jamendo.com/?trackid=887202&format=mp31&from=app-devsite")
AHAudioPlayerManager.shared.play(trackId: 0, trackURL: url!)

Acive Monitoring for UI related components

let timer = Timer(timeInterval: 0.1, target: self, selector: #selector(updatePlayer), userInfo: nil, repeats: true)
RunLoop.main.add(timer!, forMode: .commonModes)

func updatePlayer() {
let loadedProgress = CGFloat(AHAudioPlayerManager.shared.loadedProgress)
let progress = AHAudioPlayerManager.shared.progress
let currentTime = AHAudioPlayerManager.shared.currentTimePretty
let duration = AHAudioPlayerManager.shared.durationPretty
let speedStr = AHAudioPlayerManager.shared.rate.rawValue > 0 ? "\(AHAudioPlayerManager.shared.rate.rawValue)x" : "1.0x"
}

Passive Monitoring

A. Using delegate for updating local database and fetching datas for background mode

/// Update every 10s after the track startd to play.
func playerManger(_ manager: AHAudioPlayerManager, updateForTrackId trackId: Int, duration: TimeInterval)

/// Update every 10s after the track startd to play, additionally when paused, resume, and right before stop.
func playerManger(_ manager: AHAudioPlayerManager, updateForTrackId trackId: Int, playedProgress: TimeInterval)

///###### The following five are for audio background mode
/// Return a dict should include ['trackId': Int, 'trackURL': URL]. Return [:] if there's none or network is broken.
func playerMangerGetPreviousTrackInfo(_ manager: AHAudioPlayerManager, currentTrackId: Int) -> [String: Any]

/// Return a dict should include ['trackId': Int, 'trackURL': URL]. Return [:] if there's none or network is broken.
func playerMangerGetNextTrackInfo(_ manager: AHAudioPlayerManager, currentTrackId: Int) -> [String: Any]

func playerMangerGetTrackTitle(_ player: AHAudioPlayerManager, trackId: Int) -> String?

func playerMangerGetAlbumTitle(_ player: AHAudioPlayerManager, trackId: Int) -> String?

func playerMangerGetAlbumCover(_ player: AHAudioPlayerManager,trackId: Int, _ callback: @escaping(_ coverImage: UIImage?)->Void)
///######

B. Using notification for specific UI components to react to events sent out by the playerManger

public let AHAudioPlayerDidStartToPlay = Notification.Name("AHAudioPlayerDidStartToPlay")

public let AHAudioPlayerDidChangeState = Notification.Name("AHAudioPlayerDidChangeState")

/// Sent every time a track is being played
public let AHAudioPlayerDidSwitchPlay = Notification.Name("AHAudioPlayerDidSwitchPlay")

public let AHAudioPlayerDidReachEnd = Notification.Name("AHAudioPlayerDidReachEnd")

public let AHAudioPlayerFailedToReachEnd = Notification.Name("AHAudioPlayerFailedToReachEnd")

Example

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

Requirements

iOS 8.0+

Installation

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

pod "AHAudioPlayer"

Author

Andy Tong, ivsall2012@gmail.com

License

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

About

An Audio wrapper with background mode and remote control available

License:MIT License


Languages

Language:Swift 96.5%Language:Ruby 3.5%