【发布时间】:2014-11-22 09:54:20
【问题描述】:
我想在 Swift 中从 Internet 流式传输音频,但还没有找到正确的功能示例。
在Objective-C
中AVPlayerItem* playerItem =[AVPlayerItem playerItemWithURL:[NSURL URLWithString:streamURL]];
[playerItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:nil];
music = [AVPlayer playerWithPlayerItem:playerItem];
[music play];
我在 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
我也尝试添加 playerItem.addObserver(self, forKeyPath: "timedMetadata", options: NSKeyValueObservingOptions.New, context: nil) 但出现错误
'类 AVPlayerItem 的实例 0x16edbd20 已被释放,而键值观察者仍向其注册。当前观察信息:( 上下文:0x0,属性:0x16ea5880> )'
关于如何解决这个问题的任何想法?
【问题讨论】:
-
你考虑过使用AVAudioPlayer吗?
-
你确定
playerItem在播放命令后还活着吗?快速尝试是在播放命令后添加sleep(10)。这将使线程保持活动状态十秒钟。如果幸运的话,可能会有一些声音。
标签: swift stream radio avplayer