我正在尝试使用AVAssetResourceLoader从URL流式传输一个小音频剪辑,但是当我尝试在控制台中打印时,我可以看到mimetype和内容被正确识别。但是,它没有播放音频。有人可以看看它,告诉我哪里出错了。
import UIKit
import AVFoundation
import MobileCoreServices
class DetailViewController: UIViewController, AVAssetResourceLoaderDelegate, URLSessionDelegate, URLSessionDataDelegate {
var player:AVPlayer = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
session = Foundation.URLSession(configuration: URLSessionConfiguration.background(withIdentifier: "ident"), delegate: self, delegateQueue: OperationQueue.main)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func doTapped(_ sender: AnyObject) {
print("beginning")
setupSession()
let asset = AVURLAsset(url: URL(string: "testing-http://mp3.ffh.de/radioffh/livestream.mp3")!) // This can be replaced by any other api stream
asset.resourceLoader.setDelegate(self, queue: DispatchQueue.main)
let playerItem = AVPlayerItem(asset: asset)
player = AVPlayer(playerItem: playerItem)
player.actionAtItemEnd = AVPlayerActionAtItemEnd.none
player.play()
print("done")
}
@IBAction func playTapped(_ sender: AnyObject) {
print(player.rate)
}
@IBAction func testTapped(_ sender: AnyObject) {
print(player.currentItem?.loadedTimeRanges.first?.timeRangeValue.start.seconds)
}
func setupSession(){
let session = AVAudioSession.sharedInstance()
do{
try session.setCategory(AVAudioSessionCategoryPlayback)
try session.setActive(true)
print("set up")
}catch let err as NSError{
print("error setting up session:", err)
}
}
// RESOURCE LOADER
var request:AVAssetResourceLoadingRequest?
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
print("lR:", loadingRequest)
self.request = loadingRequest
let task = session.dataTask(with: URL(string: (loadingRequest.request.url?.absoluteString.replacingOccurrences(of: "testing-", with: ""))!)!)
task.resume()
return true
}
// URL SESSION DATA
var session:Foundation.URLSession!
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
//print("resp:", response)
completionHandler(.allow)
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
if let request = request, let dataRequest = request.dataRequest{
let neededData = dataRequest.requestedLength - Int(dataRequest.currentOffset)
print("the neededdata is \(neededData)")
if (data.count >= neededData){
print("finishing")
if let contentInformationRequest = request.contentInformationRequest, let mimeType = dataTask.response?.mimeType{
print("finishing 2:", mimeType)
contentInformationRequest.contentLength = dataTask.countOfBytesExpectedToReceive
if let contentType = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType as CFString, nil)?.takeRetainedValue(){
let contentType = contentType as String
print("finishing 3:", contentType)
contentInformationRequest.contentType = contentType
contentInformationRequest.isByteRangeAccessSupported = true
}
}
dataRequest.respond(with: data.subdata(in: 0..<neededData+1))
dataTask.cancel()
request.finishLoading()
self.request = nil
}else{
dataRequest.respond(with: data)
}
}
}
}
我们是否也按照相同的流程从带有标题的网址流式传输视频?
提前致谢
答案 0 :(得分:0)
您不应将以下代码放在urlseesion dataReceive数据函数中:
dataTask.cancel()
request.finishingloading()
self.request = nil
` 他们将停止请求工作并阻止您接收更多数据。