gpt4 book ai didi

ios - 如何为快速播放的声音添加延迟

转载 作者:行者123 更新时间:2023-11-29 05:10:47 25 4
gpt4 key购买 nike

我是一个完全快速的初学者,正在研究一个教程项目(magic 8 ball),并成功地完成了以下工作:- 按下“询问”按钮时播放特定的声音。- 对于每个随机挑选的图像播放特定的声音

但是现在,只要按下按钮就应该播放的声音只会播放一次,从那时起我只能听到每个图像显示的声音。这是因为我“卡在”“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()
}
}

我为你解释一下代码。我没有启用该按钮。当你的 swift 能力更强时,你可以改进这段代码

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com