StreamingKit is an audio playback streaming library for iOS and Mac OSX. Provides a concise object-oriented API, easy to use, in the CoreAudio framework for audio decompression and playback processing. StreamingKit is the only audio streaming library that supports seamless playback of audio files in different formats. Making address: github.com/tumtumtum/S…

A brief introduction to use:

Define the STKAudioPlayer property audioPlayer and initialize it in viewDidLoad, set the proxy.

audioPlayer = STKAudioPlayer()
audioPlayer.delegate = selfCopy the code

Play the pause button click event:

dynamic func playOrPause(sender:UIButton){
    //1. Enable playback
    //2. Pause playback
    //3. Continue playing
    //4. Rerun
    if firstPlay == true { / / 1 and 4
        let urll = NSURL(string: musicModel.videoUrl)
        if let musicPlayUrl = urll{
            audioPlayer.playURL(musicPlayUrl)
            print("__________ This is the url for the audio playback _______\(musicPlayUrl)The...")}// Set timer to display progress
        timer = NSTimer(timeInterval: 0.1, target: self, selector: #selector(track), userInfo: nil, repeats: true)
        // timer.fire()
        NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)

        // Background control panel information
        newMusicInfo()

        self.firstPlay = false

    }else{
        if self.audioPlayer.state.rawValue ==  3{
             / / pause
            audioPlayer.pause()
        }else{
            / / to continue
            audioPlayer.resume()
        }

    }
}Copy the code

Timer method :(the custom part can be ignored, which is roughly to obtain the current playing time and total time of music in real time and assign the value to the interface)

 dynamic func track(){
    / / print (" * * * * * * total time * * * * * * * * \ (self) audioPlayer) duration) + + + + + + ")
    / / print (" * * * * * * broadcast pace \ * * * * * * * * (self) audioPlayer) progress) + + + + + + ")
    let formatter = NSDateFormatter()
    formatter.dateFormat = "mm:ss"
    let elapsedTimeDate = NSDate(timeIntervalSince1970: audioPlayer.progress)
    self.musicPlayerView.currentLabel.text = formatter.stringFromDate(elapsedTimeDate)
    let timeRemainingDate = NSDate(timeIntervalSince1970: audioPlayer.duration)
    self.musicPlayerView.totalLabel.text = formatter.stringFromDate(timeRemainingDate)
    self.musicPlayerView.progressSlider.value =  Float(audioPlayer.progress)
    self.musicPlayerView.progressSlider.maximumValue = Float(audioPlayer.duration)
    if audioPlayer.progress == audioPlayer.duration-0.01{
        audioPlayer.stop()
        self.musicPlayerView.currentLabel.text = "Prefer"
        self.musicPlayerView.totalLabel.text = "Prefer"
        self.musicPlayerView.progressSlider.value = 0
        self.firstPlay = true
    }
}Copy the code

Bottom progress bar (custom UISlider) drag implementation method :[logic is to pause, then through seekToTime method to the specified progress, and then continue]

  dynamic func sliderDown(slider:UISlider){
    audioPlayer.pause()
}
  dynamic func sliderTouchUpInside(slider:UISlider){
    print("+++++ drag the value +++++\(Double(slider.value)) + + +")
    audioPlayer.seekToTime(Double(slider.value))
    audioPlayer.resume()
}Copy the code

Finally, list the STKAudioPlayerDelegate that must be implemented

extension MusicDetailViewController: STKAudioPlayerDelegate {
dynamic func audioPlayer(audioPlayer: STKAudioPlayer, didStartPlayingQueueItemId queueItemId: NSObject){}//typedef NS_OPTIONS(NSInteger, STKAudioPlayerState)
/ / {
// STKAudioPlayerStateReady,
//STKAudioPlayerStateRunning = 1,
//STKAudioPlayerStatePlaying = (1 << 1) | STKAudioPlayerStateRunning,
//STKAudioPlayerStateBuffering = (1 << 2) | STKAudioPlayerStateRunning,
//STKAudioPlayerStatePaused = (1 << 3) | STKAudioPlayerStateRunning,
//STKAudioPlayerStateStopped = (1 << 4),
//STKAudioPlayerStateError = (1 << 5),
//STKAudioPlayerStateDisposed = (1 << 6)
/ /};
dynamic func audioPlayer(audioPlayer: STKAudioPlayer, stateChanged state: STKAudioPlayerState, previousState: STKAudioPlayerState) {
    switch state.rawValue {
    case 3 : musicPlayerView.startPlayerBtn.selected = true
    case 5: musicPlayerView.startPlayerBtn.selected = false
    case 6: musicPlayerView.startPlayerBtn.selected = false
    case 7: musicPlayerView.startPlayerBtn.selected = false
    default: musicPlayerView.startPlayerBtn.selected = false}}dynamic func audioPlayer(audioPlayer: STKAudioPlayer, didFinishBufferingSourceWithQueueItemId queueItemId: NSObject){}dynamic func audioPlayer(audioPlayer: STKAudioPlayer, unexpectedError errorCode: STKAudioPlayerErrorCode){}dynamic func audioPlayer(audioPlayer: STKAudioPlayer, didFinishPlayingQueueItemId queueItemId: NSObject, withReason stopReason: STKAudioPlayerStopReason, andProgress progress: Double, andDuration duration: Double){}}Copy the code