How to stream audio from URL without downloading mp3 file on device

后端 未结 5 573
挽巷
挽巷 2021-02-09 21:18

How would I stream audio from a URL in Swift without downloading the mp3 file on the device? What do I need to import? Do I need certain libraries? Add anything to the info.plis

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-09 22:05

    You can use iOS AVPLayer for Streaming audio from url.

    var player: AVPlayer!
    let url  = URL.init(string:   "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.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()
    

提交回复
热议问题