UI播放器加强版(可切换上下首)

import UIKit
import AVFoundation

class ViewController: UIViewController,AVAudioPlayerDelegate {
//添加播放进度
    @IBOutlet weak var jindu: UIProgressView!
    @IBOutlet weak var label: UILabel!
//标记    
    var bz = false
//定义播放歌曲序号    
    var i = 0
    
    var audio : AVAudioPlayer?
    var timer : NSTimer?
//定义歌曲名字数组
    var str = [
               "后会无期",
               "葫芦娃",
               "白桦林",
               "绅士",
               "你的样子"
                ]
 //定义歌曲数组  
    var url = [
                NSBundle.mainBundle().URLForResource("后会无期", withExtension: "mp3"),
                NSBundle.mainBundle().URLForResource("葫芦娃", withExtension: "mp3"),
                NSBundle.mainBundle().URLForResource("白桦林", withExtension: "mp3"),
                NSBundle.mainBundle().URLForResource("绅士", withExtension: "mp3"),
                NSBundle.mainBundle().URLForResource("你的样子", withExtension: "mp3")
                ]
    override func viewDidLoad() {
        super.viewDidLoad()

        
            //尝试播放,如果没错误就播放
            //let url = NSBundle.mainBundle().URLForResource("后会无期", withExtension: "mp3")
            try! audio = AVAudioPlayer(contentsOfURL: url[i]!)
        
    
    }
    
    @IBAction func play(sender: UIButton) {
        do  {

            //加速
//            audio?.enableRate = true
//            audio?.rate = 10
            
            audio?.prepareToPlay()
            audio?.play()
            label.text = str[i]

            print(audio?.duration)
            audio?.delegate = self
            //创建一个定时器
            timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(did(_:)), userInfo: nil, repeats: true)
        }
        catch let error as NSError{
            print(error.code,error.localizedDescription)
        }
        
            if bz {
                pause1()
                bz = false
            }
    
            else {
                play1()
                bz = true
            }
        }
    
    @IBAction func up(sender: UIButton) {
        i=i-1;
        if i == -1{
            i = 4
            print(i)
        }
        label.text = str[i]
        try! audio = AVAudioPlayer(contentsOfURL: url[i]!)
        audio?.prepareToPlay()
        audio?.play()
    }
    
    @IBAction func down(sender: UIButton) {
        i=i+1;
        if i == 5 {
            i = 0
            
        }
        label.text = str[i]
        try! audio = AVAudioPlayer(contentsOfURL: url[i]!)
        audio?.prepareToPlay()
        audio?.play()
    }
    
    
    
    
    func pause1(){
        audio?.pause()
    }
    func play1(){
        audio?.play()
    }
    
//播放进度输出
    func did(timer: NSTimer) {
        print("\(audio?.currentTime)s")
        print("\((audio?.currentTime)! / (audio?.duration)!)")
        jindu.progress = Float((audio?.currentTime)! / (audio?.duration)!)
    }
    
//告诉系统播放完成
      func audioPlayerDidFinishPlaying(player:AVAudioPlayer,successfully flag:Bool) {
        print("播放完成")
    }
}

你可能感兴趣的:(UI播放器加强版(可切换上下首))