Skip to content

jharsh1202/Xylophone-iOS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Xylophone

Our Goal

The goal of this tutorial is to dive into a simple iOS recipe - how to play sound and use an Apple library called AVFoundation.

What you will create

You will be making your first musical instrument! A xylphone.

What you will learn

  • 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.

Replacement Code

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()
                
    }
}

Xylophone