Overview
Simple stream audio player.
Installation
CocoaPods
CocoaPods is a dependency manager for Cocoa projects.
Specify Wave into your project's Podfile:
platform :ios, '8.0'
use_frameworks!
target '<Your App Target>' do
pod 'Wave', :git => 'git@github.com:XWJACK/Wave.git'
end
⚠️ Because of 'Wave' has been using in CocoaPods with another personal. So, you need add
:git => 'git@github.com:XWJACK/Wave.git'
afterpod 'Wave'
.
Then run the following command:
$ pod install
Carthage
Carthage is a simple, decentralized dependency manager for Cocoa.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Wave into your Xcode project using Carthage, specify it in your Cartfile
:
github "XWJACK/Wave" ~> 0.2.2
Run carthage update
to build the framework and drag the built Wave.framework
into your Xcode project.
Usage
- Create StreamAudioPlayer
let player = StreamAudioPlayer()
- Response data from network or local
player.response(with: data)
/// player will auto play when parsed audio data.
- Custom set property
At least leastPlayPackets
packets to start playing.
player?.leastPlayPackets = 100
- Set Delegate to Self
player.delegate = self
func streamAudioPlayer(_ player: StreamAudioPlayer, parsedProgress progress: Progress) {
DispatchQueue.main.async {
/// Display progress.
}
}
func streamAudioPlayerCompletedParsedAudioInfo(_ player: StreamAudioPlayer) {
DispatchQueue.main.async {
/// Audio info has been parsed that you can seek.
}
}
func streamAudioPlayer(_ player: StreamAudioPlayer, didCompletedPlayFromTime time: TimeInterval) {
DispatchQueue.main.async {
/// Dismiss buffer indicator.
/// Seek to time successful or resume play when data has been parsed.
self.player?.play()
}
}
func streamAudioPlayer(_ player: StreamAudioPlayer, didCompletedPlayAudio isEnd: Bool) {
DispatchQueue.main.async {
if isEnd {
/// Next music
} else {
/// Showing buffer indicator.
}
}
}
- Control
player?.play()
player?.pause()
player?.stop()
/// Return true if time is already can be seek, or this time is out of range between 0 to duration.
/// Otherwise you can using delegate to listen if data is not full parsed.
player?.seek(toTime: 1024)
You should not during seek by UISlider with valueChanged, you can using like this.
/// Add valueChanged target to timeSliderValueChange
timeSlider.addTarget(self, action: #selector(timeSliderValueChange(_:)), for: .valueChanged)
/// Add touchUpInside and touchUpOutside target to timeSliderSeek
timeSlider.addTarget(self, action: #selector(timeSliderSeek(_:)), for: .touchUpInside)
timeSlider.addTarget(self, action: #selector(timeSliderSeek(_:)), for: .touchUpOutside)
@objc fileprivate func timeSliderValueChange(_ sender: MusicPlayerSlider) {
}
@objc fileprivate func timeSliderSeek(_ sender: MusicPlayerSlider) {
if player?.seek(toTime: TimeInterval(sender.value)) == true {
player?.play()
} else {
/// Show buffer indicator
}
}
Flow Diagram
Demo
The best demo is my graduation project: Music
License
Wave is released under the MIT license. See LICENSE for details.