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.

thanks!

What’s we gonna make

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

Materials

Please download image and sound files here.
SwiftPiano

Video

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

Code

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() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

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

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

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

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

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

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

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

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

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

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

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

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

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

}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}

Hope it works!


コメントを残す

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