是否可以使用观察者跟踪AVAudioPlayer对象? [英] Is It possible to track AVAudioPlayer object by using observer?

查看:156
本文介绍了是否可以使用观察者跟踪AVAudioPlayer对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I followed the documents about how to set up and observer using KVO mechanism http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html

这应该很容易。

我创建了一个AVAudioPlayer对象,我希望在每次更改后跟踪当前时间。

I created an AVAudioPlayer object and I want to track after every change in it's current time.

我使用此代码用于设置观察者:

I use this code to set up the observer:

[_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)对象更改:(NSDictionary *)更改上下文:(void *)context

For some reason the observer doesn't call to the -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

我知道我可以使用NSTImer并在音频开始播放时触发它但我正在寻找更顺畅这样做的方法。
我也可以使用AVPlayer对象并使用它来跟踪它的 addPeriodicTimeObserverForInterval:queue:usingBlock:
但我不想失去所有的AVAudioPlayer对象的优点。

I know that I can use NSTImer and trigger it when the audio starts playing but I'm looking for smoother way to do this. I also can use AVPlayer object instead and track it by using it's addPeriodicTimeObserverForInterval:queue:usingBlock: But I don't want to lose all the advantages of the AVAudioPlayer object.

我对观察者做错了什么?
您是否有另一个建议如何使用AVAudioPlayer并在其currentTime属性后管理跟踪?

What am I doing wrong with the observer? Do you have another suggestion how to use AVAudioPlayer and manage tracking after it's currentTime property?

提前致谢,

推荐答案

你没有做错任何事。

我也试过这样做而且它只是没有好了。

I've tried to do this as well and it just doesn't fire.

我不得不使用 NSTimer 代替调查当前时间:(

I had to use an NSTimer instead that polled the currentTime :(

这篇关于是否可以使用观察者跟踪AVAudioPlayer对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆