2014-10-02 4 views
3

Я нахожусь в своем первом приложении для фортепиано, и у меня проблемы.Swift, как играть в звук снова и снова, нажимая кнопку

Как я могу воспроизводить звук снова и снова, когда нажимаю кнопку?

Например, я могу воспроизводить звук 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() 
} 

ответ

1

Вы должны остановить звук, прежде чем сможете воспроизвести его снова. Хорошая дискуссия здесь:

Swift - Have an audio repeat, but it plays in intervals?

+2

благодарит за комментарий. Я не знал, что должен остановить звук. код ниже работал. Большое спасибо! @IBAction func C4 (отправитель: UIButton) { audioPlayerC4.stop() audioPlayerC4.currentTime = 0; audioPlayerC4.play() } –