Fabio Cenni
Fabio Cenni

Reputation: 851

Play sound with a little delay

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

Upvotes: 3

Views: 3572

Answers (3)

Santhosh Umapathi
Santhosh Umapathi

Reputation: 43

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

Upvotes: 0

Tanya Berezovsky
Tanya Berezovsky

Reputation: 91

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

Upvotes: 0

Caleb
Caleb

Reputation: 5616

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

Upvotes: 5

Related Questions