2013-03-15 5 views
0

자습서를 읽고 다른 게시물을보고 있지만 앱 시작과 동시에 오디오가 자동으로 재생되기 시작한 것 같습니다.ios 앱을 시작할 때 어떻게 오디오를 재생합니까?

wav 파일을 백그라운드에서 반복 재생하고 반복하고 싶습니다. 나는 컨트롤을 시작하고 멈추고 싶지 않다 ...

다음은 ViewController.m 파일에있는 코드이다. (이 위치는 맞습니까?) AVFoundation.frameowrk를 추가하고이를 내 컴퓨터로 가져 왔습니다. m 파일. 또한 내 지원 파일에 AlienRoom이라는 wav 파일을 추가했습니다. 어떤 도움이 굉장 할 것입니다! 나는 새로운 것이므로 한 단계 한 걸음 나아가거나 가능한 한 명확하게 밝혀주십시오. 고맙습니다!!!

p.s. 루프를 만들기위한 코드를 추가하지 않았으므로 도움이 될 것입니다. 애플 리케이션은 잠에서 깨어 동안

//play background sound upon opening app 

-(void) awakeFromNib 

{ 

NSString *path = [[NSBundle mainBundle] pathForResource: @"AlienRoom" ofType: @"wav"]; 
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
[theAudio play]; 
} 
+0

u는 소리를 재생하려면에 AVAudioPlayerDelegate을 구현? 아니면 그냥 첫 번째 응용 프로그램로드? –

+0

기본보기가 나타날 때마다 배경 오디오를 계속 재생하고 싶습니다. 그래서 처음 열거 나이 견해로 돌아갑니다. – Herbie999

답변

0

ViewController.h

@interface ViewController : UIViewController <AVAudioPlayerDelegate> 

@property(nonatomic,strong)AVAudioPlayer *theAudio; 

ViewController.m 파일

-(void)viewWillAppear:(BOOL)animated 
{ 
    NSString *path = [[NSBundle mainBundle] pathForResource: @"AlienRoom" ofType: @"wav"]; 
    if(!self.theAudio) 
     self.theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    self.theAudio.delegate = self; 
    [self.theAudio play]; 
} 

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)data successfully:(BOOL)flag{ 

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

} 

- (void)playAudioAgain{ 

    [self.theAudio play]; 
} 

-(void)viewWillDisappear:(BOOL)animated 
{ 
    if(self.theAudio.isPlaying) 
    { 
     self.theAudio.delegate = nil; 
     [self.theAudio stop]; 
    } 
} 
+0

도움을 주셔서 감사합니다! 이 코드를 내 ViewController.h 파일에 저장 하시겠습니까? 나는 그것을 거기에서 그리고 .m에서 시도했지만 작동하지 않는 것 같다. – Herbie999

+0

로드 된 ViewController.h 다음에 수행하는 모든 작업은 무엇입니까? 다른보기를 표시 하시겠습니까? 현재 모델보기 또는 다른 어떤? –

+0

나는 대답을 편집했다. plz 그것을 확인 –

관련 문제