代码之家  ›  专栏  ›  技术社区  ›  Anthony Rubin

当点击手势识别器按下时,AVAudioPlayer不播放声音

  •  1
  • Anthony Rubin  · 技术社区  · 7 年前

    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.
            }
        }
    }
    }
    

    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)
        }
    }
    }
    

    enter image description here

    enter image description here

    3 回复  |  直到 7 年前
        1
  •  1
  •   Jaydeep Vyas    7 年前

    我认为问题出在你们的分机上,你们在分机上给错了分机

     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")
    

        2
  •  1
  •   Nikhlesh Bagdiya    7 年前

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

    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)
        }
    }
    

    注: 需要正确导入文件(项目构建阶段>复制捆绑资源)。

    Add folder

    Folders look as

        3
  •  0
  •   Jitendra Tanwar    7 年前

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