为什么在切换视图时AVAudioPlayer停止方法不起作用

我有一个iphone应用程序,显示图像和播放从主菜单中select的audio。

用户点击一个button来select他们想要的图像/audio组合。 用animation切换视图的代码工作正常。

在新视图中显示图像,播放,暂停,擦洗和停止audio的所有代码都可以正常工作。

但是,当用户单击“主菜单”button时,我希望播放的audio停止。 我正在使用viewWillDisappear:(BOOL)animation来调用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 

您可以使用以下语法在objective-c中调用方法。

 [audioPlayer stop]; 

audioPlayer.stop将不起作用。

其他地方也一样。

audioPlayer.stop不会工作主要是因为它需要一个expression式后,例如audioPlayer.stop = //expression ,停止是一个bool ,所以你可以说audioPlayer.stop = YES;[audioPlayer stop];