yze*_*t00 0 audio xcode ios xcode7 swift2
我目前正在使用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.
}
}
Run Code Online (Sandbox Code Playgroud)
更新 正确的代码:
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.
}
}
Run Code Online (Sandbox Code Playgroud)
Lon*_*ham 12
试试这个:
var audioPlayer: AVAudioPlayer! // Global variable
Run Code Online (Sandbox Code Playgroud)
和玩法:
func playVes() {
do {
self.audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ves", ofType: "m4a")!))
self.audioPlayer.play()
} catch {
print("Error")
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
3582 次 |
| 最近记录: |