【问题标题】:Playing audio in Xcode 7 / Swift 2 Not Working在 Xcode 7 / Swift 2 中播放音频不工作
【发布时间】:2015-07-30 04:46:20
【问题描述】:

我目前正在使用带有 Swift 2 的 Xcode 7 beta 4,尝试播放音频。我的代码没有任何错误,但是当我按下播放按钮时,音频不播放。我不知道出了什么问题。

import UIKit
import AVFoundation

class VesuviusViewController: UIViewController {

    @IBOutlet weak var PausePlay: UIButton!

    func playVes() {
        do {

        _ = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ves", ofType: "m4a")!))

        } catch {
            print("Error")
        }
    }

    @IBAction func playButtonPressed(sender: AnyObject) {
        playVes()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

    }
}

更新 正确代码:

import UIKit
import AVFoundation

class VesuviusViewController: UIViewController {


@IBOutlet weak var PausePlay: UIButton!



@IBAction func playPressed(sender: AnyObject) {
    VesSet()
}
var audioPlayer: AVAudioPlayer!

    func VesSet(){


        do {
            self.audioPlayer =  try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ves", ofType: "m4a")!))
            self.audioPlayer.play()

        } catch {
            print("Error")
        }


    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

    }
}

【问题讨论】:

    标签: ios xcode audio swift2 xcode7


    【解决方案1】:

    试试这个:

    var audioPlayer: AVAudioPlayer! // Global variable
    

    及玩法:

    func playVes() {
            do {
                self.audioPlayer =  try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ves", ofType: "m4a")!))
                self.audioPlayer.play()
    
            } catch {
                print("Error")
            }
        }
    

    希望这会有所帮助!

    【讨论】:

    • 几天来一直在尝试解决这个问题。后来有很多答案......这终于奏效了。非常感谢。
    • @AshishKakkad 你忘了自己
    • 这实际上适用于 iOS 9.3 和 Swift 2.1,与大多数其他解决方案不同...:) 谢谢
    【解决方案2】:

    当前代码从 xCode 7.0 Swift 2.0 开始工作 截至 2015 年 10 月 9 日仍在工作

    //Declaration
    var hitSoundPlayer: AVAudioPlayer?
    
    //Added into the didMoveToView(view: SKView) Function
    do {
        hitSoundPlayer = try AVAudioPlayer(contentsOfURL: hitSound)
            hitSoundPlayer!.prepareToPlay()
    } catch _ {
            hitSoundPlayer = nil
    }
    
    //When you want to play it
    hitSoundPlayer!.play()
    

    【讨论】:

      【解决方案3】:

      不知道为什么会发生这种情况,但有多种方法可以在您的项目中实现声音。试试这个方法:

      class Hello : UIViewController {
      
        var Audio1 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("mySound", ofType: "wav")!)
        var AudioPlayer = AVAudioPlayer()
      
      override func viewDidLoad() {
      
        AudioPlayer = AVAudioPlayer(contentsOfURL: Audio1, error: nil)
      }
      
      func playsound() {
      
        AudioPlayer.play()
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多