I want to stream an audio from the Internet in Swift but haven't found a correct functional example just yet.
In Objective-C
AVPlayerItem* playerItem =[AVPlayerItem playerItemWithURL:[NSURL URLWithString:streamURL]];
[playerItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:nil];
music = [AVPlayer playerWithPlayerItem:playerItem];
[music play];
What Im trying in Swift
let playerItem = AVPlayerItem(URL:NSURL(string:url))
var player:AVPlayer!
player = AVPlayer(playerItem:playerItem)
player.rate = 1.0;
player.play()
//this is not working
I also tried adding playerItem.addObserver(self, forKeyPath: "timedMetadata", options: NSKeyValueObservingOptions.New, context: nil) but I got an error
'An instance 0x16edbd20 of class AVPlayerItem was deallocated while key value observers were still registered with it. Current observation info: ( Context: 0x0, Property: 0x16ea5880> )'
Any ideas on how to solve this?
playerItem
is alive after the play command? Quick thing to try is to add asleep(10)
after the play command. This will keep the thread alive for ten seconds. If you get lucky there might be some sound then. – Templet