gpt4 book ai didi

swift - 在 iOS Swift 中流式传输 Spotify Preview_url

转载 作者:行者123 更新时间:2023-11-30 10:36:18 25 4
gpt4 key购买 nike

我正在尝试流式传输 Spotify 从其 Web API 提供的预览 URL运行良好,我得到了正确的链接,但它们无法正常播放。

示例网址为 https://p.scdn.co/mp3-preview/757bc5732e5c92e36ea249d631120b381a4885e2?cid=e188f3e083e741659b2ef6cfb07cddbd.mp3我正在尝试使用与此 question 中讨论的完全相同的代码,我可以让它与他们在问题中提供的 URL 一起使用,但它不适用于 Spotify 示例。任何帮助将非常感激。

       let url = URL(string: "https://p.scdn.co/mp3-preview/757bc5732e5c92e36ea249d631120b381a4885e2?cid=e188f3e083e741659b2ef6cfb07cddbd.mp3")
let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
player = AVPlayer(playerItem: playerItem)

let playerLayer=AVPlayerLayer(player: player!)
playerLayer.frame=CGRect(x:0, y:0, width:10, height:50)
self.view.layer.addSublayer(playerLayer)
player.play()

最佳答案

该类型的 Spotify 预览网址仅持续 30 秒。它们可以通过 AVPlayer 播放,

import UIKit
import AVFoundation
class AudioVC : UIViewController {
var player = AVAudioPlayer()

override func viewDidLoad() {
let previewURL = "https://p.scdn.co/mp3-preview/757bc5732e5c92e36ea249d631120b381a4885e2?cid=e188f3e083e741659b2ef6cfb07cddbd.mp3"

downloadFileFromURL(url: URL(string: previewURL)!)

}

func downloadFileFromURL(url: URL){
var downloadTask = URLSessionDownloadTask()
downloadTask = URLSession.shared.downloadTask(with: url, completionHandler: {
customURL, response, error in

self.play(url: customURL!)

})

downloadTask.resume()


}

func play(url: URL) {

do {
player = try AVAudioPlayer(contentsOf: url)
player.prepareToPlay()
player.play()

}
catch{
print(error)
}}
}

关于swift - 在 iOS Swift 中流式传输 Spotify Preview_url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57933320/

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