How to play mp3 audio from URL in ios swift

后端 未结 9 667
鱼传尺愫
鱼传尺愫 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条回答
  •  萌比男神i
    2020-11-30 05:32

    var player : AVPlayer?
    func playSound()
        {
          guard  let url = URL(string: "https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3")
         else 
            {
              print("error to get the mp3 file")
              return
            }
         do{
             try AVAudioSession.sharedInstance().setCategory(.playback, mode.default)
             try AVAudioSession.sharedInstance().setActive(true)
             player = try AVPlayer(url: url as URL)
             guard let player = player
                   else 
                       { 
                         return 
                       }
             player.play()
          } catch let error {
                print(error.localizedDescription)
                   }
       }
    

提交回复
热议问题