arrays - Swift:音频文件数组

标签 arrays xcode swift audio

如何使用 Swift 2 创建要在 Collection View 上使用的音频文件数组?

有 4 个选项“Ocean”、“Birds”、“Nature”、“Waves”,它们显示为单元格。

单击单元格时,我需要它来播放声音。你能帮忙吗?

    var imageArray = [UIImage(named: "ocean"),UIImage(named: "birds"),UIImage(named: "nature"),UIImage(named: "wave")]
var nameArray = ["Ocean","Birds","Nature","Waves"]



func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.imageArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! homeCollectionViewCell

    cell.iconImage?.image = imageArray[indexPath.item!]
    cell.label.text = nameArray[indexPath.item!]

    return cell
}

最佳答案

你知道怎么播放声音吗?你做到了吗? 如果是这样,您需要使用以下函数并执行如下操作:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        //grab the sound from the array
        let sound = your_array_with_sounds[indexPath.row]

        //if you want to play the sound in this view:
           <code to play the sound>          
        //if you want to play the sound in another view:
           <perform the segue and pass the sound to the view you want>
    }

希望对您有所帮助。如果没有,请在您的问题中添加更多信息,我们将为您提供帮助。

关于arrays - Swift:音频文件数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38135258/

相关文章:

ios - 如何在 swift 4 中获取 ISO8601 格式?

swift - 修复 UITableViewController 的 UINavigationBar 下面的 UILabel 位置

swift - 当我们从PHAsset获取图像时如何枚举工作? ( swift )

JavaScript Array 平面函数未在 React Native 中定义

c - 从一个数组中获取子数组

php - 如何减少向数组添加值的时间?

xcode - 无法使用命令行工具运行 Swift 代码

python - 更改蒙版的NP数组中的值

ios - dequeueReusableCellWithIdentifier 不返回自定义单元格

swift - 具有自定义开始/结束时间的循环 AVPlayer