Swift 2:为数组中的不同图像播放声音
Swift 2: play sound for different images in array
我得到了一组图像
var cardImages = ["bellota", "manzana", "botas"]
我创建了 myAudioPlayer 来播放声音
let filePath = NSBundle.mainBundle().pathForResource("correct", ofType: "wav")
if let filePath = filePath
{
let filePathURL = NSURL(fileURLWithPath: filePath)
do {
try myAudioPlayer = AVAudioPlayer(contentsOfURL: filePathURL)
} catch {
print("error")
}
}
更改图像的下一个按钮
@IBAction func nextButtonTapped(sender: UIButton) {
if imageIndex < 0 {
imageIndex = maxImages
}
cardImageView.image = UIImage(named: cardImages[imageIndex])
imageIndex++
if imageIndex > maxImages {
imageIndex = 0
}
cardImageView.image = UIImage(named: cardImages[imageIndex])
}
播放声音按钮:
我试图用这种方法做的是在图像改变时播放声音。阵列中的每个图像都有不同的声音。我怎样才能做到这一点?例如 "apple" 将播放声音 1,"orange" 将在按下下一个图像时播放声音 2
@IBAction func playButtonTapped(sender: UIButton) {
myAudioPlayer.play()
}
将您的音频加载隔离到函数中:
func setTrack(audioName: String){
let filePath = NSBundle.mainBundle().pathForResource(audioName, ofType: "wav")
if let filePath = filePath
{
let filePathURL = NSURL(fileURLWithPath: filePath)
do {
try myAudioPlayer = AVAudioPlayer(contentsOfURL: filePathURL)
} catch {
print("error")
}
}
}
然后,在 nextButtonPressed:
@IBAction func nextButtonTapped(sender: UIButton) {
if imageIndex < 0 {
imageIndex = maxImages
}
cardImageView.image = UIImage(named: cardImages[imageIndex])
imageIndex++
if imageIndex > maxImages {
imageIndex = 0
}
cardImageView.image = UIImage(named: cardImages[imageIndex])
self.setTrack(audioList[imageIndex]) // or any other index you need/you have
}
我得到了一组图像
var cardImages = ["bellota", "manzana", "botas"]
我创建了 myAudioPlayer 来播放声音
let filePath = NSBundle.mainBundle().pathForResource("correct", ofType: "wav")
if let filePath = filePath
{
let filePathURL = NSURL(fileURLWithPath: filePath)
do {
try myAudioPlayer = AVAudioPlayer(contentsOfURL: filePathURL)
} catch {
print("error")
}
}
更改图像的下一个按钮
@IBAction func nextButtonTapped(sender: UIButton) {
if imageIndex < 0 {
imageIndex = maxImages
}
cardImageView.image = UIImage(named: cardImages[imageIndex])
imageIndex++
if imageIndex > maxImages {
imageIndex = 0
}
cardImageView.image = UIImage(named: cardImages[imageIndex])
}
播放声音按钮: 我试图用这种方法做的是在图像改变时播放声音。阵列中的每个图像都有不同的声音。我怎样才能做到这一点?例如 "apple" 将播放声音 1,"orange" 将在按下下一个图像时播放声音 2
@IBAction func playButtonTapped(sender: UIButton) {
myAudioPlayer.play()
}
将您的音频加载隔离到函数中:
func setTrack(audioName: String){
let filePath = NSBundle.mainBundle().pathForResource(audioName, ofType: "wav")
if let filePath = filePath
{
let filePathURL = NSURL(fileURLWithPath: filePath)
do {
try myAudioPlayer = AVAudioPlayer(contentsOfURL: filePathURL)
} catch {
print("error")
}
}
}
然后,在 nextButtonPressed:
@IBAction func nextButtonTapped(sender: UIButton) {
if imageIndex < 0 {
imageIndex = maxImages
}
cardImageView.image = UIImage(named: cardImages[imageIndex])
imageIndex++
if imageIndex > maxImages {
imageIndex = 0
}
cardImageView.image = UIImage(named: cardImages[imageIndex])
self.setTrack(audioList[imageIndex]) // or any other index you need/you have
}