我希望你能够使用Swift从互联网流式传输音频,但目前还没有找到正确的功能性示例。
在Objective-C中,
我正在尝试使用Swift进行的操作。
我尝试添加 playerItem.addObserver(self, forKeyPath: "timedMetadata", options: NSKeyValueObservingOptions.New, context: nil),但是出现了错误。
'一个 AVPlayerItem 类的实例0x16edbd20在键值观察者仍然注册它时已经被释放。当前观察信息:( 上下文:0x0,属性:0x16ea5880> )'
有什么想法可以解决这个问题吗?
在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> )'
有什么想法可以解决这个问题吗?
playerItem
还活着吗?一个快速的尝试是在播放命令之后添加sleep(10)
。这将使线程保持活动状态十秒钟。如果你很幸运,可能会有一些声音。 - Mellson