gpt4 book ai didi

ios - Swift 不会播放来自链接的音频 - iOS

转载 作者:可可西里 更新时间:2023-11-01 01:08:57 26 4
gpt4 key购买 nike

我正在使用 Subsonic,所以 mp3 文件是通过网络服务提供给我的。当我测试使用具有 .mp3 扩展名的文件时,此代码有效。当我将它与下面的链接一起使用时,它不会。

    var player: AVPlayer!

override func viewDidLoad() {
super.viewDidLoad()

let url = URL(string: "http://192.168.1.74:4040/rest/download?u=admin&p=admin&v=1.12.0&c=myapp&id=114&format=mp3")!
let playerItem = CachingPlayerItem(url: url)
playerItem.delegate = self
player = AVPlayer(playerItem: playerItem)
player.automaticallyWaitsToMinimizeStalling = false
player.play()

}

}

浏览“url”中的链接为我提供了我期望的文件。我还成功下载了保存为 MP3 的文件并从我的文档容器中播放,但这不是我希望应用程序工作的方式。

TL:DR 如何让我的应用程序在没有扩展的情况下从 rest API 播放音频

最佳答案

您可以使用 AVAssetResourceLoader无需扩展名即可播放音频。

这是一个例子。

首先配置resourceloader的delegate

var playerAsset: AVAsset!

if fileURL.pathExtension.count == 0 {
var components = URLComponents(url: fileURL, resolvingAgainstBaseURL: false)!

components.scheme = "fake" // make custom URL scheme
components.path += ".mp3"

playerAsset = AVURLAsset(url: components.url!)
(playerAsset as! AVURLAsset).resourceLoader.setDelegate(self, queue: DispatchQueue.global())
} else {
playerAsset = AVAsset(url: fileURL)
}

let playerItem = AVPlayerItem(asset: playerAsset)

然后,读取音频的数据并响应资源加载器

// MARK: - AVAssetResourceLoaderDelegate methods

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
if let url = loadingRequest.request.url {
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
components.scheme = NSURLFileScheme // replace with the real URL scheme
components.path = String(components.path.dropLast(4))

if let attributes = try? FileManager.default.attributesOfItem(atPath: components.url!.path),
let fileSize = attributes[FileAttributeKey.size] as? Int64 {
loadingRequest.contentInformationRequest?.isByteRangeAccessSupported = true
loadingRequest.contentInformationRequest?.contentType = "audio/mpeg3"
loadingRequest.contentInformationRequest?.contentLength = fileSize

let requestedOffset = loadingRequest.dataRequest!.requestedOffset
let requestedLength = loadingRequest.dataRequest!.requestedLength

if let handle = try? FileHandle(forReadingFrom: components.url!) {
handle.seek(toFileOffset: UInt64(requestedOffset))
let data = handle.readData(ofLength: requestedLength)

loadingRequest.dataRequest?.respond(with: data)
loadingRequest.finishLoading()

return true
} else {
return false
}
} else {
return false
}
} else {
return false
}
}

关于ios - Swift 不会播放来自链接的音频 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50041441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com