How to play mp3 audio from URL in ios swift

后端 未结 9 672
鱼传尺愫
鱼传尺愫 2020-11-30 04:50

I am getting mp3 url as a response of api calling. I want to play that audio, so how can i do that? (ios swift)

here is my response

 {
    content =          


        
9条回答
  •  隐瞒了意图╮
    2020-11-30 05:19

    After some research, and some disappointment since none of the answers solved my problem, I've come to a solution and here's the final result. The accepted answer is half right, you still need to create an AVPlayerLayer

    let url = URL(string: "https://www.myinstants.com/media/sounds/cade-o-respeito.mp3" ?? "")
    playerItem = AVPlayerItem(url: url!)
    player = AVPlayer(playerItem: playerItem)
    
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
    self.contentView.layoutSublayers(of: playerLayer)
    
    player?.play()
    

提交回复
热议问题