我有一个从服务器下载到我的文档目录的音频文件。我试图在 swift 中使用 AVPlayer 玩同样的游戏,但由于某种原因无法这样做。下面是我的全部代码。如果我们用它替换 localAudioPath
它onlineResource
会很好
import UIKit
import AVFoundation
class AudioController: UIViewController {
var player : AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func playAudio(sender: AnyObject) {
var name = "bell.mp3"
var docs = NSSearchPathForDirectoriesInDomains(directory, .UserDomainMask, true)[0] as! String
var localAudioPath = docs.stringByAppendingPathComponent(name)
var onlineResource = "path/to/online/bell.mp3"
if NSFileManager.defaultManager().fileExistsAtPath(localAudioPath!) {
let playerItem = AVPlayerItem(URL: NSURL(string: localAudioPath!)!)
player = AVPlayer(playerItem: playerItem)
player.play()
} else {
/* Alamo fire code to download file to docs directory*/
let destination = Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
Alamofire.download(.GET, NSURL(string: audio.audioUrl)!.http, destination)
.progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
println(totalBytesRead)
}
.response { (request, response, _, error) in
println(response)
}
}
}
}
有人可以告诉我是否可以使用 AVPlayer 播放本地文件?