Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否可以使用observer跟踪AVAudioPlayer对象?_Ios_Ios4_Ios5 - Fatal编程技术网

Ios 是否可以使用observer跟踪AVAudioPlayer对象?

Ios 是否可以使用observer跟踪AVAudioPlayer对象?,ios,ios4,ios5,Ios,Ios4,Ios5,我遵循了有关如何使用KVO机制设置和监视程序的文档 这应该很容易 我创建了一个AVAudioPlayer对象,我想跟踪它当前时间的每次更改 我使用此代码设置观察者: [_player addObserver:self forKeyPath:@"currentTime" options:NSKeyValueObservingOptionNew context:NULL]; 此代码用于处理更改: -(void)observeValueForKeyPath:(NSString *)keyPath

我遵循了有关如何使用KVO机制设置和监视程序的文档

这应该很容易

我创建了一个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*)键路径:(id)对象更改:(NSDictionary*)更改上下文:(void*)上下文

我知道我可以使用NSTImer并在音频开始播放时触发它,但我正在寻找更平滑的方法来实现这一点。 我也可以使用AVPlayer对象,通过使用它的
addPeriodicTimeObserverForInterval:queue:usingBlock:
但我不想失去AVAudioPlayer对象的所有优点

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


提前谢谢,你没有做错什么

我也试过这么做,但它就是不点火

我不得不用一个
NSTimer
来代替当前时间的轮询:(

你试过了吗

//KVO[self setValue:[NSNumber numberWithFloat:currentTime]forKey:@“currentTime”]


我在更改currentTime时这样做了,KVO工作正常。但是它仍然需要一个计时器来告诉设置值=。=

KVO将仅在调用方直接更改值时才会为AVAudioPlayer上的currentTime属性触发。它不会在音频剪辑进行时触发。要跟踪这一点,您必须使用NSTimer,正如已经建议的那样由deanWombourne指点。

谢谢您的回复。最终,我将我的播放器替换为AVPlayer,并使用它的addPeriodicTimeObserverForInterval:queue:usingBlock:methode在当前时间后进行跟踪。嗯,我可能必须在我的应用程序中执行同样的操作;这是一个比我的计时器破解更好的解决方案:)谢谢提示!我使用了此解决方案,但仅使用
CADisplayLink
而不是
NSTimer
,以实现更一致的刷新率。