在Xcode 7 / Swift 2中播放音频不起作用

yzet00

我目前在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")
        }
    }

希望这可以帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

tableView.reloadData()在使用Simulator的Xcode 7 beta 4和Swift2中不起作用

尝试在Xcode 7 / Swift 2中播放视频时出错

音频播放错误-XCODE 7(Swift 2.0)

UIScreen.mainScreen()。nativeBounds.height在使用Xcode 7 / Swift 2,目标iOS7时不起作用

无法使SWRevealViewController工作(Swift 2,Xcode 7)

带有Xcode 7,Swift 2的TouchID

我正在使用Xcode 7和Swift,但是我的控件拖动操作不起作用

Swift 2 Playground(Xcode 7 Beta 4)-数组中的多个类型仅在导入UIKit时才起作用吗?

Xcode 7,Swift 2中的Gmail API iOS

SKCropNode在Xcode 7 + Swift 2中无法正常工作

XCode 7和Swift的困惑

Xcode 7,Swift 2.0,迦太基

URL WebView Swift Xcode 7

setValuesForKeys在Xcode 9中的Swift 4中不起作用

WKScriptMessageHandler 不起作用 ios swift xcode

PromiseKit无法使用Swift 2和Xcode 7进行编译

如果字符串包含语句-Swift 2 Xcode 7

Xcode 7不显示Swift 2的函数调用器

使用swift 2(xcode 7 beta 5)解析注册错误

如何创建图像数组(swift2,xcode7)

Xcode 7 Beta 5 Swift 2冗余符合协议错误

基于NStimer Xcode 7 Swift 2的随机生成器

Swift 2.0 Defer在XCode 7.0.1中不起作用?

自定义字体在Swift / Xcode中不起作用

Swift 自动布局在 Xcode 9.3 中不起作用

NSKeyedArchiver在Swift 3(XCode 8)中不起作用

Xcode 中的 Swift Playground - TapGestureRecognizer 到 UILabel 不起作用

Swift 2.0:Xcode 7中的HTTP GET错误处理

AVAudioPlayer不再在Swift 2.0 / Xcode 7 beta中工作