gpt4 book ai didi

ios - 在 Xcode 7/Swift 2 中播放音频不工作

转载 作者:行者123 更新时间:2023-11-28 17:59:01 25 4
gpt4 key购买 nike

我目前正在使用 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.

}
}

最佳答案

试试这个:

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

希望这对您有所帮助!

关于ios - 在 Xcode 7/Swift 2 中播放音频不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31715341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com