Turn off the sound and immediately start by pressing the button

I am making a small fun app to test my learning and I got it for the most part, but there is a little caveat to the solution I came up with.

The app should play a beep every time you tap the screen. But this should allow you to constantly press it in quick succession, and the beep sounds every time. It should also stop the previous beep so that there are no overlapping sounds.

Here is what I have been maintaining so far

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var horn:AVAudioPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareAudios()
    }
    @IBAction func buttonPressed(sender: AnyObject) {
        horn.stop()
        horn.currentTime = 0
        horn.play()
    }

    func prepareAudios() {
        var path = NSBundle.mainBundle().pathForResource("horn", ofType: "mp3")
        horn = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!), error: nil)
        horn.prepareToPlay()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

      

And it works like. It stops the sound and brings it back to the beginning, but if you click really fast it skips and doesn't play sometimes. I want to try to get around this, but I don't find much on google. Is there a better solution that will allow me to play this sound so quickly?

UPDATE

As per the answer from Duncan, I tried to create two instances of audio to play and switch between both of them, but I can't get them to start the audio right away when the screen is touched every time it touches fast succession. Here is the code I tried

class ViewController: UIViewController {

    var horn1:AVAudioPlayer = AVAudioPlayer()
    var horn2:AVAudioPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareAudios()
    }
    var bool = true
    @IBAction func butonPressed(sender: AnyObject) {
        if(bool){
            horn1.play()
            horn2.stop()
            horn2.currentTime = 0
            bool = false
        }else{
            horn2.play()
            horn1.stop()
            horn1.currentTime = 0
            bool = true
        }
    }

    func prepareAudios() {
        var path = NSBundle.mainBundle().pathForResource("horn", ofType: "mp3")
        horn1 = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!), error: nil)
        horn2 = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!), error: nil)
        horn1.prepareToPlay()
        horn2.prepareToPlay()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

      

Each time you press the button, there is a slight delay before the sound plays. And I want the sound to start right away. I wish the horn could sound like this video

https://www.youtube.com/watch?v=Ks5bzvT-D6I

+3


source to share


2 answers


it's much easier! if you want the sound to start exactly after the button is pressed, you must change the event to "Touch Down" when you connect the button to your code. It! and for a button this is enough:



@IBAction func buttonPressed(sender: AnyObject) {

    horn.currentTime = 0
    horn.play()

}

      

+1


source


Try to create 2 instances of your horn player and call prepareToPlay every time. Then when you press the button, stop the one that is playing, start the new one, and call prepareToPlay on the one you stopped to prepare it. You will need to create some programming logic to implement it.



0


source







All Articles