Friday, February 22, 2019

Swift play music online

We want to play music online, declare a variable to above viewDidLoad.
var player : AVPlayer!
Copy in to viewDidLoad.
let url =  "https://s3.amazonaws.com/kargopolov/kukushka.mp3"
let songUrl = NSURL(string: url)
var playerItem:AVPlayerItem?
do {

   playerItem = AVPlayerItem(URL: songUrl!)
player=AVPlayer(playerItem: playerItem!)
let playerLayer=AVPlayerLayer(player: player)
  playerLayer.frame=CGRectMake(0, 0, 300, 50)
self.view.layer.addSublayer(playerLayer)

player.play()

    } catch (let error asNSError) {
// print("audioPlayer error: \(error)")

    }

We use AVPlayer, not AVAudioPlayer
Add these lines in to file info.plist on left tree folder.
<key>NSAppTransportSecurity</key>
     <dict>
     <key>NSAllowsArbitraryLoads</key>
     <true/>
     </dict>
Right click mouse to open source and copy to.



No comments:

Post a Comment