在swift目录中播放音频文件

时间:2014-11-14 02:12:53

标签: ios audio swift

我正在尝试将音频文件从服务器保存到用户手机,这样他们就不必再次下载它了。我几乎所有的东西,但我试图测试,看看音频文件是否实际播放后保存。我该怎么做?

我的代码:

 var urlWebView = NSURL(string: "http://domain.com//////audios////Nightmares.wav")
        var requestWebView = NSURLRequest(URL: urlWebView)

        NSURLConnection.sendAsynchronousRequest(requestWebView, queue: NSOperationQueue.mainQueue(), completionHandler: {
            response, data, error in

            if error != nil {

                println("There was an error")

            } else {

                let musicFile = (data: data)

                var documentsDirectory:String?

                var paths:[AnyObject] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)

                if paths.count > 0 {

                    documentsDirectory = paths[0] as? String

                    var savePath = documentsDirectory! + "/audio.wav"

                    NSFileManager.defaultManager().createFileAtPath(savePath, contents: data, attributes: nil)
                    self.audioPlayer = AVAudioPlayer(contentsOfURL: savePath, error: nil) 
                    //tried to play it here but i cant since savePath is a string and not actually audio file

                }

            }


        })

1 个答案:

答案 0 :(得分:2)

import UIKit
import Foundation
import AVFoundation

class ViewController: UIViewController {
    @IBOutlet weak var strFiles: UITextView!
    var myPlayer = AVAudioPlayer()
    var yourSound:NSURL?
    func prepareYourSound(myData:NSData) {
        myPlayer = AVAudioPlayer(data: myData, error: nil)
        myPlayer.prepareToPlay()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.



        var urlWebView = NSURL(string: "http://freewavesamples.com/files/Korg-DS-8-Rotary-Organ-C6.wav")!
        var requestWebView = NSURLRequest(URL: urlWebView)

        NSURLConnection.sendAsynchronousRequest(requestWebView, queue: NSOperationQueue.mainQueue(), completionHandler: {
            response, data, error in

            if error != nil {

                println("There was an error")

            } else {

                let musicFile = (data: data)

                var documentsDirectory:String?

                var paths:[AnyObject] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)

                if paths.count > 0 {

                    documentsDirectory = paths[0] as? String

                    var savePath = documentsDirectory! + "/audio.wav"

                    NSFileManager.defaultManager().createFileAtPath(savePath, contents: data, attributes: nil)

                    self.prepareYourSound(musicFile)
                    self.myPlayer.play()
                    //tried to play it here but i cant since savePath is a string and not actually audio file

                    // list your files from disk (documents)

                    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
                    let files = NSFileManager().enumeratorAtPath(documentsPath)
                    var myFiles:[String] = []
                    while let file: AnyObject = files?.nextObject() {
                        myFiles.append(file as String)
                        self.strFiles.text = "\(self.strFiles.text)\n\(file as String)"
                    }




                }

            }


        })

    }

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


}