如何使用Swift 3.0播放附加到Image Array的声音?

是否有可能发挥时,图像-5被称为在下面的图像阵列如何使用Swift 3.0播放附加到Image Array的声音?

下面是完整的代码,包括建立AVAudioPlayer

ImageArray和你建议的代码声音:

import UIKit 

import AVFoundation

class ViewController: UIViewController {

var player:AVAudioPlayer = AVAudioPlayer()

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {

super.viewDidLoad()

var imagesName = ["Image-1.png","Image-2.png","Image-3.png","Image-4.png","Image-5.png","Image-6.png","Image-7.png","Image-8.png","Image-9.png","Image-10.png"]

var images = [UIImage]()

for i in 0..<imagesName.count{

images.append(UIImage(named: imagesName[i])!)

}

imageView.animationImages = images

imageView.animationDuration = 0.5

imageView.animationRepeatCount = 1

imageView.startAnimating()

do

{

let audioPath = Bundle.main.path(forResource: "song", ofType: "mp3")

try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)

}

catch

{

//ERROR

}

let whichImage: Int = 5

let when = DispatchTime.now() + imageView.animationDuration * TimeInterval(whichImage-1)

DispatchQueue.main.asyncAfter(deadline: when) {

self.player.play()

// Play sound here

}

回答:

正如你间隔是固定的,你还挺已经知道当图像-5将被解雇,那么试试这个:

let whichImage: Int = 5 

let when = DispatchTime.now() + imageView.animationDuration * TimeInterval(whichImage-1)

DispatchQueue.main.asyncAfter(deadline: when) {

// Play sound here

}

以上是 如何使用Swift 3.0播放附加到Image Array的声音? 的全部内容, 来源链接: utcz.com/qa/258544.html

回到顶部