以x code播放音频7 /斯威夫特2不工作 [英] Playing audio in Xcode 7 / Swift 2 Not Working
本文介绍了以x code播放音频7 /斯威夫特2不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前X code 7测试版4斯威夫特2,尝试播放音频。我没有得到任何错误在我的code,但是当我preSS播放按钮,音频无法播放。我无法弄清楚什么是错的。
进口的UIKit
进口AVFoundation类VesuviusViewController:UIViewController的{ @IBOutlet弱VAR PausePlay:UIButton的! FUNC playVes(){
做{ _ =尝试AVAudioPlayer(contentsOfURL:NSURL(fileURLWithPath:NSBundle.mainBundle()pathForResource(新村,ofType:M4A))!) } {抓
打印(错误)
}
} @IBAction FUNC为playButton pressed(发件人:AnyObject){
playVes()
} 覆盖FUNC viewDidLoad中(){
super.viewDidLoad() //在载入视图后的任何额外的设置。 } 覆盖FUNC didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
可以重新创建任何资源的处置//。 }
}
更新的
正确的code:
进口的UIKit
进口AVFoundation类VesuviusViewController:UIViewController的{
@IBOutlet弱VAR PausePlay:UIButton的!@IBAction FUNC玩pressed(发件人:AnyObject){
VesSet()
}
VAR audioPlayer:AVAudioPlayer! FUNC VesSet(){
做{
self.audioPlayer =尝试AVAudioPlayer(contentsOfURL:NSURL(fileURLWithPath:NSBundle.mainBundle()pathForResource(新村,ofType:M4A))!)
self.audioPlayer.play() } {抓
打印(错误)
}
}
覆盖FUNC viewDidLoad中(){
super.viewDidLoad() //在载入视图后的任何额外的设置。 } 覆盖FUNC didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
可以重新创建任何资源的处置//。 }
}
解决方案
试试这个:
VAR audioPlayer:AVAudioPlayer! //全局变量
和播放方法:
FUNC playVes(){
做{
self.audioPlayer =尝试AVAudioPlayer(contentsOfURL:NSURL(fileURLWithPath:NSBundle.mainBundle()pathForResource(新村,ofType:M4A))!)
self.audioPlayer.play() } {抓
打印(错误)
}
}
希望这有助于!
I am currently on Xcode 7 beta 4 with Swift 2, trying to play audio. I do not get any error in my code, but when I press the play button, the audio does not play. I cant figure out what is wrong.
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.
}
}
UPDATE Correct code:
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.
}
}
解决方案
Try this:
var audioPlayer: AVAudioPlayer! // Global variable
And play method:
func playVes() {
do {
self.audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ves", ofType: "m4a")!))
self.audioPlayer.play()
} catch {
print("Error")
}
}
Hope this helps!
这篇关于以x code播放音频7 /斯威夫特2不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文