Search code examples
iosswiftaudiouiimageviewdelay

How to add a delay to a sound being played with swift


I am a total swift beginner and am working on a tutorial project (magic 8 ball) and have been successful at doing the following: - play a specific sound when the "Ask" button is pressed. - play a specific sound when for each of the randomly picked images

However now the sound that should play whenever the button is pressed only plays once and from there on i only hear the sounds that are being displayed with each image. Is this because I am "stuck" in the "if - else" loop ? Or do I have to delay the sounds that are being played for each image in the array ?

Thanks so much for your help !

Here is my code:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    @IBOutlet weak var magicBall: UIImageView!

    var magicBallDisplay = 1
    var audioPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

      magicBall.image = #imageLiteral(resourceName: "theanswerisyes")

    }

    @IBAction func askButtonPressed(_ sender: UIButton) {

        let magicBallArray = [ #imageLiteral(resourceName: "askagainlater"),#imageLiteral(resourceName: "no"),#imageLiteral(resourceName: "theanswerisyes"),#imageLiteral(resourceName: "yes"),#imageLiteral(resourceName: "noidea")]
        magicBall.image = magicBallArray.randomElement()

        let soundURL = NSURL(fileURLWithPath: Bundle.main.path(forResource: "Ask", ofType: "wav")!)

        do{
            audioPlayer = try AVAudioPlayer(contentsOf: soundURL as URL)

        }catch {
            print("there was some error. The error was \(error)")
        }
        audioPlayer.play()

        if (magicBall.image?.isEqual(UIImage(named: "yes")))! {

            let soundURL = NSURL(fileURLWithPath: Bundle.main.path(forResource: "yes", ofType: "mp3")!)
            do{
            audioPlayer = try AVAudioPlayer(contentsOf: soundURL as URL)

            } catch {
            print("there was some error. The error was \(error)")
            }
             audioPlayer.play()
        }
        else if (magicBall.image?.isEqual(UIImage(named: "no")))! {
            let soundURL = NSURL(fileURLWithPath: Bundle.main.path(forResource: "no", ofType: "mp3")!)
        do{
        audioPlayer = try AVAudioPlayer(contentsOf: soundURL as URL)

        } catch {
        print("there was some error. The error was \(error)")
        }
         audioPlayer.play()
        }
        else if (magicBall.image?.isEqual(UIImage(named: "theanswerisyes")))! {
            let soundURL = NSURL(fileURLWithPath: Bundle.main.path(forResource: "theanswerisyes", ofType: "mp3")!)
        do{
        audioPlayer = try AVAudioPlayer(contentsOf: soundURL as URL)

        } catch {
        print("there was some error. The error was \(error)")
        }
         audioPlayer.play()
        }
        else if (magicBall.image?.isEqual(UIImage(named: "noidea")))! {
            let soundURL = NSURL(fileURLWithPath: Bundle.main.path(forResource: "noidea", ofType: "mp3")!)
        do{
        audioPlayer = try AVAudioPlayer(contentsOf: soundURL as URL)

        } catch {
        print("there was some error. The error was \(error)")
        }
         audioPlayer.play()
        }
        else if (magicBall.image?.isEqual(UIImage(named: "askagainlater")))! {
            let soundURL = NSURL(fileURLWithPath: Bundle.main.path(forResource: "askagainlater", ofType: "mp3")!)
        do{
        audioPlayer = try AVAudioPlayer(contentsOf: soundURL as URL)

        } catch {
        print("there was some error. The error was \(error)")
        }
         audioPlayer.play()
        }

    }

    }

Solution

  • I make a new project and change your code to this:

    import UIKit
    import AVFoundation
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var magicBall: UIImageView!
        // you never used this
        //var magicBallDisplay = 1
        var audioPlayer = AVAudioPlayer()
        override func viewDidLoad() {
            super.viewDidLoad()
            magicBall.image = #imageLiteral(resourceName: "theanswerisyes")
        }
    
        @IBAction func askButtonPressed(_ sender: UIButton) {
            // because you have audio file and image with equal name i made array of string
            let magicBallArray = [ "yes","no","theanswerisyes","noidea","askagainlater"]
            // check if i get not null item
            guard let choosedImageName = magicBallArray.randomElement() else {return}
            print(choosedImageName)
            // set image with random picked name
            magicBall.image = UIImage(named: choosedImageName)
            // play ask sound
            playSound(fileName: "Ask", fileType: "wav")
            // play picked image sound after 10 second from now()
            // change number to your needed time
            DispatchQueue.main.asyncAfter(deadline: .now() + 10.0, execute: {
                self.playSound(fileName: choosedImageName, fileType: "mp3")
            })
        }
    
        private func playSound(fileName: String, fileType: String)
        {
            // check if you find the audio file
            guard let url = Bundle.main.path(forResource: fileName, ofType: fileType) else {
                print("path not found")
                return
            }
            // make NSURL from path
            let soundURL = NSURL(fileURLWithPath: url)
    
            do{
                audioPlayer = try AVAudioPlayer(contentsOf: soundURL as URL)
    
            } catch {
                print("there was some error. The error was \(error)")
            }
            audioPlayer.play()
        }
    }
    

    I explain code for you. I didn't enable the button. you can improve this code when you came stronger in swift