使用AVQueuePlayer循环播放单个playerItem

时间:2011-05-08 15:53:03

标签: iphone avqueueplayer

我使用下面的代码来循环使用playerItem

的单个AVQueuePlayer
-(void) init {
    _soundPlayer = [ [ AVQueuePlayer alloc ] init ];
    [ _soundPlayer addObserver: self forKeyPath: @"status" options: NSKeyValueObservingOptionNew context: NULL ];
    [ [ NSNotificationCenter defaultCenter ] addObserver: self selector: @selector ( playerItemDidReachEnd: ) name: AVPlayerItemDidPlayToEndTimeNotification object: nil ];
    [ [ NSNotificationCenter defaultCenter ] postNotificationName: AVPlayerItemDidPlayToEndTimeNotification object: nil ];
}

- ( void ) observeValueForKeyPath: ( NSString * ) keyPath ofObject: ( AVPlayer * ) object change: ( NSDictionary * ) change context: ( void * ) context {
if ( _soundPlayer.status == AVPlayerStatusReadyToPlay )
    [ _soundPlayer play ];
}

- ( void ) playerItemDidReachEnd: ( NSNotification * ) notification {
    AVPlayerItem *playerItem = [ [ AVPlayerItem alloc ] initWithURL: [ NSURL fileURLWithPath: _playerItemFilePath ] ];
    [ _soundPlayer insertItem: playerItem afterItem: nil ];
    [ playerItem release ];
}

它在模拟器上工作正常,即使仪器表明泄漏类似于AudioToolbox SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&),人们说这只是一些“框架泄漏”,它在设备上会很好,但是当我运行它时在设备上,仪器没有泄漏,但反而崩溃......我想念的是什么?谢谢,如果有人可以帮我这个。

有时,playerItem将是在线流文件,因此无法使用AVAudioPlayer


最后,我发现这是因为_playerItemFilePath。它已不存在,NSString方法NSBundle's返回pathForResource需要保留。但我仍然想知道为什么它可以在模拟器上运行。毕竟,关于audioToolbox的泄漏也是固定的,所以任何人遇到“框架泄漏”,有些人提到可能需要再次查看代码。它会因为一些小错误而出现。

0 个答案:

没有答案
相关问题