2012-05-01 3 views
0

배경 음악

#import "AVFoundation/AVFoundation.h" 

그래서 내가, 내 클래스의 init 메소드에서이 코드를 처음 문제가 시도되었다 (AVFoundation.framework 사용)

NSString* soundFilePath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]; 
NSURL* soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 
AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; 
player.numberOfLoops=-1; 
[player play]; 
(즉 처음로드 하나입니다)

하지만 스레드를하기로 결정보다 그것은 작동하지 않았다 : 초기화 방법 :

[NSThread detachNewThreadSelector:@selector(playMusic) toTarget:self withObject:nil]; 

및 방법 itse lf :

-(void) playMusic { 
    NSString* soundFilePath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]; 
    NSURL* soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 
    AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; 
    player.numberOfLoops=-1; 
    [player play]; 
} 

물론 * .h 파일에 선언되어 있습니다. 그것은 또한 작동하지 않았다.

는 I 파일이 2 또는 3 초 재생 한 후 정지된다 playMusic 방법
} 

라인

에서 디버그했습니다. 디버그 없이는 재생되지 않습니다. 문제가 무엇입니까?

+0

AVAudioPlayer를 012_근무한 – Felix

+0

mg의 인스턴스 변수로 설정합니다. 감사합니다.)) – Alexander

답변

2

AVAudioPlayer는 즉시 할당 해제됩니다. 플레이어를 유지해야합니다. AVAudioPlayer를 클래스의 인스턴스 변수로 만듭니다.