2010-07-10 6 views
0

오디오 플레이어가있는 클래스가 있습니다. B 클래스에는 배경 음악 재생을 중단하고 배경 음악을 중단하고 싶을 때 전환 할 수있는 스위치가 있습니다.문제 AVAudioPlayer 시작 및 중지

스위치를 통해 음악을 중단하고 재생할 수 없습니다. 나는 그것을 어떻게 클래스 B의 스위치 없음

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/bgmusic.wav", [[NSBundle mainBundle] resourcePath]]]; 
NSError *error; 
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
audioPlayer.numberOfLoops = -1; 
[audioPlayer play]; 
if (audioPlayer == nil) NSLog([error description]); 
else [audioPlayer play]; 

을 클래스의 viewDidLoad에서 내 오디오 플레이어를 할 수

-(IBAction) musicValueChanged:(id)sender{  
if (musicSwitch.on) { 
    isStopped = FALSE; 
    firstViewController.player.currentTime = 0; 
    [firstViewController.audioPlayer play]; 
}else { 
    [firstViewController.audioPlayer volume:0]; 
} 
} 

되어 이것을이됩니다 ?

+0

어디서 붙어 있습니까? 무엇을 시도 했습니까? 우리는 초능력이 아니므로 디자인이 어떻게 생겼는지 알려줘야합니다. –

+0

이것은 A 클래스의 viewdidload에있는 내 오디오 플레이어입니다. NSURL * url = [NSURL fileURLWithPath : [NSString stringWithFormat : @ "% @/bgmusic.wav", [[NSBundle mainBundle] resourcePath]]]]; \t \t NSError * error; \t audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL : url 오류 : & 오류]; \t audioPlayer.numberOfLoops = -1; \t \t [audioPlayer play]; \t if (audioPlayer == nil) \t \t NSLog ([오류 설명]); \t else \t \t [audioPlayer play]; – iscavengers

+0

이 클래스 B 의 스위치 없음이 - (IBAction를는) musicValueChanged (ID) 송신기 {\t \t 경우 (musicSwitch.on) { \t \t isStopped = FALSE 단계; \t \t firstViewController.player.currentTime = 0; \t \t [firstViewController.audioPlayer play]; \t} else {[firstViewController.audioPlayer volume : 0]; }} – iscavengers

답변

1

이있는 한 방법이 호출되고 musicSwitch.on에 값이,이 작동합니다 :

- (IBAction를가) musicValueChanged 다음 AVAudioPlayer의 문서를 참조하십시오 (ID) 보낸 사람 {

if (musicSwitch.on) { 
    isStopped = FALSE; 
    firstViewController.player.currentTime = 0; 
    [firstViewController.audioPlayer play]; 
}else { 
    [firstViewController.audioPlayer stop]; 
} 
} 

합니다.