iOS Swift Programming1: making Piano app version1

Here I’m going to explain how to make an iPhone piano app (SwiftPiano) by using Apple’s new language Swift.

There are two versions of SwiftPiano tutorials, easier but long one and difficult but short one.

This 1st version might easier for beginner because it doesn’t use complex programming skills (like Array, for in loop, tag property).

please check SwiftPiano2ndversion if you are still interested in after making 1st version.


What’s we gonna make

Swift Piano!
スクリーンショット 2014-10-03 12.36.59


Please download image and sound files here.


Video should be easier to understand how to make an app than just reading the article.


here is all the codes written in ViewController.swift.
Again, this is simple but long code version.
You can check shorter code version here.

import UIKit
import AVFoundation

class ViewController: UIViewController {

var pianoSoundC3 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("C3", ofType: "mp3")!)
var audioPlayerC3 = AVAudioPlayer()

var pianoSoundCS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("C#", ofType: "mp3")!)
var audioPlayerCS = AVAudioPlayer()

var pianoSoundD = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("D", ofType: "mp3")!)
var audioPlayerD = AVAudioPlayer()

var pianoSoundDS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("D#", ofType: "mp3")!)
var audioPlayerDS = AVAudioPlayer()

var pianoSoundE = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("E", ofType: "mp3")!)
var audioPlayerE = AVAudioPlayer()

var pianoSoundF = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("F", ofType: "mp3")!)
var audioPlayerF = AVAudioPlayer()

var pianoSoundFS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("F#", ofType: "mp3")!)
var audioPlayerFS = AVAudioPlayer()

var pianoSoundG = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("G", ofType: "mp3")!)
var audioPlayerG = AVAudioPlayer()

var pianoSoundGS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("G#", ofType: "mp3")!)
var audioPlayerGS = AVAudioPlayer()

var pianoSoundA = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("A", ofType: "mp3")!)
var audioPlayerA = AVAudioPlayer()

var pianoSoundAS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("A#", ofType: "mp3")!)
var audioPlayerAS = AVAudioPlayer()

var pianoSoundB = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("B", ofType: "mp3")!)
var audioPlayerB = AVAudioPlayer()

var pianoSoundC4 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("C4", ofType: "mp3")!)
var audioPlayerC4 = AVAudioPlayer()

override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.

audioPlayerC3 = AVAudioPlayer(contentsOfURL: pianoSoundC3, error: nil)

audioPlayerCS = AVAudioPlayer(contentsOfURL: pianoSoundCS, error: nil)

audioPlayerD = AVAudioPlayer(contentsOfURL: pianoSoundD, error: nil)

audioPlayerDS = AVAudioPlayer(contentsOfURL: pianoSoundDS, error: nil)

audioPlayerE = AVAudioPlayer(contentsOfURL: pianoSoundE, error: nil)

audioPlayerF = AVAudioPlayer(contentsOfURL: pianoSoundF, error: nil)

audioPlayerFS = AVAudioPlayer(contentsOfURL: pianoSoundFS, error: nil)

audioPlayerG = AVAudioPlayer(contentsOfURL: pianoSoundG, error: nil)

audioPlayerGS = AVAudioPlayer(contentsOfURL: pianoSoundGS, error: nil)

audioPlayerA = AVAudioPlayer(contentsOfURL: pianoSoundA, error: nil)

audioPlayerAS = AVAudioPlayer(contentsOfURL: pianoSoundAS, error: nil)

audioPlayerB = AVAudioPlayer(contentsOfURL: pianoSoundB, error: nil)

audioPlayerC4 = AVAudioPlayer(contentsOfURL: pianoSoundC4, error: nil)


override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

@IBAction func C3(sender: UIButton) {
audioPlayerC3.currentTime = 0

@IBAction func CS(sender: UIButton) {
audioPlayerCS.currentTime = 0

@IBAction func D(sender: UIButton) {
audioPlayerD.currentTime = 0

@IBAction func DS(sender: UIButton) {
audioPlayerDS.currentTime = 0

@IBAction func E(sender: UIButton) {
audioPlayerE.currentTime = 0

@IBAction func F(sender: UIButton) {
audioPlayerF.currentTime = 0

@IBAction func FS(sender: UIButton) {
audioPlayerFS.currentTime = 0

@IBAction func G(sender: UIButton) {
audioPlayerG.currentTime = 0

@IBAction func GS(sender: UIButton) {
audioPlayerGS.currentTime = 0

@IBAction func A(sender: UIButton) {
audioPlayerA.currentTime = 0

@IBAction func AS(sender: UIButton) {
audioPlayerAS.currentTime = 0

@IBAction func B(sender: UIButton) {
audioPlayerB.currentTime = 0

@IBAction func C4(sender: UIButton) {
audioPlayerC4.currentTime = 0


Hope it works!


メールアドレスが公開されることはありません。 * が付いている欄は必須項目です