發出聲音吧!!AVFoundation 實作:木琴App 木琴 🔊
Published in
2 min readJul 22, 2020
成果:
練習使用Action & AVFoundation
製作流程
先下載木琴聲音匯入xcode
Storyboard :
將button 配對每個琴鍵圖片 並命名為 木琴聲音的檔名因為我們要透過按下的currentTitle去抓取對應的wav檔
主要程式碼:
定義一個func playsound 傳入的字串 去播放
透過按下的button抓取title 播放對應的wav檔
@IBAction func SoundButton(_ sender: UIButton) {playSound(SoundNane: sender.currentTitle!)sender.alpha = 0.5DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {sender.alpha = 1.0}}func playSound(SoundNane:String) {let url = Bundle.main.url(forResource: SoundNane, withExtension: "wav")player = try! AVAudioPlayer(contentsOf: url!)player?.play()}
完整程式碼:
希望這篇文章能夠幫助到你(妳)
如有錯誤指正
I hope you found this guide helpful. If not, then please let me know either in the comments below, I’m AlberLee
Swift#10