关于编辑额外的内容:
Luaan 也是对的。"
直播需要服务器支持-至少需要能够进行部分下载。有没有其他播放器可以使用那个URL进行直播?" 你也应该尝试使用一个未定义时长的音频URL。
你是否尝试过将其视为一个未定义时长的音频流来处理?
avplayerItem = CachingPlayerItem(url: url, recordingName: recordingName ?? "default.mp3")
CachingPlayerItem将从URL准备一个AVURLAsset。
init(url: URL, customFileExtension: String?, recordingName: String) {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
let scheme = components.scheme,
var urlWithCustomScheme = url.withScheme(cachingPlayerItemScheme) else {
fatalError("Urls without a scheme are not supported")
}
self.recordingName = recordingName
self.url = url
self.initialScheme = scheme
if let ext = customFileExtension {
urlWithCustomScheme.deletePathExtension()
urlWithCustomScheme.appendPathExtension(ext)
self.customFileExtension = ext
}
let asset = AVURLAsset(url: urlWithCustomScheme)
asset.resourceLoader.setDelegate(resourceLoaderDelegate, queue: DispatchQueue.main)
super.init(asset: asset, automaticallyLoadedAssetKeys: nil)
resourceLoaderDelegate.owner = self
addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.new, context: nil)
NotificationCenter.default.addObserver(self, selector: #selector(playbackStalledHandler), name:NSNotification.Name.AVPlayerItemPlaybackStalled, object: self)
}
在初始化AVPlayerItem之后,从中创建一个AVPlayer对象,以便启动从给定URL加载音频流的过程:
player = AVPlayer(playerItem: playerItem)
player.automaticallyWaitsToMinimizeStalling = false
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
if playingFromData {
} else if session == nil {
guard let initialUrl = owner?.url else {
fatalError("internal inconsistency")
}
startDataRequest(with: initialUrl)
}
pendingRequests.insert(loadingRequest)
processPendingRequests()
return true
}
资源安装完成后:
func startDataRequest(with url: URL) {
var recordingName = "default.mp3"
if let recording = owner?.recordingName{
recordingName = recording
}
fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
.appendingPathComponent(recordingName)
let configuration = URLSessionConfiguration.default
configuration.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
session?.dataTask(with: url).resume()
outputStream = OutputStream(url: fileURL, append: true)
outputStream?.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
outputStream?.open()
}
然后,开始将数据字节接收到委托函数中:
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)
现在您已经开始接收实时音频流。
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
let bytesWritten = data.withUnsafeBytes{outputStream?.write($0, maxLength: data.count)}
print("bytes written :\(bytesWritten!) to \(fileURL)")
}
现在创建一个OutputStream对象,打开它,然后将我们在上面的委托函数中接收到的字节追加进去,就完成了,我们保存了实时音频流的所需部分。
摘自:
Medium文章,Mohan Pandey
AVPlayer
正在下载整个文件? - Rhythmic Fistman