1

コールバック内でそのプロパティの値を読み取って、どのデータベース レコードが再生されたかを正確に把握 できるように、作成する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...
}
4

2 に答える 2

4

何かをサブクラス化するときと同じように、サブクラスを作成してプロパティを追加できます。

インターフェース

@interface MyAudioPlayer : AVAudioPlayer

@property (nonatomic) int primaryKey;

@end

実装

@implementation MyAudioPlayer

@synthesize primaryKey = _primaryKey;

@end

作成

MyAudioPlayer *player = [[MyAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.primaryKey = thePrimaryKey;
...

デリゲート

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    if ([player isKindOfClass:[MyAudioPlayer class]]) {
        MyAudioPlayer *myPlayer = (MyAudioPlayer *)player;
        NSNumber *primaryKeyObject = [NSNumber numberWithInt:myPlayer.primaryKey];
        ...
    }
}
于 2012-06-08T23:00:22.397 に答える
1

簡単な方法は、NSMutableDictionary を作成し、作成した AVAudioPlayers を KEYS として使用し、主キー (または辞書全体) を対応する VALUE として使用することです。その後、プレーヤーが再生を停止 (またはエラー) したときに、辞書で調べて、好きなものを回復できます。

于 2012-06-08T22:58:02.697 に答える