我是一个非常敏捷的初学者,并且正在从事一个教程项目(魔术8球),并且成功完成了以下工作:
-按下“询问”按钮时播放特定的声音。
-为每个随机选取的图像播放特定的声音

但是,现在每按一次该按钮应播放的声音只播放一次,从那以后我只听到每个图像显示的声音。这是因为我“陷入”了“if- else”循环吗?还是我必须延迟阵列中每个图像的播放声音?

非常感谢你的帮助 !

这是我的代码:

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

    }

    }

最佳答案

我创建一个新项目,并将您的代码更改为此:

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

我为您解释代码。
我没有启用该按钮。当您变得更强大时,可以改进此代码

关于ios - 如何为快速播放的声音增加延迟,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59704751/

10-10 11:12
查看更多