iOS Developer程序员手机移动程序开发

 Swift使用StreamingKit框架实现音乐播放

2017-01-24  本文已影响722人  轻云_

StreamingKit是一个适用于iOS和Mac OSX的音频播放流媒体库。提供了一个简洁的面向对象API,简单易用,用于在CoreAudio框架下进行音频的解压和播放处理。StreamingKit是唯一支持不同格式音频文件无缝播放的音频播放流媒体库。github地址:https://github.com/tumtumtum/StreamingKit

简单的介绍一下使用:

定义STKAudioPlayer属性audioPlayer 并在viewDidLoad中初始化,设置代理.

audioPlayer = STKAudioPlayer()
audioPlayer.delegate = self

播放暂停按钮的点击事件:

dynamic func playOrPause(sender:UIButton){
    //1.开启播放
    //2.暂停播放
    //3.继续播放
    //4.重新播放
    if firstPlay == true { //1、4
        let urll = NSURL(string: musicModel.videoUrl)
        if let musicPlayUrl = urll{
            audioPlayer.playURL(musicPlayUrl)
            print("__________这是音频播放的url_______\(musicPlayUrl)__________")
        }
        //设置定时器显示进度
        timer = NSTimer(timeInterval: 0.1, target: self, selector: #selector(track), userInfo: nil, repeats: true)
        //            timer.fire()
        NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
        
        //后台控制面板信息
        newMusicInfo()
        
        self.firstPlay = false
    
    }else{
        if self.audioPlayer.state.rawValue ==  3{
             //暂停
            audioPlayer.pause()
        }else{
            //继续
            audioPlayer.resume()
        }
        
    }
}

定时器方法:(自定义部分可忽略不看,大致就是实时获取音乐当前的播放时间与总时间并赋值给界面)

 dynamic func track(){
    //        print("******总时间********\(self.audioPlayer.duration)++++++")
    //        print("******播放进度********\(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 = "00:00"
        self.musicPlayerView.totalLabel.text = "00:00"
        self.musicPlayerView.progressSlider.value = 0
        self.firstPlay = true
    }
}

底部进度条(自定义UISlider)拖动实现的方法:[逻辑是先暂停,再通过seekToTime方法到指定进度,然后继续]

  dynamic func sliderDown(slider:UISlider){
    audioPlayer.pause()
}
  dynamic func sliderTouchUpInside(slider:UISlider){
    print("+++++拖动值+++++\(Double(slider.value))+++")
    audioPlayer.seekToTime(Double(slider.value))
    audioPlayer.resume()
}

最后罗列一下必须实现的代理STKAudioPlayerDelegate

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) {

    
}
}
上一篇下一篇

猜你喜欢

热点阅读