在X code7播放音频 [英] Playing Audio in Xcode7

查看:237
本文介绍了在X code7播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想,当我点击一个按钮来播放音频,但我得到一个错误这条线code的。

I'm simply trying to play audio when I tap a button, but I get an error with this line of code.

ButtonAudioPlayer = AVAudioPlayer(contentsOfURL: ButtonAudioURL, error: nil)

这是我的整个code:

This is my whole code:

import UIKit
import AVFoundation

class ViewController: UIViewController {

var ButtonAudioURL = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource("Audio File 1", ofType: "mp3")!)

var ButtonAudioPlayer = AVAudioPlayer()



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    ButtonAudioPlayer = AVAudioPlayer(contentsOfURL: ButtonAudioURL, error:       nil)

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func playAudio1(sender: UIButton) {


}

}

该错误是

在调用不正确的参数标签(有'contentsOfURL:错误:',预计'contentsOfURL:fileTypeHint:')。

Incorrect argument label in call (have 'contentsOfURL:error:', expected 'contentsOfURL:fileTypeHint:').

当我改变错误fileTypeHint,它仍然无法正常工作。

When I change error to fileTypeHint, it still does not work.

推荐答案

您需要通过更改该行,以使用新雨燕2语法

You need to use the new Swift 2 syntax by changing that line to

ButtonAudioPlayer = try! AVAudioPlayer(contentsOfURL: ButtonAudioURL)

这篇关于在X code7播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆