我正在制作我的第一部钢琴应用程序而且我遇到了麻烦。
按下按钮时,如何一次又一次地播放声音?
例如,按下按钮时,我可以播放C4声音。 但是当我想再次播放C4声音时,我必须等到声音完全消失。 我想按下按钮时多次播放C4声音。
这是我的"查看controller.swift"
提前致谢。
import UIKit
import AVFoundation
class ViewController: UIViewController {
// make sure to add this sound to your project
var pianoSoundC4 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoC4", ofType: "m4a")!)
var audioPlayerC4 = AVAudioPlayer()
var pianoSoundCS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoC#", ofType: "m4a")!)
var audioPlayerCS = AVAudioPlayer()
var pianoSoundD = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoD", ofType: "m4a")!)
var audioPlayerD = AVAudioPlayer()
var pianoSoundDS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoD#", ofType: "m4a")!)
var audioPlayerDS = AVAudioPlayer()
var pianoSoundE = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoE", ofType: "m4a")!)
var audioPlayerE = AVAudioPlayer()
var pianoSoundF = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoF", ofType: "m4a")!)
var audioPlayerF = AVAudioPlayer()
var pianoSoundFS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoF#", ofType: "m4a")!)
var audioPlayerFS = AVAudioPlayer()
var pianoSoundG = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoG", ofType: "m4a")!)
var audioPlayerG = AVAudioPlayer()
var pianoSoundGS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoG#", ofType: "m4a")!)
var audioPlayerGS = AVAudioPlayer()
var pianoSoundA = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoA", ofType: "m4a")!)
var audioPlayerA = AVAudioPlayer()
var pianoSoundAS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoA#", ofType: "m4a")!)
var audioPlayerAS = AVAudioPlayer()
var pianoSoundB = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoB", ofType: "m4a")!)
var audioPlayerB = AVAudioPlayer()
var pianoSoundC5 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoC5", ofType: "m4a")!)
var audioPlayerC5 = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
audioPlayerC4 = AVAudioPlayer(contentsOfURL: pianoSoundC4, error: nil)
audioPlayerC4.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()
audioPlayerC5 = AVAudioPlayer(contentsOfURL: pianoSoundC5, error: nil)
audioPlayerC5.prepareToPlay()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func C4(sender: UIButton) {
audioPlayerC4.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 C5(sender: UIButton) {
audioPlayerC5.play()
}
答案 0 :(得分:1)
您必须先停止声音才能再次播放。这里有一个很好的讨论: