【发布时间】:2012-06-08 22:44:33
【问题描述】:
我需要创建一个名为 primaryKey 的新 NSNumber 或整数属性,以包含在我创建的所有 AVAudioPlayer 对象中,以便我可以在 audioPlayerDidFinishPlaying 回调中读取该属性的值并确切知道哪个数据库播放记录。
我需要这样做的原因是:我无法使用播放器URL property 来确定它是哪个数据库记录,因为同一声音文件可以在播放列表中多次使用。
我怎样才能像这样向现有的 iOS 类添加新属性?
例子:
AVAudioPlayer *newAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
self.theAudio = newAudio; // automatically retain audio and dealloc old file if new file is loaded
if (theAudio != nil) [audioPlayers addObject:theAudio];
[newAudio release];
[theAudio setDelegate: theDelegate];
[theAudio setNumberOfLoops: 0];
[theAudio setVolume: callVolume];
// This is the new property that I want to add
[theAudio setPrimaryKey: thePrimaryKey];
[theAudio play];
然后我会像这样在回调中检索它:
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSNumber *finishedSound = [NSNumber numberWithInt:[player primaryKey]];
// Do something with this information now...
}
【问题讨论】:
标签: iphone objective-c ios avaudioplayer subclassing