是否可以使用观察者来跟踪 AVAudioPlayer 对象?

2024-02-22

我遵循了有关如何使用 KVO 机制设置和观察者的文档http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html

这应该是很容易的。

我创建了一个 AVAudioPlayer 对象,我想跟踪当前时间的每次更改。

我使用以下代码来设置观察者:

[_player addObserver:self forKeyPath:@"currentTime" options:NSKeyValueObservingOptionNew context:NULL];

此代码用于处理更改:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"currentTime"]) {
    //Do something
}}

音频结束时的代码:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
// Remove the observation
[_player removeObserver:self forKeyPath:@"currentTime"];}

由于某种原因,观察者没有调用-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

我知道我可以使用 NSTImer 并在音频开始播放时触发它,但我正在寻找更流畅的方法来执行此操作。 我也可以使用 AVPlayer 对象来代替并通过使用它来跟踪它addPeriodicTimeObserverForInterval:queue:usingBlock:但我不想失去 AVAudioPlayer 对象的所有优点。

我对观察者做错了什么? 您还有其他建议如何使用 AVAudioPlayer 并在其 currentTime 属性之后管理跟踪吗?

提前致谢,


你没有做错什么。

我也尝试过这样做,但就是不火。

我不得不使用NSTimer相反,轮询当前时间:(

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否可以使用观察者来跟踪 AVAudioPlayer 对象? 的相关文章

随机推荐