import UIKit
import AVFoundation
enum Mode {//定义三种音乐播放模式
case repeatSong
case repeatOneSong
case random
}
var playmd : Mode = .repeatSong//初始化播放模式
class ViewController: UIViewController, AVAudioPlayerDelegate {
var audio : AVAudioPlayer?
var times : NSTimer!
var i = 0
let url1 = ["Justin Bieber&Ludacris-Baby", "One Direction-Steal My Girl (Live Acoustic Session)", "Damien Rice&Lisa Hannigan-9 Crimes"]
@IBOutlet weak var progressView: UIProgressView!
//播放暂停键
@IBAction func avaudio(sender: UIButton) {
buttonPlayChangePause()
play()
}
//下一首歌播放键
@IBAction func nextplaying(sender: UIButton) {
progressView.progress = 0
switch playmd {
case .random:
i = Int(arc4random_uniform(UInt32(url1.count-1)))
default:
if(i >= url1.count-1){
i = 0
}
else{
i += 1
}
}
playing()
play()
}
//上一首歌播放键
@IBAction func beforPlaying(sender: AnyObject) {
progressView.progress = 0
switch playmd {
case .random:
i = Int(arc4random_uniform(UInt32(url1.count-1)))
default:
if(i <= 0){
i = url1.count-1
}
else{
i -= 1
}
}
playing()
play()
}
//播放模式切换键
@IBOutlet weak var changemd: UIButton!
@IBAction func changeMode(sender: UIButton) {
changemd.layer.cornerRadius = 5
switch playmd {
case .repeatSong:
playmd = .repeatOneSong
changemd.setImage(UIImage(named: "play_repeat_one"), forState: .Normal)
case .repeatOneSong:
playmd = .random
changemd.setImage(UIImage(named: "play_random"), forState: .Normal)
case .random:
playmd = .repeatSong
changemd.setImage(UIImage(named: "play_repeat"), forState: .Normal)
}
}
//音量调节属性
@IBOutlet weak var sound: UISlider!
//播放按钮属性
@IBOutlet weak var playbutton: UIButton!
//背景图片
@IBOutlet weak var backgroundImage: UIImageView!
//歌名显示
@IBOutlet weak var songsName: UILabel!
//歌曲开始播放时间
@IBOutlet weak var playingTime: UILabel!
//歌曲总时间
@IBOutlet weak var sumTime: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
sound.value = 0.1//音量调节按钮设置
progressView.progress = 0//播放进度条初始化
playing()
audio?.pause()
playbutton.selected = false
}
//AVAudioPlayerDelegate协议方法
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
print("完成播放")
//完成播放后自动跳到下一首(判断模式)
switch playmd {
case .repeatOneSong:
audio?.numberOfLoops = -1
case .repeatSong:
if(i >= url1.count-1){
i = 0
}
else{
i += 1
}
default:
i = Int(arc4random_uniform(UInt32(url1.count-1)))
}
playing()
}
//计时器方法:实现了进度条的同步和播放时间同步
func didtime(times : NSTimer){
progressView.progress = Float((audio?.currentTime)!/(audio?.duration)!)//计算进度条
audio?.volume = sound.value
playingTime.text = "\(Int((audio?.currentTime)!/60)):\(Int((audio?.currentTime)!%60/10))\(Int((audio?.currentTime)!%60%10))"//播放时间计算
sumTime.text = "\(Int((audio?.duration)!/60)):\(Int((audio?.duration)!%60/10))\(Int((audio?.duration)!%60%10))"//音乐总时间计算
}
//开始播放音乐
func playing(){
let url = NSBundle.mainBundle().URLForResource(url1[i], withExtension: "mp3")//将资源url进行存储
try! audio = AVAudioPlayer(contentsOfURL: url!)//传递url
audio?.enableRate = true
audio?.numberOfLoops = 0//设置循环播放
audio?.rate = 1//设置播放速度
audio?.prepareToPlay()
audio?.play()
songsName.text = url1[i]
playbutton.selected = true
audio?.delegate = self
}
//切换播放/暂停
func buttonPlayChangePause() {
playbutton.selected = !playbutton.selected
}
//判断是否播放
func play() {
if playbutton.selected == true{
audio?.play()
times = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: #selector(didtime(_:)), userInfo: nil, repeats: true)
}
else{
audio?.pause()
times?.invalidate()
}
}
}
网友评论