简体   繁体   English

Swift:AVAssetResourceLoader不从URL播放音频

[英]Swift: AVAssetResourceLoader is not playing audio from URL

I am trying to stream a small audio clip from a URL using AVAssetResourceLoader, but I can see the mimetype and content being correctly identified when I try to print it in console. 我正在尝试使用AVAssetResourceLoader从URL流式传输一个小音频剪辑,但是当我尝试在控制台中打印时,我可以看到mimetype和内容被正确识别。 But, it is not playing the audio. 但是,它没有播放音频。 Can someone take a look at it and tell me where am I going wrong. 有人可以看看它,告诉我哪里出错了。

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

DO we also follow the same process to stream videos from a URL with headers? 我们是否也按照相同的流程从带有标题的网址流式传输视频?

Thanks in advance 提前致谢

You should not place the following codes in urlseesion dataReceive data function: 您不应将以下代码放在urlseesion dataReceive数据函数中:

dataTask.cancel()
request.finishingloading()
self.request = nil

` They will stop requesting job and prevent you from receiving more data. `他们将停止请求工作并阻止您接收更多数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM