【发布时间】:2016-09-10 12:30:24
【问题描述】:
我需要创建音频播放器。我从 url 获取音频,然后从 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)
}
【问题讨论】: