gpt4 book ai didi

ios - 如何加载和播放音频文件列表

转载 作者:行者123 更新时间:2023-11-30 13:46:49 24 4
gpt4 key购买 nike

我有两个 View Controller collectionViewChildimageViewController。我在 imageViewController 中加载图像。现在,在该图像上我有一个按钮,我想在每个图像上播放特定的声音。我知道如何播放单个音频声音,但不知道如何选择特定声音并与所需的图像一起播放。

// collectionViewChild , here i want to display the image in the collectionviewChild



func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("showDetailedImage", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){ if segue.identifier == "showDetailedImage" {
let indexPaths = self.collectionViewChild!.indexPathsForSelectedItems()!
let indexPath = indexPaths[0] as NSIndexPath
let vc = segue.destinationViewController as! imageViewController
vc.title = self.tableData[indexPath.row]
vc.image = images[indexPath.row] as! UIImage
}}}

//这是ImageViewController

import UIKit
import AVFoundation

class imageViewController: UIViewController {
@IBOutlet weak var imageView03: UIImageView!
var selMake = UIImage()
var image = UIImage()
var audioPlayer = AVAudioPlayer()

// on this button i want to play the specific audio file
@IBAction func soundButton(sender: AnyObject) {
let soundLocation = NSBundle.mainBundle().pathForResource("2", ofType: ".mp3")
do {
self.audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: soundLocation!))
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
}
catch {
print("Something bad happened. Try catching specific errors to narrow things down")
}
self.audioPlayer.prepareToPlay()
self.audioPlayer.play()
}
else { print("") }

override func viewDidLoad() {
self.imageView03.image = self.image
super.viewDidLoad()
}

最佳答案

具有多个音频播放器的辅助类不会有帮助吗?我制作了一个简单的音频播放器类,可以轻松扩展为多个轨道。

你可以从 gitHub 获取我的助手 https://github.com/crashoverride777/Swift2-Music-Helper

当按下图像时,您会在按钮方法中播放相应的声音。

Music.sharedInstance.playImage1()
Music.sharedInstance.playImage2()

...

您可以将其与上面的 Mohamed 评论结合起来。

这是您要找的吗?

关于ios - 如何加载和播放音频文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34825658/

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