The goal of this tutorial is to dive into a simple iOS recipe - how to play sound and use an Apple library called AVFoundation.
You will be making your first musical instrument! A xylphone.
- How to play sound using AVFoundation and AVAudioPlayer.
- Understand Apple documentation and how to use StackOverflow.
- Functions and methods in Swift.
- Data types.
- Swift loops.
- Variable scope.
- The ViewController lifecycle.
- Error handling in Swift.
- Code refactoring.
- Basic debugging.
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player: AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func keyPressed(_ sender: UIButton) {
playSound()
}
func playSound() {
let url = Bundle.main.url(forResource: "C", withExtension: "wav")
player = try! AVAudioPlayer(contentsOf: url!)
player.play()
}
}