我正在尝试播放我在解析时保存的音频文件。我从PFFile中获取了我保存到解析对象的URL。当我运行应用程序时,avplayer不会产生任何音频。我测试了avplayer是否正在播放下面的第一个代码片段并打印出“播放”,这意味着播放器正在播放但没有音频。我也试过设置avplayer的音量,这没有帮助。如果有人愿意帮助我的话,不明白为什么它不会播放。
此代码会在播放时停止播放:
if (player.rate > 0) && (player.error == nil) {
// player is playing
println("Playing")
} else {
println("Not Playing")
}
AVPlayer代码:
let objectAudio: PFObject = object as PFObject
let parseAudio: PFFile = objectAudio.valueForKey("audioFileParse") as PFFile
let audioPath: String = parseAudio.url
let urlParse: NSURL = NSURL(fileURLWithPath: audioPath)!
player = AVPlayer(URL: urlParse)
println(player) //prints out <AVPlayer: 0x79e863c0>
player.volume = 1.0
player.play()
答案 0 :(得分:1)
您使用错误的方法在此处获取NSURL,您尝试从指向远程服务器上的资源的URL创建本地文件URL。
您应该使用接受URL字符串作为输入的initalizer,而不是NSURL(fileURLWithPath: audioPath)
(请参阅此处https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/#//apple_ref/occ/instm/NSURL/initWithString:)
您当前的代码将指向本地文件系统上不存在的本地资源,而它应该指向Parse服务器上的文件。
仅作为参考,URLWithString
和fileURLWithPath
What is difference between URLWithString and fileURLWithPath of NSURL?之间的差异