ボタンを押すとアプリがクラッシュ
Swiftでピアノアプリを作っています。
アプリをビルドし、ピアノの鍵盤をタップすると音が鳴らなく、また、アプリが落ちてしまいます。
ソースコードは以下のようになります。
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()
let audioPlayerC3 = try! AVAudioPlayer(contentsOfURL:pianoSoundC3)
audioPlayerC3.prepareToPlay()
let audioPlayerCS = try! AVAudioPlayer(contentsOfURL:pianoSoundCS)
audioPlayerCS.prepareToPlay()
let audioPlayerD = try! AVAudioPlayer(contentsOfURL:pianoSoundD)
audioPlayerD.prepareToPlay()
let audioPlayerDS = try! AVAudioPlayer(contentsOfURL:pianoSoundDS)
audioPlayerDS.prepareToPlay()
let audioPlayerE = try! AVAudioPlayer(contentsOfURL:pianoSoundE)
audioPlayerE.prepareToPlay()
let audioPlayerF = try! AVAudioPlayer(contentsOfURL:pianoSoundF)
audioPlayerF.prepareToPlay()
let audioPlayerFS = try! AVAudioPlayer(contentsOfURL:pianoSoundFS)
audioPlayerFS.prepareToPlay()
let audioPlayerG = try! AVAudioPlayer(contentsOfURL:pianoSoundG)
audioPlayerG.prepareToPlay()
let audioPlayerGS = try! AVAudioPlayer(contentsOfURL:pianoSoundGS)
audioPlayerGS.prepareToPlay()
let audioPlayerA = try! AVAudioPlayer(contentsOfURL:pianoSoundA)
audioPlayerA.prepareToPlay()
let audioPlayerAS = try! AVAudioPlayer(contentsOfURL:pianoSoundAS)
audioPlayerAS.prepareToPlay()
let audioPlayerB = try! AVAudioPlayer(contentsOfURL:pianoSoundB)
audioPlayerB.prepareToPlay()
let audioPlayerC4 = try! AVAudioPlayer(contentsOfURL:pianoSoundC4)
audioPlayerC4.prepareToPlay()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func C3(sender: UIButton) {
audioPlayerC3.play()
}
@IBAction func CS(sender: UIButton) {
audioPlayerCS.play()
}
@IBAction func D(sender: UIButton) {
audioPlayerD.play()
}
@IBAction func DS(sender: UIButton) {
audioPlayerDS.play()
}
@IBAction func E(sender: UIButton) {
audioPlayerE.play()
}
@IBAction func F(sender: UIButton) {
audioPlayerF.play()
}
@IBAction func FS(sender: UIButton) {
audioPlayerFS.play()
}
@IBAction func G(sender: UIButton) {
audioPlayerG.play()
}
@IBAction func GS(sender: UIButton) {
audioPlayerGS.play()
}
@IBAction func A(sender: UIButton) {
audioPlayerA.play()
}
@IBAction func AS(sender: UIButton) {
audioPlayerAS.play()
}
@IBAction func B(sender: UIButton) {
audioPlayerB.play()
}
@IBAction func C4(sender: UIButton) {
audioPlayerC4.play()
}
}