2012-10-12 3 views
0

앱을 실행하면 시작 클립이 재생됩니다. 다음 코드는 appDelegate.m 내의 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions에 있으며 단지 훌륭하게 작동합니다.iPhone :보기 변경시 사운드 클립 시작이 중지되지 않습니다.

NSLog(@"PLAY SOUND CLIP WHILE LOADING APP"); 
NSURL *clip = [[NSBundle mainBundle] URLForResource: @"intro" withExtension:@"caf"]; 
self.startupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL]; 
[self.startupPlayer play]; 

소개 사운드가 끝나기 전에 사용자가보기를 변경하면 계속 재생됩니다. 나는이 코드를 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 외부에 놓았지만 도움이 될 것이라고 생각했다. 내가 바로 트릭을 할 수있는 부하 요청 후 if 문을 넣어 경우

-(void)viewDidDisappear:(BOOL)animated { 
[super viewDidDisappear:animated]; 
// Stop Sound 
[self.startupPlayer stop]; 
} 

나는 어쩌면 생각하지만 일을하지 않았다. 예제보기 :

NSLog(@"PLAY SOUND CLIP WHILE LOADING APP"); 
NSURL *clip = [[NSBundle mainBundle] URLForResource: @"intro" withExtension:@"caf"]; 
self.startupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL]; 
[self.startupPlayer play]; 

if ([viewDidDisappear == YES]) { 
[self.startupPlayer stop]; 
} 

사용자가보기를 변경하면 해당 클립이 재생되기 전에 소개 사운드를 중단시키는 제안 사항은 훌륭합니다. 아, 그리고 난 "viewWillDisappear"성공의 응용 프로그램의 다른 부분에 사용했지만,이 경우에는 나중에 나도 작동하지 않았기 때문에 "viewDidDisappear"를 선택합니다. 그래서 나는 혼란 스럽다. 미리 감사드립니다.

EDIT : 그래서 viewWillDisappear를 MainViewController로 옮기고 델리게이트를 호출했지만 여전히 행운이 없습니다. 다시 한 번 도움을 주시면 감사하겠습니다.

답변

0

나는이 문제를 * startupPlayer에 대한 @property 선언을 취하여 아래의 방법 대신 AppDelegate.h 파일에 배치하여이 문제를 해결했습니다.

다음
@implementation AppDelegate 
@synthesize startupPlayer = _startupPlayer; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ 

//------ PLAY SOUND CLIP WHILE LOADING APP ----- 

NSLog(@"PLAY SOUND CLIP WHILE LOADING APP"); 
NSURL *clip = [[NSBundle mainBundle] URLForResource: @"intro" withExtension:@"caf"]; 
self.startupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL]; 
[self.startupPlayer play]; } 

내 MainViewController.m에서 다음 AppDelegate.m

@interface AppDelegate() 

@property (nonatomic, strong) AVAudioPlayer *startupPlayer; 
@end 
다음

난 여전히 아래 그림과 같이하는 .m 파일을 @synthesized과 동일 didFinishLaunchingWithOptions를 유지에서

#import "AppDelegate.h" 

-(void)viewWillDisappear:(BOOL)animated 
{ 
[super viewWillDisappear:animated]; 
//stop intro sound 
AppDelegate *introClip = (AppDelegate *)[[UIApplication sharedApplication]delegate]; 
[[introClip startupPlayer]stop];} 

이제 인트로 음악이 한 사이클을 통해 계속 재생되는 경우에도 사용자는 다른보기로 전환하고 해당 음악을 중지 할 수 있습니다.

+0

오디오 재생이 끝난 후에 어떻게 반복합니까? @meltrek –

+0

_startupPlayer.numberOfLoops = -1; // 음수는 무기한 재생을 의미합니다. – saintjab