Swift won't play audio from link - iOS

大憨熊 提交于 2020-02-23 03:40:10

问题


I am using Subsonic so mp3 files are served to me via a webservice. When I test using files that have a .mp3 extension this code works. When I use it with the link I have below it does not.

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

    }

}

Browsing the link in 'url' provides me with the file I'd expect. I have also successfully downloaded the file saved it as MP3 and played it from my documents container, this is not how I want the application to work though.

TL:DR how can I get my application to play audio from a rest API without an extension


回答1:


You can use AVAssetResourceLoader to play audio without extension.

Here is an example.

First, configure the delegate of 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)

then, read audio's data and responds to the resource loader

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


来源:https://stackoverflow.com/questions/50041441/swift-wont-play-audio-from-link-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!