Playing Audio From Parse.com

自作多情 提交于 2020-01-13 19:51:30

问题


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.

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

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

回答1:


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.

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.

Just as a reference, the difference between URLWithString and fileURLWithPath What is difference between URLWithString and fileURLWithPath of NSURL?



来源:https://stackoverflow.com/questions/28389961/playing-audio-from-parse-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!