我想在Swift中從Internet流式傳輸音頻,但尚未找到正確的功能示例。Swift Radio Streaming AVPlayer
在Objective-C的
AVPlayerItem* playerItem =[AVPlayerItem playerItemWithURL:[NSURL URLWithString:streamURL]];
[playerItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:nil];
music = [AVPlayer playerWithPlayerItem:playerItem];
[music play];
什麼我試着在斯威夫特
let playerItem = AVPlayerItem(URL:NSURL(string:url))
var player:AVPlayer!
player = AVPlayer(playerItem:playerItem)
player.rate = 1.0;
player.play()
//this is not working
我也嘗試添加playerItem.addObserver(個體經營,forKeyPath: 「timedMetadata」,選項:NSKeyValueObservingOptions .New,context:nil)但出錯
'實例0x16edb AVPlayerItem類的d20被釋放,而鍵值觀察者仍然在其中註冊。當前的觀察信息:( 上下文:0x0,屬性:0x16ea5880> )'
有關如何解決此問題的任何想法?
您是否考慮使用AVAudioPlayer? – 2015-02-03 12:57:37
播放命令後你確定'playerItem'是否還活着嗎?快速嘗試的是在播放命令之後添加一個「sleep(10)」。這將使線程保持十秒鐘。如果你幸運,那麼可能會有一些聲音。 – Mellson 2015-02-09 19:30:11