gpt4 book ai didi

swift - Swift:响应中带有应用程序/八位字节流的GET请求

转载 作者:行者123 更新时间:2023-12-02 23:27:53 27 4
gpt4 key购买 nike

我有以下GET请求(用于解释的简短版本)用于播放声音:

func playSound() {

guard let url = URL(string: "https://someWebsite/api/sounds/play?id=1") else {fatalError() }

var request = URLRequest(url: url)

request.httpMethod = "GET"

request.setValue(uuid, forHTTPHeaderField: "X-AUTH-Device")
request.setValue(appid, forHTTPHeaderField: "X-AUTH-AppID")
request.setValue(someToken, forHTTPHeaderField: "X-AUTH-Token")

URLSession.shared.dataTask(with: request) { data, response, _ in
guard let httpResponse = response as? HTTPURLResponse,
(200...599).contains(httpResponse.statusCode),
let mediaData = data else {
return
}
print(mediaData)
}
.resume()
}

因此,在 mediaData中,我收到了一些数据,我所知道的是它必须是 (Content-Type: application/octet-stream)类型的流。因此,我对如何解码/处理此媒体数据以使其可在AVPlayer或其他播放器中播放一无所知。
另外,我尝试在没有获取请求的情况下在AVPlayer中添加URL和 header ,但是它不起作用,我不确定在这里使用哪种方法。预先感谢您的帮助!

最佳答案

这对我有用:
https://github.com/neekeetab/CachingPlayerItem

并在代码中:

   ...URLSession.shared.dataTask(with: request) { data, response, _ in
guard let httpResponse = response as? HTTPURLResponse,
(200...599).contains(httpResponse.statusCode),
let mediaData = data else {
return
}
self.playSound(data: mediaData)
}
.resume()
}

func playSound(data: Data) {
let playerItem = CachingPlayerItem.init(data: data, mimeType: "audio/mpeg", fileExtension: ".mp3")
player = AVPlayer(playerItem: playerItem)
guard let player = player else { return }
player.play()
}

关于swift - Swift:响应中带有应用程序/八位字节流的GET请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59793130/

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