gpt4 book ai didi

ios - 快速播放目录中的音频文件

转载 作者:行者123 更新时间:2023-11-28 05:32:54 25 4
gpt4 key购买 nike

我正在尝试将音频文件从服务器保存到用户手机,这样他们就不必再次下载它,因为它始终存在。我几乎拥有一切,但我正在尝试测试并查看音频文件在保存后是否真正播放。我该怎么做?

我有代码:

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


}

关于ios - 快速播放目录中的音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26921820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com