我使用AVPlayer播放网址,我从后端获取URL。最初,我正在将这些项目下载到我的文档目录中,并使用这些URL通过AVAudioPlayer
播放这些文件。我切换到AVPlayer
,所以我可以流式传输音频,而不是下载它们。我看到URL正在成功获取,但是一旦我尝试播放它,我就没有音频。下面是一个URL即时获取的例子:AVPlayer不从后端播放网址
/Users/ellie/Desktop/ellie/sound/uploads/ellie1/Track5.m4a
var player: AVPlayer!
var fetchedURL: NSURL?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//I left out the fetching process
self.fetchedURL = NSURL(string:parseString!)
print("fetchedURL is \(self.fetchedURL!)")
self.playCell()
}
func playCell() {
let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
} catch _ {
}
do {
try audioSession.setActive(true)
} catch _ {
}
print("fetchedURL is \(self.fetchedURL!)")
player = AVPlayer(URL:self.fetchedURL!)
player.play()
}
“我切换到AVPlayer,所以我可以流式传输音频,而不是下载它们”实时流式传输网址在哪里? /Users/ellie/Desktop/ellie/sound/uploads/ellie1/Track5.m4a这看起来不像其他人可以帮助你与这个网址。 – Imran
NSURL(字符串:)仅用于Web链接。对于本地资源路径,您需要使用NSURL(fileURLWithPath :) https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/#//apple_ref/occ/clm/NSURL/ fileURLWithPath: –
为什么不只是使用try?或者试试!而不是“尝试catch_” –