1

私はObjective Cの初心者です。uibuttonのタッチイベントでサウンドを再生するアプリを開発しています。ボタンをタッチすると画像が変わり音声が再生されます。音声の再生が終わったら、uibutton の元画像を入れたいです。どうやってやるの?終了したオーディオのイベントをキャッチするにはどうすればよいですか? 私はそのコードを使用しています:

- (SystemSoundID) createSoundID: (NSString*)name
{
    NSString *path = [NSString stringWithFormat: @"%@/%@",
    [[NSBundle mainBundle] resourcePath], name];              
    NSURL* filePath = [NSURL fileURLWithPath: path isDirectory: NO];
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
    return soundID;
} 

viewDidLoad

電話する

freq = [self createSoundID: @"freq.wav"];

ボタンのタッチで呼び出される関数では、次を使用します。

AudioServicesPlaySystemSound(freq);

どのようにできるのか?ありがとうございました!

4

2 に答える 2

2

AudioService API で可能かどうかはわかりませんが、代わりに使用して、プロトコルを実装し、呼び出されたときに何かをAVAudioPlayer行うデリゲートを割り当てることができます。AVAudioPlayerDelegateaudioPlayerDidFinishPlaying:successfully:

フレームワークを追加することを忘れないでくださいAVFoundation

例:

インターフェイス定義で:

#import <AVFoundation/AVFoundation.h>

...

@inteface SomeViewController : UIViewController <AVAudioPlayerDelegate>

...

@propety(nonatomic, retain) AVAudioPlayer *player;

あなたの実装では:

@synthesize player;

...

// in some viewDidLoad or init method
self.player = [[[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:NULL] autorelease];
self.player.delegate = self;

...

- (void)pressButton:(id)sender {
  // set play image
  [self.player stop];
  self.player.currentTime = 0;
  [self.player play];
}

...

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
   // set finish image
}

...

# in some dealloc or viewDidUnload method
self.player = nil;
于 2011-09-28T11:23:04.630 に答える