从字符串数组 Swift 播放声音

Posted

技术标签:

【中文标题】从字符串数组 Swift 播放声音【英文标题】:Play Sound From String Arrays Swift 【发布时间】:2019-08-15 18:18:02 【问题描述】:

我正在尝试为集合视图中的每个图像播放声音文件,因此如果用户点击图像,用户将听到该图像的名称。例如,如果用户点击猫的图像,用户将听到声音文件“猫”。我创建了图像的集合视图,并将 UITapGestureRecognizer 添加到单元格中。声音文件在代码最底部的 imageTapped 函数中播放,但我不知道如何编码。我不知道之后要编码什么:如果 tap.state == .recognized,让 imageView = tap.view as? UIImageView 该函数中的当前代码构建,但是当我点击每个图像时我没有听到声音。我真的很感激任何帮助。

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 clas-s-room = ["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, clas-s-room, 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, clas-s-room, 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, clas-s-room, 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)
            
        
    

【问题讨论】:

【参考方案1】:

我认为你让这比你需要的更复杂。

首先,您不断创建arrays 多次,没有必要这样做。你可以这样做

var arrays =  [[String]]()

override func viewDidLoad() 
  super.viewDidLoad()

  // populate the array when your view loads
  arrays = [animals, bodyparts, clas-s-room, 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的更多信息

【讨论】:

安德鲁 - 这行得通!我不知道该怎么感谢你才足够!我已经为此苦苦挣扎了几个星期。我从表格视图开始,然后更改为集合视图,并没有正确更改代码。我是一名初学者,我总是让代码比需要的复杂得多。感谢您的明确指示。我将研究这段代码,以便我知道下次该做什么。祝你一切顺利,再次感谢你!【参考方案2】:

尝试在collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 中调用playSound(file: String?)

从选定的单元格中查找图像

【讨论】:

以上是关于从字符串数组 Swift 播放声音的主要内容,如果未能解决你的问题,请参考以下文章

Swift:无法根据数组播放每个声音序列

Swift 2:为数组中的不同图像播放声音

如何使用 Swift 3.0 播放附加到图像数组的声音?

如何延迟 For 循环直到声音在 Swift 中播放完毕

如何在 Xcode11 中的 Swift 中随机播放数组中的音频文件?

SWIFT 3:将不同的声音文件数组匹配到 CollectionView 中的每个部分