Published in
7 min readJan 10, 2020
彼得潘輸的那一天我做出音樂來慶祝自己
這個小小的作品,我花了不少青春歲月,只能等待好心人的發佈,在這要謝謝我們班上的雙軒(特別重要,真得太感謝他們了),這兩位大大的鼎力相助,可以讓我完成對彼得潘的回憶(輸)!!!以下是基本的畫面!
//以下是對播放跟暫停的圖片與播放作切換
@IBAction func playSong(_ sender: UIButton) {if !isPlaying {playButton.setImage(UIImage(systemName: "pause.fill"), for:.normal)isPlaying = trueaudioPlayer.play()}else{playButton.setImage(UIImage(systemName: "play.fill"), for:.normal)audioPlayer.pause()isPlaying = false}}
再來是對上下首作切換
@IBAction func previousAndNextSong(_ sender: UIButton) {switch sender.restorationIdentifier {case "next":if index == playList.count - 1{isPlaying = falseplayButton.setImage(UIImage(systemName: "play.fill"), for: .normal)}index += 1index %= playList.countcase "previous"://這邊的小於4秒可以讓他換下一首,但是大於4秒就是原來重播if audioPlayer.currentTime().seconds < 4{if index > 0{index -= 1}else if index == 0{index = playList.count-1}}default:break}audioPlayer.pause()findSongPath(index: index)time()updatePlayerUI()audioPlayer.play()}
這個是取得播放的時間
func time(){audioPlayer.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, preferredTimescale: 1), queue: DispatchQueue.main){(CMTime) -> Void inif self.audioPlayer.currentItem?.status == .readyToPlay{let currentTime = CMTimeGetSeconds(self.audioPlayer.currentTime())self.timeSlider!.value = Float(currentTime)let all : Int = Int(currentTime)let m : Int = all % 60let f : Int = Int(all/60)var time : String = ""if f<10{time = "0\(f):"}else{time = "\(f)"}if m<10 {time += "0\(m)"}else {time += "\(m)"}self.timeLabel.text = time}}}_________________________________________
/這邊是取的固定時間func formatConversion(time:Float64) -> String {let songLengthTime = Int(time)let minutes = Int(songLengthTime / 60)let seconds = Int(songLengthTime % 60)var time = ""if minutes < 10 {time = "0\(minutes):"} else {time = "\(minutes)"}if seconds < 10 {time += "0\(seconds)"} else {time += "\(seconds)"}return time}func updatePlayerUI() {let duration = playItem!.asset.durationlet seconds = CMTimeGetSeconds(duration)songLenght.text = formatConversion(time: seconds)}
這是音樂結束時直接換下一首
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: .main){(_) inself.index += 1self.index %= self.playList.countself.audioPlayer.pause()self.findSongPath(index: self.index)self.audioPlayer.play()self.updatePlayerUI()self.time()}_________________________________________//取得音樂func findSongPath(index: Int) {let songName = playList[index].namelet image = UIImage(named: songName)imageLabel.image = imagelet fileUrl = Bundle.main.url(forResource: songName, withExtension: "mp3")!playItem = AVPlayerItem(url: fileUrl)audioPlayer.replaceCurrentItem(with: playItem)}
還有背景的漸層
func viewBackGround(){gridentLayer.frame = view.boundsgridentLayer.colors = [UIColor.blue.cgColor,UIColor.gray.cgColor,UIColor.black.cgColor]view.layer.insertSublayer(gridentLayer, at: 0)gridentLayer.startPoint = CGPoint(x: 0, y: 0)gridentLayer.endPoint = CGPoint(x: 0.5, y: 1)gridentLayer.locations = [0,0.3,0.8,1]}
這個是GitHub,完成度還不是很高還是有一些小小的Bug請多見諒!!!
這個的完成時間絕對超過你的想像(我還太嫩了),對Swift還不是那麼的熟悉只能在網路上、雙軒(2位)同學以及帥氣的彼得潘的意見,真的要努力的學習了有一些方法還真不是很懂!謝謝!