我正在創建一個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框架,所以這也不是問題。
我發佈你的答案,確保你給出了正確的擴展名,它不是在你的卡片構造函數中要求 –