从Parse.com播放音频

时间:2015-02-08 02:35:36

标签: ios swift parse-platform avplayer

我正在尝试播放我在解析时保存的音频文件。我从PFFile中获取了我保存到解析对象的URL。当我运行应用程序时,avplayer不会产生任何音频。我测试了avplayer是否正在播放下面的第一个代码片段并打印出“播放”,这意味着播放器正在播放但没有音频。我也试过设置avplayer的音量,这没有帮助。如果有人愿意帮助我的话,不明白为什么它不会播放。

音频文件网址:http://files.parsetfss.com/292b6f11-5fee-4be7-b317-16fd494dfa3d/tfss-ccc3a843-967b-4773-b92e-1cf2e8f3c1c6-testfile.wav

此代码会在播放时停止播放:

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()

1 个答案:

答案 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服务器上的文件。

仅作为参考,URLWithStringfileURLWithPath What is difference between URLWithString and fileURLWithPath of NSURL?之间的差异