2012-03-28 3 views
1

가능한 중복 :
iOS SDK : playing music on the background and switching viewsiOS의 배경 음악 문제

안녕 얘들은 현재 전에보기를 전환 할 때 그 나누었다을 출시 할 때 내 응용 프로그램에서 배경 음악을 재생하려면이 코드를 사용하여 메신저 내 SecondView 및 첫 번째보기로 되돌아갑니다. 음악이 시작되어 현재 재생중인 음악과 겹치며 음악을 겹쳐서 저장합니다. Firstview로 돌아 가면 SecondView에서 내 Firstview로 다시 전환 할 때 음악을 다시 시작할 수 있습니까? .

당신은 재 할당하고 AVAudioPlayer 당신이보기를 표시 할 때마다 다시하는

- (void)viewDidLoad { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"]; 
    theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL]; 
    theAudio.delegate = self; 
    theAudio.numberOfLoops = -1; 
    [theAudio play]; 
+0

'theAudio'란 무엇입니까? –

답변

0

FirstViewController.m. 가능하다면 이것을 피해야합니다. 문제에 대한 해결책으로 코드에 따라 [theAudio stop]viewDidDisappear 또는 viewDidUnload 이벤트로 호출하십시오. 그러면 오디오 플레이어가 배경으로 들어갈 때 오디오 플레이어가 멈 춥니 다.

+0

예, viewDidDisappear 또는 viewWillDisappear에서 [theAudio stop]을 호출 할 수 있습니다. 하지만 viewDidUnload에서는 메모리 경고가 발생했을 때만 호출되므로 ... –

+0

글쎄, 모든 뷰에서 재생할 배경 음악이 필요하고 내 홈 버튼을 내 첫보기에 맞춰 다시 시작할 때 음악이 필요합니다. 나는 ViewDidDisappear에서 [TheAudio stop]을 시도했지만 뷰를 가로 질러 전환 할 때 음악을 멈추고 그 옵션이 필요 없다. –

1

두 가지 옵션, 첫 번째보기가 사라지는 동안 플레이어를 중지하는 두 가지 옵션, 두 가지 옵션, 두 가지 옵션, 플레이어를 중지합니다.

[theAudio stop] in `viewWillDisappear` 

2 명이 다시오고 싶지 않다면. 은 appDelegate 클래스에 theAudio을 선언합니다. 할당되기 전에 할당되었는지 여부를 확인합니다.

if(!appDelegate.theAudio) 
{ 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"]; 
    appDelegate.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL]; 
    theAudio.delegate = self; 
    theAudio.numberOfLoops = -1; 
    [theAudio play]; 
} 
else 
{ 

[theAudiostop]; 
    [theAudio play]; 
} 
+0

음 배경 음악을 모든보기에서 재생해야하고 음악을 재생해야 다시 시작할 수 있습니다. 내 첫 단추를 누르면 집으로 돌아갑니다. 현재 [theAudio prepareToPlay]를 추가했습니다. ~ (void) viewWillDisappear : (BOOL) animated {..홈 버튼을 내 FirstView로 다시 돌리면 음악이 계속 재생되지만 잘 작동하지만 배경 음악을 다시 시작하여 내 FirstView에 홈 버튼을 다시 놓고 싶습니다. –

3

NSUserDefaults를 사용하여 음악 재생 상태를 저장할 수 있으므로보기가로드 될 때 음악이 재생되지 않습니다.

if ([[NSUserDefaults standardUserDefaults] stringForKey:@"music"] == nil) { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"]; 
    theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL]; 
    theAudio.delegate = self; 
    theAudio.numberOfLoops = -1; 
    [theAudio play]; 
    [[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; } 

앱을 종료 할 때이 키를 다시 초기화해야합니다. 그렇지 않으면 플레이어가 앱을 종료하고 다음에 앱을 다시 시작하면 @ "음악"문자열이 0이되지 않습니다. projectAppDelegate.m 파일

이동 : 음악이 응용 프로그램을 통해 재생되는

- (void)applicationDidEnterBackground:(UIApplication *)application { 
     [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"music"]; } 

경우, 안전을 위해 다음과 같이 코드를 연주 음악을 넣어. projectAppDelegate.h 파일에 AVAudioPlayer * theAudio를 선언하는 것을 잊지 마십시오.

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"]; 
    theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL]; 
    theAudio.delegate = self; 
    theAudio.numberOfLoops = -1; 
    [theAudio play]; 
    [[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; } 

여기에 약간의 비트가 누락되었을 수 있습니다. 몇 가지 테스트를 실행하여 원하는 방식으로 테스트하십시오. 방금 내 애플 리케이션을 시도하고 그것은 나를 위해 일했다.