2009-07-10 6 views
16

AVAudioPlayer 프레임 워크를 사용하고 있으며 한 번에 하나씩 재생되는 여러 사운드가 있습니다. 사운드 재생이 끝나면 응용 프로그램이 무언가를하기를 원합니다. audioPlayerDidFinishPlaying을 사용하여 첫 번째 사운드의 끝에서 작업을 시도했지만 재정의 오류가 발생하여 두 번째 사운드에는 사용할 수 없습니다. 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를 사용하려고

...

뇌 방귀? 그 방법은 내가 소리를내는 것과 관련이있다.

...하지만 재 정의 오류가 발생하여 두 번째 사운드에는 사용할 수 없습니다.

허? 메서드 본문에서 플레이어 개체를 비교하는 대신 메서드를 두 번 또는 무언가로 구현 했습니까?

정확한 오류 메시지가 표시되면 도움이됩니다.

+0

오! 예, 전적으로 applicationDidFinishLaunching이 아니라 audioPlayerDidFinishPlaying입니다. 죄송합니다. 대리자를 self로 설정하고 첫 번째 사운드에 효과가있었습니다. 하지만 문제는 audioPlayerDidFinishPlaying이 작동하지 않는다는 것입니다. 문제는 두 번째 사운드를 다시 재생하는 방법을 모르는 것입니다. 두 가지 소리가 난다. – Evelyn

+0

그건 그렇고, [player setDelegate : self]를 넣을 때, 경고 메시지가 나타납니다. "class 'ViewController'는 'AVAudioPlayerDelegate'프로토콜을 구현하지 않습니다." 그게 문제 야? – Evelyn

+0

자신을 두 사운드 '위임자로 설정해야합니다. 사운드는 위임 메시지를 보낼 때 자신을 식별하므로 사용자가 가지고있는 소리와 비교할 수 있습니다. –

1

스위프트 3 :

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { 
     print("sound file finished") 
    } 
관련 문제