AVAudioPlayer play from url swift code example

Example 1: swiftui play mp3

import AVFoundation

var bombSoundEffect: AVAudioPlayer?
let path = Bundle.main.path(forResource: "example.mp3", ofType:nil)!
let url = URL(fileURLWithPath: path)

do {
    bombSoundEffect = try AVAudioPlayer(contentsOf: url)
    bombSoundEffect?.play()
} catch {
    // couldn't load file :(
}

Example 2: swift play audio stream from url

func loadRadio(radioURL: String) {

        guard let url = URL.init(string: radioURL) else { return }
        let playerItem = AVPlayerItem.init(url: url)
        player = AVPlayer.init(playerItem: playerItem)
        player?.play()
        startNowPlayingAnimation(true)
        played = true
    }

Example 3: swift play video from url

import AVKit
import AVFoundation

class VideoController: UIViewController

override func viewDidLoad(){
  	guard let videoURL = URL(string: "VideoURL") else {
      	return
    }
    let player = AVPlayer(url: videoURL)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    self.present(playerViewController, animated: true) {
      playerViewController.player?.play()
    }


}