早上好。今天,我有一个要求,详细信息是:我有一个受保护的mp3歌曲URL(例如:http://abcd.com/sample.mp3),并尝试使用AVPlayer播放该歌曲。 发布此问题之前,我进行了很多搜索,但未能解决问题。我尝试过的以下代码,但歌曲未播放。任何建议/想法可能会非常有帮助。在此先感谢
方法1:----不播放任何歌曲
let url = URL.init(string: "http://abcd.com/sample.mp3")
var audioplayer : AVPlayer?
let credential = URLCredential(
user: "username",
password: "password",
persistence: .forSession
)
let protectionSpace = URLProtectionSpace(
host: "abcd.com",
port: 443,
protocol: "http",
realm: nil,
authenticationMethod: NSURLAuthenticationMethodHTTPBasic
)
URLCredentialStorage.shared.setDefaultCredential(credential, for: protectionSpace)
let playerItem: AVPlayerItem = AVPlayerItem(url: url!)
audioplayer = AVPlayer(playerItem: playerItem)
audioplayer?.play()
方法2:在audioplayer上崩溃?.play()并显示崩溃,因为无法识别的选择器发送给....
let headers: HTTPHeaders = [
"Username": "username",
"Password": "password"
]
let asset = AVURLAsset(url: URL(string: "http://abcd.com/sample.mp3")!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
let playerItem = AVPlayerItem(asset: asset)
audioplayer = AVPlayer(playerItem: playerItem)
audioplayer?.play()
[pinky谢谢你问我会回答的问题