我的代码在模拟器中可以正常运行,并且可以毫无问题地传输URL。但是,当插入实际设备时,它不会播放音频。我的代码如下
var playerItem: AVPlayerItem?
var player: AVPlayer?
func initPlayer() {
let url:URL = URL(string:"https://storage.googleapis.com/purelightdatabucket/samples%2F60-Energy%20UPLIFT.mp3")!
self.playerItem = AVPlayerItem(url: url)
self.player=AVPlayer(playerItem: self.playerItem!)
let playerLayer=AVPlayerLayer(player: self.player!)
playerLayer.frame = CGRect(x: 0, y: 0, width: 10, height: 50) // actually this player layer is not visible
self.view.layer.addSublayer(playerLayer)
}
最佳答案
将此代码放入 didFinishLaunchingWithOptions
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
关于ios - AvPlayer无法在设备中播放声音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47788568/