错误的指令错误

时间:2017-04-05 09:33:33

标签: ios swift xcode avaudioplayer

我是Swift的新手。我的程序应该是钢琴键盘,其中每个按钮必须播放不同的声音。

//
//  ViewController.swift
//  FirstPianoApp
//
//  Created by Alex on 28/03/2017.
//  Copyright © 2017 Alex. All rights reserved.
//

import UIKit
import AVFoundation

class ViewController: UIViewController {

let pianoSoundC3 = Bundle.main.url(forResource: "DO1", withExtension: "mp3")!
var audioPlayerC3 = AVAudioPlayer()

let pianoSoundCS = Bundle.main.url(forResource: "DO#", withExtension: "mp3")!
var audioPlayerCS = AVAudioPlayer()

let pianoSoundD = Bundle.main.url(forResource: "RE", withExtension: "mp3")!
var audioPlayerD = AVAudioPlayer()

let pianoSoundDS = Bundle.main.url(forResource: "RE#", withExtension: "mp3")!
var audioPlayerDS = AVAudioPlayer()

let pianoSoundE = Bundle.main.url(forResource: "MI", withExtension: "mp3")!
var audioPlayerE = AVAudioPlayer()

let pianoSoundF = Bundle.main.url(forResource: "FA", withExtension: "mp3")!
var audioPlayerF = AVAudioPlayer()

let pianoSoundFS = Bundle.main.url(forResource: "FA#", withExtension: "mp3")!
var audioPlayerFS = AVAudioPlayer()

let pianoSoundG = Bundle.main.url(forResource: "SOL", withExtension: "mp3")!
var audioPlayerG = AVAudioPlayer()

let pianoSoundGS = Bundle.main.url(forResource: "SOL#", withExtension: "mp3")!
var audioPlayerGS = AVAudioPlayer()

let pianoSoundA = Bundle.main.url(forResource: "LA", withExtension: "mp3")!
var audioPlayerA = AVAudioPlayer()

let pianoSoundAS = Bundle.main.url(forResource: "LA#", withExtension: "mp3")!
var audioPlayerAS = AVAudioPlayer()

let pianoSoundB = Bundle.main.url(forResource: "SI", withExtension: "mp3")!
var audioPlayerB = AVAudioPlayer()

let pianoSoundC4 = Bundle.main.url(forResource: "DO2", withExtension: "mp3")!
var audioPlayerC4 = AVAudioPlayer()


@IBOutlet weak var lbl: UILabel!


@IBAction func slider(_ sender: UISlider){
    lbl.text = String(Int(sender.value))
}

override func viewDidLoad() {
    super.viewDidLoad()

    do {
    try audioPlayerC3 = AVAudioPlayer(contentsOf: pianoSoundC3 as URL)
    audioPlayerC3.prepareToPlay()

    try audioPlayerCS = AVAudioPlayer(contentsOf: pianoSoundCS as URL)
    audioPlayerCS.prepareToPlay()

    try audioPlayerD = AVAudioPlayer(contentsOf: pianoSoundD as URL)
    audioPlayerD.prepareToPlay()

    try audioPlayerDS = AVAudioPlayer(contentsOf: pianoSoundDS as URL)
    audioPlayerDS.prepareToPlay()

    try audioPlayerE = AVAudioPlayer(contentsOf: pianoSoundE as URL)
    audioPlayerE.prepareToPlay()

    try audioPlayerF = AVAudioPlayer(contentsOf: pianoSoundF as URL)
    audioPlayerF.prepareToPlay()

    try audioPlayerFS = AVAudioPlayer(contentsOf: pianoSoundFS as URL)
    audioPlayerFS.prepareToPlay()

    try audioPlayerG = AVAudioPlayer(contentsOf: pianoSoundG as URL)
    audioPlayerG.prepareToPlay()

    try audioPlayerGS = AVAudioPlayer(contentsOf: pianoSoundGS as URL)
    audioPlayerGS.prepareToPlay()

    try audioPlayerA = AVAudioPlayer(contentsOf: pianoSoundA as URL)
    audioPlayerA.prepareToPlay()

    try audioPlayerAS = AVAudioPlayer(contentsOf: pianoSoundAS as URL)
    audioPlayerAS.prepareToPlay()

    try audioPlayerB = AVAudioPlayer(contentsOf: pianoSoundB as URL)
    audioPlayerB.prepareToPlay()

    try audioPlayerC4 = AVAudioPlayer(contentsOf: pianoSoundC4 as URL)
    audioPlayerC4.prepareToPlay()
    } catch {

    }


}

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

}

我在这一行得到了这个错误

let pianoSoundC3 = Bundle.main.url(forResource: "DO1", withExtension: 
"mp3")!

...........

<<<< exc_bad_instruction(code=exc_i386_invop subcode=0x0)

此说明必须使用按钮"DO1"

吸收声音pianoSoundC3

1 个答案:

答案 0 :(得分:2)

很可能您的捆绑包中没有资源名称DO1.mp3

检查您是否在项目中添加了此类资源,并且路径是否正确。