无法在xcode 6 swift中播放音频

无法在xcode 6 swift中播放音频,xcode,audio,swift,Xcode,Audio,Swift,我通常创建一个sound类,然后使用函数调用声音,如下所示: 声音等级: import UIKit import AVFoundation class PlaySoundViewController: UIViewController { var audioplayer: AVAudioPlayer! override func viewDidLoad() { super.viewDidLoad() // Do any additional

我通常创建一个sound类,然后使用函数调用声音,如下所示:

声音等级:

import UIKit
import AVFoundation

class PlaySoundViewController: UIViewController {
    var audioplayer: AVAudioPlayer!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        if var filePath = NSBundle.mainBundle().pathForResource("movie_quote", ofType: "mp3") {
            var fileUrl = NSURL.fileURLWithPath(filePath)
            audioplayer = AVAudioPlayer(contentsOfURL: fileUrl, error: nil)


        }else{
        println("path file is empty")
        }
    }

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

    @IBAction func PlaySoundSlow(sender: UIButton) {
        audioplayer.play()
    }
播放声音功能:

//需要AVAudioPlayer吗?实例

//需要AVFoundation导入

//需要AVAudioPlayerDelegate

var audioPlayer:AVAudioPlayer()

func播放声音(声音:声音){

让调度队列= 调度\获取\全局\队列(调度\队列\优先级\默认值,0)

希望这对你有帮助

class Sound {
    //properties
    var fileName = ""
    var fileExtension = ""

    //custom init
    init(fileName: String, fileExtension: String) {
        self.fileName = fileName
        self.fileExtension = fileExtension
    }
}
dispatch_async(dispatchQueue, {[weak self] in
    let mainBundle = NSBundle.mainBundle()

    /* Find the location of our file to feed to the audio player */
    let filePath = mainBundle.pathForResource(sound.fileName, ofType:sound.fileExtension)

    if let path = filePath{
        let fileData = NSData(contentsOfFile: path)

        var error:NSError?

        /* Start the audio player */
        self!.audioPlayer = AVAudioPlayer(data: fileData, error: &error)

        /* Did we get an instance of AVAudioPlayer? */
        if let player = self!.audioPlayer{
            /* Set the delegate and start playing */
            player.delegate = self
            if player.prepareToPlay() && player.play(){
                /* Successfully started playing */
            } else {
                /* Failed to play */
            }
        } else {
            /* Failed to instantiate AVAudioPlayer */
        }
    }

})