2011-07-15 22 views
0

我有一個iPhone應用程序,它可以顯示圖像並播放從主菜單中選擇的音頻。爲什麼在切換視圖時AVAudioPlayer停止方法不起作用

用戶點擊一個按鈕來選擇他們想要的圖像/音頻組合。用動畫切換視圖的代碼工作正常。

在新視圖中顯示圖像,播放,暫停,擦洗和停止音頻的所有代碼都可以正常工作。

但是,當用戶單擊主菜單按鈕時,我希望播放音頻停止。我使用viewWillDisappear:(BOOL)動畫調用stop方法:

-(void)viewWillDisappear:(BOOL)animated { 
audioPlayer.stop; 
[super viewWillDisappear: animated];} 

當用戶切換回主功能表視圖此代碼不會停止聲音。有一個更好的方法嗎?難道我做錯了什麼?

這裏是從整個類的代碼,其中上述代碼段駐留:

#import "twelthPoem.h" 

UIImageView *largeImageView; 

@implementation twelthPoem 

-(void)resetControls 
{ 
audioPlayer.currentTime = 0; 
scrubber.value = 0; 
[playButton setImage:[UIImage imageNamed:@"play_HL.png"] 
    forState:UIControlStateNormal]; 
} 

-(void)play:(id)sender { 


if (! audioPlayer.playing) { 
audioPlayer.play; 
[playButton setImage:[UIImage imageNamed:@"pauseHL.png"] forState:UIControlStateNormal]; 
} 
else { 
audioPlayer.pause; 
[playButton setImage:[UIImage imageNamed:@"play_HL.png"]   forState:UIControlStateNormal]; 
} 

[self becomeFirstResponder]; 
} 

-(void)stop:(id)sender { 

audioPlayer.stop; 
[self resetControls]; 
} 

-(void)changeVolume:(id)sender { 

audioPlayer.volume = volume.value; 
[self becomeFirstResponder]; 
} 

-(void)scrub:(id)sender { 

if (audioPlayer.playing) { 

audioPlayer.pause; 
audioPlayer.currentTime = scrubber.value; 
audioPlayer.play; 
} 
else 
audioPlayer.currentTime = scrubber.value; 
[self becomeFirstResponder]; 
} 

-(void)createControls { 

//play/pause button 
playButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[playButton setFrame:CGRectMake(60,405,80,20)]; 
[playButton setImage:[UIImage imageNamed:@"play_HL.png"] forState:UIControlStateNormal]; 
[playButton addTarget:self action:@selector(play:) 
forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:playButton]; 

//stop button 
stopButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[stopButton setFrame:CGRectMake(180,405,80,20)]; 
[stopButton setImage:[UIImage imageNamed:@"stopHL.png"] forState:UIControlStateNormal]; 
[stopButton addTarget:self action:@selector(stop:) 
forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:stopButton]; 

//volume control 
volume = [[UISlider alloc] initWithFrame:CGRectMake(10,445,145,20)]; 
[volume addTarget:self action:@selector(changeVolume:) 
forControlEvents:UIControlEventValueChanged]; 
volume.minimumValue = 0.0; 
volume.maximumValue = 1.0; 
volume.value = audioPlayer.volume; 
volume.continuous = YES; 
[self.view addSubview:volume]; 

//scrubber control 
scrubber = [[UISlider alloc] initWithFrame:CGRectMake(165,445,145,20)]; 
[scrubber addTarget:self action:@selector(scrub:) 
    forControlEvents:UIControlEventValueChanged]; 
scrubber.minimumValue = 0.0; 
scrubber.maximumValue = audioPlayer.duration; 
scrubber.value = audioPlayer.currentTime; 
scrubber.continuous = NO; 
[self.view addSubview:scrubber]; 

} 

- (void)remoteControlReceivedWithEvent:(UIEvent *)event { 

switch (event.subtype) 
{ 
case UIEventSubtypeRemoteControlTogglePlayPause: 
    [self play:nil]; 
    break; 

case UIEventSubtypeRemoteControlNextTrack: 
    //do nothing 
    break; 

case UIEventSubtypeRemoteControlPreviousTrack: 
    //do nothing 
    break; 
} 
} 

- (BOOL)canBecomeFirstResponder { 

return YES; 
} 

-(void)viewDidLoad {  

//for scrolling the image 

[super viewDidLoad]; 

CGRect scrollFrame = CGRectMake(0,40,320,350); 
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame]; 
scrollView.minimumZoomScale = 1.0; 
scrollView.maximumZoomScale = 1.5; 
scrollView.delegate = self; 


UIImage *bigImage = [UIImage imageNamed:@"birches.png"]; 



largeImageView = [[UIImageView alloc] initWithImage:bigImage]; 


[scrollView addSubview:largeImageView]; 
scrollView.contentSize = largeImageView.frame.size; //important! 



[self.view addSubview:scrollView]; 

[scrollView release]; 


//for playing the recording 

NSString *filePath = [[[NSBundle mainBundle] resourcePath] 
      stringByAppendingPathComponent:@"birches_final_mp3.mp3"]; 
NSURL *fileURL = [NSURL fileURLWithPath:filePath]; 

NSError *error = nil; 

OSStatus status = AudioSessionInitialize(NULL, NULL, NULL, NULL);  
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback; 
status = AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, 
        sizeof (sessionCategory), 
        &sessionCategory); 
AudioSessionSetActive(YES); 

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL 
         error:&error]; 

     if (error) 
    NSLog(@"An error occurred: %@",error); 
     else 
    { 
    audioPlayer.volume = 0.3; 
    [audioPlayer prepareToPlay]; 

    [self createControls]; 
    } 

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
} 

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{ 
    return largeImageView; 
} 

- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player 
{ 
    interrupted = audioPlayer.playing; 
} 

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player 
{ 
    if (interrupted) 
    audioPlayer.play; 
} 

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player 
       successfully:(BOOL)flag 
{ 
    [self resetControls]; 
} 




-(void)viewWillDisappear:(BOOL)animated { 
    audioPlayer.stop; 
    [super viewWillDisappear: animated];  

} 

- (void)dealloc { 

    [[UIApplication sharedApplication] endReceivingRemoteControlEvents]; 
    [scrubber release]; 
    [volume release]; 
    [audioPlayer release]; 

    [super dealloc]; 
} 

@end 

回答

2

你打電話使用以下語法在Objective-C的方法。

[audioPlayer stop]; 

audioPlayer.stop將不起作用。

其他地方也一樣。

+0

當我停止按鈕調用此一件作品: – wryIP

+0

- (空)站:(ID)發送{ audioPlayer.stop; [self resetControls]; } – wryIP

+0

我是否需要隨時更改語法 - 即使它正在工作? – wryIP

0

audioPlayer.stop將不會工作,主要是因爲它需要一個表達式,例如, audioPlayer.stop = //expression,停止是bool,所以可以說audioPlayer.stop = YES;[audioPlayer stop];

+0

audioPlayer.stop = YES;拋出代碼錯誤 - 它說「是」未聲明 – wryIP

+0

我遇到同樣的問題。有效地[audioPlayer停止]不會停止播放器。我已經設置了一個斷點,並知道它得到這個聲明。使用Xcode 4.1 – Ric

+0

好吧,在我的情況下,這是一個停用的對象,我沒有完全理解的生活。當你是新手時,很難理解這一點,但我想確認它是我的代碼,而不是Xcode環境。 – Ric

相關問題