Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have a sound stop and start immediately on button press

Tags:

ios

swift

audio

I am making a little app for fun to test my learning and I've gotten it for the most part but there is a little caveat to the solution I've come up with.

The app should play a horn sound everytime you click the screen. But this should allow you to continually press it in quick succession and the horn sound plays each time. It should also stop the previous horn sound as well so there are no overlapping sounds.

Here is what I've managed 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, sort of. It stops the sound and sets it back to the beginning, but if you click really quickly it skips and doesn't play sometimes. I want to try ti get around this but I'm not finding much on google. Is there a better solution that will allow me to play this sound like this really quickly?

UPDATE

As per the answer from Duncan, I tried to create two instances of the sound to play and switch between them both but I can't seem to get them to start the sound right when the screen is touched each time it's touched in quick succession. Here is the code I have 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.
    }

}

On each press of the button there is a slight delay before the sound plays. And I want the sound to start over immediately. I want the horn to be able to sound like in this video

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

like image 995
Jordan Avatar asked Dec 15 '25 08:12

Jordan


1 Answers

it's so much simpler than that! if you want the audio to start exactly after the button is pressed, you should change the event to "Touch Down" when you're connecting the button to your code. That's it! and for the button this is enough :

@IBAction func buttonPressed(sender: AnyObject) {

    horn.currentTime = 0
    horn.play()

}
like image 197
Farzam Avatar answered Dec 16 '25 21:12

Farzam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!