Play sound with a little delay
Asked Answered
A

3

3

I have a sound in my app that starts automatically when appear the view; but, as the title says, I'd like that this sounds starts with a little delay, about an half second after the view appear. I tried to use PlayAtTime, but or it does not work or I have set somethings wrong... This is my code:

var player = AVAudioPlayer?
override func viewDidLoad()
{
    super.viewDidLoad()
    playAudioWithDelay()
}

func playAudioWithDelay()
{
    let file = NSBundle.mainBundle().URLForResource("PR1", withExtension: "wav")
    player = AVAudioPlayer(contentsOfURL: file, error: nil)
    player!.volume = 0.5
    player!.numberOfLoops = -1
    player!.playAtTime(//I tried with 0.5 but doesn't work)
    player!.prepareToPlay()
    player!.play()
}
Augite answered 5/8, 2015 at 18:25 Comment(1)
Generalization of this question: #15413514Vietnam
D
5

You can try using this:

let seconds = 1.0//Time To Delay
let delay = seconds * Double(NSEC_PER_SEC)  // nanoseconds per seconds
var dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

dispatch_after(dispatchTime, dispatch_get_main_queue(), {
    //Play Sound here
})

Full code:

func playAudioWithDelay()
{
    let file = NSBundle.mainBundle().URLForResource("PR1", withExtension: "wav")
    player = AVAudioPlayer(contentsOfURL: file, error: nil)
    player!.volume = 0.5
    player!.numberOfLoops = -1
    player!.playAtTime(//I tried with 0.5 but doesn't work)
    player!.prepareToPlay()
    let seconds = 1.0//Time To Delay
    let delay = seconds * Double(NSEC_PER_SEC)  // nanoseconds per seconds
    var dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

    dispatch_after(dispatchTime, dispatch_get_main_queue(), {
        player!.play()
    })
}
Dacha answered 5/8, 2015 at 18:28 Comment(2)
Ok thanks a lot, it works perfectly! so what is playAtTime use for? for to start the audio at a given time?Augite
Any solution to create a delay between repeats.Piteous
J
0

Try the following function implemented in Swift 3.0

 var player: AVAudioPlayer?

func playAudioWithDelay()
{
    let file = Bundle.main.url(forResource: "PR1", withExtension: "wav")

    do {
        player = try AVAudioPlayer(contentsOf: file!)
        player?.volume = 0.5
        player?.numberOfLoops = -1
        player?.prepareToPlay()

    } catch let error as NSError {
        print("error: \(error.localizedDescription)")
    }


    let seconds = 1.0//Time To Delay
    let when = DispatchTime.now() + seconds

    DispatchQueue.main.asyncAfter(deadline: when) {
        self.play()
    }
}

func play() {
    if player?.isPlaying == false {
        player?.play()
    }
}
Jeter answered 19/8, 2017 at 13:29 Comment(0)
M
0

Swift 5 Audio Delay Settings:

 var player: AVAudioPlayer?

func playAudio(soundName: String, extension: String, delay: Double)
{
    let file = Bundle.main.url(forResource: soundName, withExtension: extension)

    do {
        player = try AVAudioPlayer(contentsOf: file!)
        player?.volume = 0.5
        player?.numberOfLoops = -1
        player?.prepareToPlay()

    } catch let error as NSError {
        print("error: \(error.localizedDescription)")
    }


    let seconds = delay//Time To Delay
    let when = DispatchTime.now() + seconds

    DispatchQueue.main.asyncAfter(deadline: when) {
        player.play()
    }
}
Making answered 29/2, 2020 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.