简体   繁体   English

从目录快速播放音频文件

[英]Playing Audio File from the directory in swift

I am trying to save a audio file from a server to the users phone so they dont have to download it again its always there. 我正在尝试将音频文件从服务器保存到用户电话,这样他们就不必再次将其始终下载。 I have almost everything but i am trying to test and see if the audio file actually plays after it is saved. 我几乎拥有所有东西,但是我正在尝试测试并查看音频文件在保存后是否真正播放。 How do i do this? 我该怎么做呢?

Code i have: 代码我有:

 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

                }

            }


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


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM