我正在尝试播放我在解析时保存的音频文件。我从我保存要解析的对象的 PFFile 中获取 url。当我运行该应用程序时,avplayer 不产生音频。我通过下面的第一个代码片段测试了 avplayer 是否正在播放,它打印出“正在播放”,这意味着播放器正在播放但没有音频。我还尝试为 avplayer 设置音量,但没有帮助。如果有人愿意帮助我,我不明白为什么它不会播放。
这段代码会在 avplayer 正在播放时停止它:
if (player.rate > 0) && (player.error == nil) {
// player is playing
println("Playing")
} else {
println("Not Playing")
}
AV播放器代码:
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()
最佳答案
您在此处使用错误的方法获取 NSURL,您尝试从指向远程服务器资源的 URL 创建本地文件 URL。
而不是 NSURL(fileURLWithPath: audioPath)
你应该使用接受 URL 字符串作为输入的初始化器(见这里 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?
关于ios - 从 Parse.com 播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28389961/