在 Xcode 7 / Swift 2 中播放音频不工作
Playing audio in Xcode 7 / Swift 2 Not Working
我目前正在使用 Xcode 7 beta 4 和 Swift 2,尝试播放音频。我的代码没有出现任何错误,但是当我按下播放按钮时,音频没有播放。我不知道出了什么问题。
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.
}
}
不确定为什么会发生这种情况,但有多种方法可以在您的项目中实现声音。试试这个方法:
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()
}
试试这个:
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")
}
}
希望对您有所帮助!
当前代码工作于 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()
我目前正在使用 Xcode 7 beta 4 和 Swift 2,尝试播放音频。我的代码没有出现任何错误,但是当我按下播放按钮时,音频没有播放。我不知道出了什么问题。
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.
}
}
不确定为什么会发生这种情况,但有多种方法可以在您的项目中实现声音。试试这个方法:
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()
}
试试这个:
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")
}
}
希望对您有所帮助!
当前代码工作于 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()