从Parse.com播放音频 [英] Playing Audio From Parse.com
问题描述
我正在尝试播放解析时保存的音频文件.我从PFFile中获取了我保存要解析的对象的网址.当我运行该应用程序时,avplayer不会产生音频.我测试了avplayer是否正在按照下面的第一个代码片段进行播放,并打印出正在播放",这表示该播放器正在播放但没有音频.我也尝试设置avplayer的音量,但没有帮助.不明白,如果有人想帮我,为什么它不会播放.
I am trying to play an audio file i saved on parse. I am getting the url from the PFFile from the object i saved to parse. When i run the app the avplayer produces no audio. I tested to see if the avplayer was playing by the first code snippet below and it prints out "Playing" which means the player is playing but no audio. I also tried setting the volume for avplayer and that didn't help. Don't understand why it won't play if anyone would like to help me out.
如果正在播放此代码,它将停止avplayer:
This Code stops avplayer if it is playing:
if (player.rate > 0) && (player.error == nil) {
// player is playing
println("Playing")
} else {
println("Not Playing")
}
AVPlayer代码:
AVPlayer Code:
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.
You are using the wrong method to get a NSURL here, you try to create a local file URL from an URL that points to a resource on a remote server.
而不是NSURL(fileURLWithPath: audioPath)
,您应该使用接受URL字符串作为输入的初始化器(请参见此处
Instead of NSURL(fileURLWithPath: audioPath)
you should use the initalizer that accepts an URL string as the input (see here https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/#//apple_ref/occ/instm/NSURL/initWithString:)
您当前的代码将指向本地文件系统上不存在的本地资源,而应指向解析服务器上的文件.
Your current code would point to a local resource which does not exist on the local filesystem whereas it should point to the file on the Parse server.
作为参考,URLWithString
和fileURLWithPath
之间的区别
Just as a reference, the difference between URLWithString
and fileURLWithPath
What is difference between URLWithString and fileURLWithPath of NSURL?
这篇关于从Parse.com播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!