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. 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:
dataTask.cancel()
request.finishingloading()
self.request = nil
` They will stop requesting job and prevent you from receiving more data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.