播放来自 Parse.com 的音频

Playing Audio From Parse.com

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

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

此代码会停止正在播放的 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()

您在此处使用错误的方法获取 NSURL,您尝试从指向远程服务器资源的 URL 创建本地文件 URL .

您应该使用接受 URL 字符串作为输入的初始化器而不是 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?