コールバック内でそのプロパティの値を読み取って、どのデータベース レコードが再生されたかを正確に把握 できるように、作成するprimaryKey
すべてのAVAudioPlayerオブジェクトに含めるために呼び出される新しい NSNumber または整数プロパティを作成する必要があります。audioPlayerDidFinishPlaying
これを行う必要がある理由は、プレイリスト内で同じサウンド ファイルを複数回使用できるため、プレーヤーのURL プロパティを使用して、それがどのデータベース レコードであったかを判断できないからです。
このような既存の 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...
}