Swift, how to play sound again and again by pressing a button

Tags: audio swift
Question!

I'm in way of making my first piano app and I'm in trouble.

How can I play sound again and again when I press a button?

For example, I can play C4 sound when I press the button. but when I want to play C4 sound again, I have to wait till the sound is completely gone. I want to play C4 sound as many times as I press the button.

Here is my "view controller.swift"

Thanks in advance.

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


Answers

You have to stop the sound before you can play it again. A good discussion here:

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



This video can help you solving your question :)
By: admin