2009-07-10 15 views
16

私はAVAudioPlayerフレームワークを使用していますが、私は一度に1つずつ再生するいくつかのサウンドを持っています。サウンドの再生が終了すると、アプリケーションは何かをしたい。私はaudioPlayerDidFinishPlayingを使って最初のサウンドの最後にアクションを実行しようとしましたが、再定義エラーが発生したため、2番目のサウンドには使用できませんでした。 NSTimeIntervalを使用してサウンドの持続時間を取得できますか?サウンドの再生が終了するとAVAudioPlayerでサウンドの再生が終了したときにアクションを実行しますか?

// ViewController.h 
#import <UIKit/UIKit.h> 
#import <AVFoundation/AVFoundation.h> 
#import <AVFoundation/AVAudioPlayer.h> 

@interface ViewController : UIViewController { 
UIButton  *begin; 
UIWindow  *firstTestWindow; 
UIWindow  *secondTestWindow; 
AVAudioPlayer *player; 
NSTimeInterval duration; 
} 

@property (nonatomic, retain) IBOutlet UIButton  *begin; 
@property (nonatomic, retain) IBOutlet UIWindow  *firstTestWindow; 
@property (nonatomic, retain) IBOutlet UIWindow  *secondTestWindow; 
@property (nonatomic, retain)   AVAudioPlayer *player; 
@property (readonly)     NSTimeInterval duration; 


- (IBAction)begin:(id)sender; 
- (IBAction)doTapScreen:(id)sender; 

@end 



//ViewController.m 
#import "ViewController.h" 

@implementation ViewController 

@synthesize begin, player, firstTestWindow, secondTestWindow; 

//first sound 
- (IBAction)begin:(id)sender; { 

    [firstTestWindow makeKeyAndVisible]; 
    NSString *soundFilePath = 
    [[NSBundle mainBundle] pathForResource: @"Tone1" 
            ofType: @"mp3"]; 

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 

    AVAudioPlayer *newPlayer = 
    [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL 
              error: nil]; 
    [fileURL release]; 
    self.player = newPlayer; 
    [newPlayer release]; 

    [player prepareToPlay]; 
    [self.player play]; 

} 

//If user does not do anything by the end of the sound go to secondWindow 
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player 
        successfully: (BOOL) flag { 
           if (flag==YES) { 
    [secondWindow makeKeyAndVisible]; 
    } 
} 

//second sound 
- (IBAction)doTapScreen:(id)sender { 
    //When user touches the screen, either play the sound or go to the next window 
    if (self.player.playing) { 
    [self.player stop]; 
    [thirdWindow makeKeyAndVisible]; 
    } 

    else { 
    NSString *soundFilePath = 
    [[NSBundle mainBundle] pathForResource: @"Tone2" 
       ofType: @"mp3"]; 

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 

    AVAudioPlayer *newPlayer = 
    [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL 
              error: nil]; 
    [fileURL release]; 
    self.player = newPlayer; 
    [newPlayer release]; 

    [player prepareToPlay]; 
    [self.player play]; 

} 
} 

答えて

24

、私は、アプリケーションが何かをしたいです。

自分自身を代理人として設定し、audioPlayerDidFinishPlaying:successfully:に返信してください。

あなたが示したコードスニペットでは、手順2は完了しましたが、手順1は完了していません。デリゲートとして自分自身を設定していません。私は最初の音の最後にアクションを行うためにapplicationDidFinishLaunchingを使用しようとした

...

脳おなら?その方法は、私が音を出すこととは何の関係もありません。

...私は再定義エラーがあるので、2番目のサウンドには使用できませんでした。

hh?メソッド本体のプレーヤーオブジェクトを単に比較するのではなく、メソッドを2回実装するか、何かを実装しましたか?

正確なエラーメッセージが表示された場合は、役立ちます。

+0

ああ!ええ、私は、applicationDidFinishLaunchingではなく、audioPlayerDidFinishPlayingと言っていました。ごめんなさい。 私はデリゲートをselfに設定しました。それは最初のサウンドで機能しました。しかし、問題は、audioPlayerDidFinishPlayingが動作しないということではなく、問題はもう2番目のサウンドのためにそれをやり直す方法を知らないことです。私は2つの異なる音を持っています。 – Evelyn

+0

ちなみに、[player setDelegate:self]を入れると、警告が表示されます。「class 'ViewController'は 'AVAudioPlayerDelegate'プロトコルを実装していません。問題ありますか? – Evelyn

+0

自分自身を両方のサウンドの代理人として設定する必要があります。デリゲートメッセージを送信すると、そのサウンド自体が識別されるため、手元にあるサウンドと比較することができます。 –

1

スウィフト3:

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { 
     print("sound file finished") 
    } 
関連する問題