2010-07-10 68 views
0

我有一個課,我有一個音頻播放器。在B班,我有一個開關,從那裏我想控制播放和停止背景音樂。問題啓動和停止AVAudioPlayer

我無法通過開關停止和播放音樂。 這是在上課的viewDidLoad我的音頻播放機的

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]; 

這是B類的馬桶蓋

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

我該怎麼辦呢?

+0

你卡在哪裏,你試過了什麼?我們不是通靈的,所以你必須告訴我們你的設計是怎樣的。 – 2010-07-10 13:16:49

+0

這是我的音頻播放器在viewdidload類A NSURL * url = [NSURL fileURLWithPath:[NSString stringWithFormat:@「%@/bgmusic.wav」,[[NSBundle mainBundle] resourcePath]]]; \t \t NSError * error; \t audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; \t audioPlayer.numberOfLoops = -1; \t \t [audioPlayer play]; (audioPlayer == nil) \t \t NSLog([error description]);如果(audioPlayer == nil) \t \t \t else \t \t [audioPlayer play]; – iscavengers 2010-07-10 13:17:54

+0

這是B類 的SWTICH - (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 2010-07-10 13:18:22

回答

1

只要方法被調用和musicSwitch.on具有值,這應該工作:

- (IBAction爲)musicValueChanged:(ID)發送方{

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

參見AVAudioPlayer文檔。