2011-04-14 6 views
0

AVAudioPlayer에 할당 한 메모리를 해제하고 싶습니다.하지만 사운드를 재생하려고 시도했을 때 사운드가 재생되지 않습니다. 잠재적 인 메모리 누수가 있습니다. 어떻게하면 빠져 나올 수 있습니까? 이.아래의 코드에서 AVAudioPlayer를 해제하는 방법

잘못된 선택을 선택한 다음 오류 사운드를 재생할 때 아래의 코드가 사용됩니다. 여러 번 잘못된 선택을 선택하면 여러 번 할당되어 어떻게 제거 될 수 있습니다.

 else 
     { 
      NSString *[email protected]"Error.mp3"; 

      NSError *error; 
      NSURL *urlString = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%s", [[NSBundle mainBundle] resourcePath],[soundName UTF8String]]]; 

      AVAudioPlayer *worngAudioPlay = [[AVAudioPlayer alloc] initWithContentsOfURL:urlString error:&error];// here I also used to release using autorelease then not played sound 

      worngAudioPlay.delegate = self; 

      if (worngAudioPlay == nil) 
      { 
      } 
      else 
      { 
       [worngAudioPlay play]; 
      } 
      // [worngAudioPlay release]; // Here I released then not played sound 
     } 

감사합니다. Madan Mohan.

답변

0

avaudio 플레이어에서 audioDidFinishPlaying이라는 대리자 메서드를 해제 할 수 있습니다. 나는 그 이름을 정확하게 모른다. 하지만 내가 무슨 말을하고 있는지 알게 될 것입니다

+0

안녕하세요,하지만 멈췄을 때이 메서드는 호출되지 않습니다. –

+0

@Madan Mohan은 정확한 방법 이름을 기억하지 못합니다. avaudioplayer 클래스 문서 –

0

클래스의 유지 된 속성으로 만들고 dealloc에서 해제하십시오.

구현에
@property(retain)AVAudioPlayer *player; 

(하는 .m) 파일 : 당신의 dealloc에서

else 
{ 
    NSString *[email protected]"Error.mp3"; 

    NSError *error; 
    NSURL *urlString = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%s", [[NSBundle mainBundle] resourcePath],[soundName UTF8String]]]; 

    AVAudioPlayer *worngAudioPlay = [[AVAudioPlayer alloc] initWithContentsOfURL:urlString error:&error]; 

    if (worngAudioPlay != nil) 
    { 
     // this will cause the previous one to be released 
     [self setPlayer:worngAudioPlay]; 
     [worngAudioPlay release]; 
     worngAudioPlay.delegate = self; 
     [worngAudioPlay play]; 
    } 
} 

: 위의 코드에서

@synthesize player; 

헤더 파일에서

-(void)dealloc { 
    [player release]; 
    [super dealloc]; 
} 
+0

에는 setPlayer 메소드가 없습니다. 나는 지치고 있습니다 –

+0

위와 같이 헤더 파일에 플레이어 속성을 만들고 "@synthesize player;"를 추가하면 구현에 따라 setHeader 메소드가 암시 적입니다. –

관련 문제