我正在创建一个flashcard应用程序。现在我正处于可以左右扫描每张图像的地步。每个图像都有一个与之关联的声音文件。点击图像时,与其关联的声音文件应播放。到目前为止,我可以通过图像轻扫,但是当我点击图像时没有声音播放。下面的代码。AVAudioPlayer按下手势识别器时不会播放声音
import UIKit
class SecondViewController: UIViewController , UIGestureRecognizerDelegate {
var imageIndex: Int = 0
@IBAction func home(_ sender: Any) {
performSegue(withIdentifier: "home", sender: self)
}
@IBOutlet weak var imgPhoto: UIImageView!
let itemList:[Card] = [
Card(image: UIImage(named: "alligator")!, soundUrl: "Alligator.m4a"),
Card(image: UIImage(named: "apple")!, soundUrl: "Apple.m4a"),
Card(image: UIImage(named: "ball")!, soundUrl: "Ball.m4a")
]
override func viewDidLoad() {
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imgPhoto.isUserInteractionEnabled = true
imgPhoto.addGestureRecognizer(tapGestureRecognizer)
imgPhoto.image = (itemList[0]).image
// Do any additional setup after loading the view.
imgPhoto.isUserInteractionEnabled = true
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
leftSwipe.cancelsTouchesInView = false
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
rightSwipe.cancelsTouchesInView = false
leftSwipe.direction = .left
rightSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
@IBAction func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
print("hhh")
itemList[imageIndex].playSound()
// Your action
}
func Swiped(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right :
print("User swiped right")
// decrease index first
imageIndex -= 1
// check if index is in range
if imageIndex < 0 {
imageIndex = itemList.count - 1
}
imgPhoto.image = itemList[imageIndex].image
case UISwipeGestureRecognizerDirection.left:
print("User swiped Left")
// increase index first
imageIndex += 1
// check if index is in range
if imageIndex > itemList.count - 1 {
imageIndex = 0
}
imgPhoto.image = itemList[imageIndex].image
default:
break //stops the code/codes nothing.
}
}
}
}
然后我有另一个有AVAudioPlayer代码的类,并且给出了每个“Card”(下面的代码)的定义。
import Foundation; import UIKit; import AVFoundation
var player: AVAudioPlayer?
class Card: NSObject
{
var image: UIImage
var soundUrl: String
init(image: UIImage, soundUrl: String) {
self.image = image
self.soundUrl = soundUrl
}
func playSound()
{
guard let url = Bundle.main.url(forResource: self.soundUrl, withExtension: "m4a") else { return }
do
{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.prepareToPlay()
player.play()
print("play")
} catch let error {
print(error.localizedDescription)
}
}
}
该代码编译没有错误,但没有播放声音。当我点击屏幕时程序确实打印了一行,但是我没有从播放声音功能中打印任何行,这意味着该部分代码没有被执行。我已经把var player:AVAudioPlayer?在班级之外推荐,但仍然没有运气。我已经添加了AVAudioPlayer框架,所以这也不是问题。
我发布你的答案,确保你给出了正确的扩展名,它不是在你的卡片构造函数中要求 –