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

时间:2018-04-26 11:08:35

标签: ios swift avplayer

我正在使用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 如何让我的应用程序在没有扩展程序的情况下从其他API播放音频

1 个答案:

答案 0 :(得分:1)

您可以使用AVAssetResourceLoader播放没有扩展名的音频。

这是一个例子。

首先,配置resourceloader

的委托
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
    }
}