我需要创建一个音频播放器。我从URL获取音频并下载音频,然后播放它。但是我需要在音频下载时播放音频,我该如何做到这一点?
这是我的代码如何从URL播放:
func URLSession(session: NSURLSession,
downloadTask: NSURLSessionDownloadTask,
didFinishDownloadingToURL location: NSURL){
do {
loadingView.hidden=true
actInd.stopAnimating()
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
player=try AVAudioPlayer(contentsOfURL: location)
player?.delegate=self
selectedAudio.status=true
selectedAudio.isDownload=true
player?.enableRate=true
switch speedType_Index {
case 0:
appDelegate.player?.rate=Float(1)
break
case 1:
appDelegate.player?.rate=Float(1.5)
break
case 2:
appDelegate.player?.rate=Float(2)
break
case 3:
appDelegate.player?.rate=Float(0.5)
break
default:
break
}
switch playingType_Index {
case 0:
appDelegate.player?.numberOfLoops = 0
break
case 1:
appDelegate.player?.numberOfLoops = -1
break
default:
break
}
player?.volume=Float(volume)
player?.play()
self.tableView.reloadData()
}catch let error as NSError{
print(error.localizedDescription)
}
}
func URLSession(session: NSURLSession,
downloadTask: NSURLSessionDownloadTask,
didWriteData bytesWritten: Int64,
totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64){
let progress=Float(totalBytesWritten) / Float(totalBytesExpectedToWrite);
bite.text=String(format: "%.1f%%",progress * 100)
}
AVPlayerViewController
(https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVPlayerViewController_Class/),它会提供控件,包括进度条。否则,我相信其他人也遇到过这个问题,所以你应该能够找到解决方案。例如,尝试谷歌搜索“AVPlayer音频流控件”。 - pbodskplayer.play()
时,它是否需要在播放之前先下载整个文件?还是它会自动完成正确的工作? - Ahmad F