2011-10-28 6 views
0

시작시에만 음악을 어떻게 재생합니까? 나는 시작시에만 음악을 연주하고 싶다. 지금은 viewDidLoad 메서드에 있습니다. 그 페이지로 돌아올 때마다 소리가 재생됩니다. 한 번만 재생할 수 있습니까?시작시 음악을 어떻게 재생합니까?

알았습니다. 당신은 applicationDidBecomeActive 문에 넣어 :

코드 :

-(void)applicationDidBecomeActive:(UIApplication *)application { 
[player setVolume:1]; 
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/PBLTune.mp3", [[NSBundle mainBundle] resourcePath]]]; 

NSError *error; 

player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
player.numberOfLoops = 0; 

if (player == nil) 
    NSLog([error description]); 
else 
    [player setVolume:0.1]; 

[player play]; 

}

답변

1

음악을 재생하는 코드를 -applicationDidFinishLaunching으로 이동하십시오.

코드를 보지 않고도 한 번만 재생할 수있는 방법을 제안 할 수 없습니다.

+0

한 번 재생하려면'numberOfLoops'를 1로 설정하십시오. –

0

는 아래 applicationDidFinishLaunching 방법에 코드를 삽입합니다.

+0

이 앱 위임 파일에 있습니까? 예, 그렇습니다. – iOSprogrammer

관련 문제