按下手势识别器时AVAudioPlayer无法播放声音

时间:2017-07-29 15:17:56

标签: ios swift avfoundation avaudioplayer uitapgesturerecognizer

我正在创建一个flashcard应用程序。现在我正处于可以向左和向右滑动每个图像的位置。每个图像都有一个与之关联的声音文件。单击图像时,应播放与其关联的声音文件。截至目前,我可以滑动图像,但是当我点击图像时没有声音播放。代码如下。

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代码的类,并列出了每个&#34; Card&#34;(下面的代码)的定义。

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播放器:AVAudioPlayer?在课堂之外推荐,但仍然没有运气。我已经添加了AVAudioPlayer框架,因此也不是问题。

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:1)

我认为问题在你的扩展中,你在

给出了错误的扩展名
 guard let url = Bundle.main.url(forResource: self.soundUrl, withExtension: "m4a")

并且此处不需要扩展

Card(image: UIImage(named: "alligator")!, soundUrl: "Alligator"),
    Card(image: UIImage(named: "apple")!, soundUrl: "Apple"),
    Card(image: UIImage(named: "ball")!, soundUrl: "Ball")

使用您的文件扩展名 .mp4

更改扩展程序

答案 1 :(得分:1)

您必须将文件扩展名删除为:

let itemList:[Card] = [
    Card(image: UIImage(named: "alligator")!, soundUrl: "Alligator"),
    Card(image: UIImage(named: "apple")!, soundUrl: "Apple"),
    Card(image: UIImage(named: "ball")!, soundUrl: "Ball")
]

因为您要在func playSound()函数中添加扩展名

guard let url = Bundle.main.url(forResource: self.soundUrl, withExtension: "m4a") else { return }

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()
    } catch let error {
        print(error.localizedDescription)
    }
}

所以它会为您的文件名添加双重扩展名,而这在你的包中是不可用的。

注意:确保您的文件名和扩展名与您的包文件名相同。 该文件需要正确导入(Project Build Phases&gt; Copy Bundle Resources)。

Add folder

Folders look as

答案 2 :(得分:0)

我认为该问题是由于NSDefaultRunLoopMode引起的,其中两个事件同时运行。当我以毫秒为单位更新标签时,我遇到了这个NSDefaultRunLoopMode的问题。在打开键盘时它只在那个时间没有更新。然后解决这个问题我在NSRunLoopCommonModes中运行该计时器。我不确定它是否适用于您的情况,但仍然可以共享,如果它可以帮助您..