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];
}
}
오! 예, 전적으로 applicationDidFinishLaunching이 아니라 audioPlayerDidFinishPlaying입니다. 죄송합니다. 대리자를 self로 설정하고 첫 번째 사운드에 효과가있었습니다. 하지만 문제는 audioPlayerDidFinishPlaying이 작동하지 않는다는 것입니다. 문제는 두 번째 사운드를 다시 재생하는 방법을 모르는 것입니다. 두 가지 소리가 난다. – Evelyn
그건 그렇고, [player setDelegate : self]를 넣을 때, 경고 메시지가 나타납니다. "class 'ViewController'는 'AVAudioPlayerDelegate'프로토콜을 구현하지 않습니다." 그게 문제 야? – Evelyn
자신을 두 사운드 '위임자로 설정해야합니다. 사운드는 위임 메시지를 보낼 때 자신을 식별하므로 사용자가 가지고있는 소리와 비교할 수 있습니다. –