gpt4 book ai didi

swift - 从字符串数组快速播放声音

转载 作者:行者123 更新时间:2023-12-03 02:28:18 25 4
gpt4 key购买 nike

我正在尝试在集合 View 中为每个图像播放声音文件,因此,如果用户点击图像,则用户将听到该图像的名称。例如,如果用户点击猫的图像,则用户将听到声音文件“猫”。我创建了图像的收集 View ,并将UITapGestureRecognizer添加到该单元格中。声音文件在代码最底部的imageTapped函数中播放,但是我不知道该如何编码。我不知道该怎么写:如果tap.state == .recognized,让imageView = tap.view为? UIImageView {该函数中的当前代码会生成,但是当我点击每个图像时我听不到声音。我真的很感谢您的帮助。

Here is a screenshot

class FlashcardsViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource  {

var animals = ["alligator", "bat", "bear", "beaver", "bird", "butterfly", "camel", "cat", "cheetah", "chicken"]

var bodyparts = ["arm", "ear", "eyes", "foot", "hair", "hand", "head", "leg", "mouth", "nose"]

var classroom = ["backpack", "blackboard", "book", "bookshelf", "calculator", "chair", "chalk", "chalkboard", "clock", "colored pencil"]

var clothes = ["belt", "boots", "coat", "dress", "hat", "jeans", "mittens", "pajamas", "pants", "scarf"]

var family = ["baby", "brother", "dad", "family", "grandma", "grandpa", "grandparents", "mom", "parents", "sister"]

var feelings = ["angry", "cold", "confused", "happy", "hot", "hurt", "sad", "scared", "sick", "tired"]

var food = ["apple", "bacon", "banana", "bean", "blackberry", "blueberry", "broccoli", "butter", "carrot", "cereal"]

var house = ["bathtub", "bed", "bowl", "couch", "cup", "dresser", "fridge", "lamp", "mirror", "plant"]

var transportation = ["ambulance", "bike", "boat", "bus", "car", "firetruck", "helicopter", "motorcycle", "police car", "plane"]

var verbs = ["brush your teeth", "carry", "clap", "color", "cook", "crawl", "cut", "dance", "draw", "drink"]

@IBOutlet weak var collectionView: UICollectionView!

@IBOutlet weak var segmentedControl: UISegmentedControl!

var audioPlayer = AVAudioPlayer()

override func viewDidLoad() {
super.viewDidLoad()

let tap = UITapGestureRecognizer(target: self, action: #selector(self.imageTapped(tap:)))
view.addGestureRecognizer(tap)

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

let arrays = [animals, bodyparts, classroom, clothes, family, feelings, food, house, transportation, verbs]
let sectionArray = arrays[segmentedControl.selectedSegmentIndex]
return sectionArray.count

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell

let arrays = [animals, bodyparts, classroom, clothes, family, feelings, food, house, transportation, verbs]

cell.imageView?.image = UIImage(named: arrays[segmentedControl.selectedSegmentIndex][indexPath.row])

cell.isUserInteractionEnabled = true

return cell

}

@IBAction func segmentedControlAction(_ sender: Any) {

collectionView.reloadData()

}

func playSound(file: String?) {

if let file = file {
let musicFile = URL(fileURLWithPath: file)

do{

try audioPlayer = AVAudioPlayer(contentsOf: musicFile)

audioPlayer.play()

}catch{

}
}
}
@objc func imageTapped(tap: UITapGestureRecognizer) {

_ = tap.view as? UIImageView

print("imageTapped")

let arrays = [animals, bodyparts, classroom, clothes, family, feelings, food, house, transportation, verbs]
let sectionArray = arrays[segmentedControl.selectedSegmentIndex]

if tap.state == .recognized, let imageView = tap.view as? UIImageView {
for (image) in sectionArray {
imageView.image = UIImage (named: image)
if let sound = Bundle.main.path(forResource: image, ofType: "wav") {
playSound(file: sound)
}
}
}
}

}

最佳答案

我认为您正在使这一过程变得比您需要的复杂。

首先,您需要多次创建arrays,因此无需这样做。你可以做这样的事情

var arrays =  [[String]]()

override func viewDidLoad() {
super.viewDidLoad()

// populate the array when your view loads
arrays = [animals, bodyparts, classroom, clothes, family, feelings, food, house, transportation, verbs]

}

这意味着您不需要在 cellForItemAt中构造它,但可以访问刚刚创建的 arrays变量。对于UICollectionView,您应该使用 indexPath.item而不是 indexPath.rowindexPath.row用于UITableViews。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as CollectionViewCell

let array = arrays[segmentedControl.selectedSegmentIndex]

cell.imageView?.image = UIImage(named: array[indexPath.item])

cell.isUserInteractionEnabled = true

return cell

}

您也不需要使用手势识别器。 UICollectionViews带有一个称为 didSelectItemAt的函数,当您点击单元格时会调用该函数。所以你可以做这样的事情。您可能想在播放下一个文件之前检查文件是否已经在播放。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let array = arrays[segmentedControl.selectedSegmentIndex]
let filename = array[indexPath.item]
if let filepath = Bundle.main.path(forResource: filename, ofType: "wav") {
playSound(file: filepath)
}
}

同样,最初使用文件名创建的所有那些数组,除非计划更改它们的内容,否则应将它们声明为 let。与 let相比,使用 var的性能更高。 var仅在计划更改存储的值时才应使用。

所以改变
var animals = ["alligator", "bat", "bear", "beaver", "bird", "butterfly", "camel", "cat", "cheetah", "chicken"]


let animals = ["alligator", "bat", "bear", "beaver", "bird", "butterfly", "camel", "cat", "cheetah", "chicken"]

查看Apple文档以获取有关UICollectionViews https://developer.apple.com/documentation/uikit/uicollectionview的更多信息

关于swift - 从字符串数组快速播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57514306/

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