2011-03-31 22 views
2

我使用AVAudioPlayer播放MP3文件。當玩家正在運行時,我想用播放器的當前時間來控制幻燈片。當然,我可以用一個NSTimer來完成這個任務,這個NSTimer在比賽開始的時候會被解僱,但這聽起來不像一個甜蜜的解決方案。我如何向AVAudioPlayer的當前時間值添加觀察者?使用AVAudioPlayer當前時間控制幻燈片

感謝, 菲利普

回答

0

做到這一點,最徹底的方法是建立自己使用AVPlayer一個音頻播放器和設置使用-[AVPlayer addBoundaryTimeObserverForTimes:queue:usingBlock:]時間過去了觀察。


如果你堅持用AVAudioPlayer我相信你可以使用Key-Value Observing來監聽currentTime的變化:

​​

,然後實現

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
    if (context == &someObservationContext) { 
     NSTimeInterval t = [(AVAudioPlayer *)object currentTime]; 
     // do your thing 
    } else { 
     [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; 
    } 

一定要打電話-[NSObject removeObserver:forKeyPath:]在拆除AVAudioPlayer之前刪除觀察掛鉤。

+0

我試圖用AVAudioPlayer和觀察器做到這一點,不幸的是,observeValueForKeyPath不會被調用。我需要任何框架或初始化? – plaetzchen 2011-03-31 17:20:18

+0

我在那裏猜測;我可能是錯的,它可能不是一個可觀察的屬性。 – 2011-03-31 23:10:02

+0

嗯。有沒有簡單的方法來使用AVPlayer?可以使用自定義UI嗎? – plaetzchen 2011-03-31 23:37:33

相關問題