Swift无法播放链接中的音频-iOS [英] Swift won't play audio from link - iOS

查看:183
本文介绍了Swift无法播放链接中的音频-iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Subsonic,因此mp3文件是通过Web服务提供给我的. 当我使用扩展名为.mp3的文件进行测试时,此代码有效.当我通过链接使用它时,下面没有.

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

    }

}

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

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 如何使我的应用程序从没有扩展名的rest API播放音频

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

推荐答案

您可以使用 AVAssetResourceLoader 播放不带扩展名的音频.

You can use AVAssetResourceLoader to play audio without extension.

这里是一个例子.

首先,配置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
    }
}

这篇关于Swift无法播放链接中的音频-iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆