2013-02-22 3 views
0

저는 AVAudioPlayer을 배우려고 노력했으며 그 동작이 중요합니다. 내가 AVAudioPlayer 비헤이비어 처리 문제

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; 
player.numberOfLoops = -1; 
[player play]; 

그것은 한 응용 프로그램이 배경을 입력하지 않은으로 잘 작동 않았다

을 작성했다과

그래서 시작합니다. 그래서 나는 다시 검색 나는 지금 내 샘플 응용 프로그램 재생뿐만 아니라 배경에서 소리
NSError *activationError = nil; 
    if([[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&activationError] == NO) 
     NSLog(@"*** %@ ***", activationError); 

    [[AVAudioSession sharedInstance] setActive: YES error: nil]; 
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

코드

다음 작성한 또한 PLIST에 항목을 추가해야하는 것을 발견했다. 그런 다음 다른 문제가 발생했습니다. 이 모든 처리 전에 수면을 취한 경우 수면 중에 백그라운드에서 응용 프로그램을 가져 와서 다른 응용 프로그램에서 음악 재생을 시작합니다.이 경우 응용 프로그램에서 소리를 재생할 수 없습니다. 다른 음악 응용 프로그램에서 사용한 오디오 세션을 얻지 못했기 때문일 수 있습니다.

질문 1 내가 다른 사운드가 재생되는 경우 감지 할 수있는 방법이 있다는 것을 이해하지만, 내가 그것을 중지 할 방법을 찾을 수 없습니다. iPod과 같은 표준 응용 프로그램 사운드를 중지하는 방법이 있지만 다른 응용 프로그램에서 소리를 중지하는 일반적인 방법이 있습니다. [player play];은 결과적으로 NO이됩니다. 어떤 실패가 발생했는지 어떻게 알 수 있습니까? audioPlayerDecodeErrorDidOccur:error: 메서드를 구현했지만 결코 호출되지 않습니다.

이 점에 대해 알고 싶습니다. 나는 이미 스택 오버플로에 관한 대부분의 질문을 거쳤지만이 특별한 문제에 유용하지 않은 것을 찾지 못했습니다.

+0

당신의 soundFileURL –

+0

@Manohar을 인쇄하십시오 : 사용 URL을 얻기 위해 코드를 다음 '는 NSString * soundFilePath = [[NSBundle mainBundle] pathForResource : @ ofType을 "소리": "MP3"@] NSURL * soundFileURL = [NSURL fileURLWithPath : soundFilePath]; ' 응용 프로그램이 백그라운드에서 실행될 때까지 계속 작동했습니다. 그래서 URL이 문제가되지 않는다고 생각합니다. – Sagrian

+0

NSURL * soundFileURL = [NSURL URLWithString : soundFilePath]; 시도해보십시오 –

답변

0
-(void)loadPlayer 
{ 
     NSURL *audioFileLocationURL = [NSURL URLWithString:[[NSBundle mainBundle] URLForResource:@"01-YedhiYedhi[www.AtoZmp3.in]" withExtension:@"mp3"]]; 
     NSError *error; 
     audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error]; 
     [audioPlayer setNumberOfLoops:-1]; 
     if (error) 
     { 
      NSLog(@"%@", [error localizedDescription]); 

      [[self volumeControl] setEnabled:NO]; 
      [[self playPauseButton] setEnabled:NO]; 

      [[self alertLabel] setText:@"Unable to load file"]; 
      [[self alertLabel] setHidden:NO]; 
     } 
     else 
     { 
      [[self alertLabel] setText:[NSString stringWithFormat:@"%@ has loaded", str]]; 
      [[self alertLabel] setHidden:NO]; 

      //Make sure the system follows our playback status 
      [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
      [[AVAudioSession sharedInstance] setActive: YES error: nil]; 

      //Load the audio into memory 
      [audioPlayer prepareToPlay]; 
     } 
    } 
    if (!self.audioPlayer.playing) { 
     [self playAudio]; 

    } else if (self.audioPlayer.playing) { 
     [self pauseAudio]; 

    } 
} 

- (void)playAudio { 
    [audioPlayer play]; 
} 
+0

alertLabel이란 무엇이며 왜 사용해야합니까? 나는 이것에 어떤 사용자 상호 작용도 원하지 않는다. 'audioPlayer'와'self '도 있습니다. audioPlayer는 다른가요? – Sagrian

+0

alertlabel –

+0

Manohar 코드가 불완전한 것 같습니다. 2 질문에 답할 수 없습니다. '[self playAudio]'구현도 제공되지 않습니다. 그것은 단지 [선수 플레이]입니다. – Sagrian