Why does the AVAudioPlayer stop method not work when switching views

*爱你&永不变心* 提交于 2019-12-11 17:42:35

问题


I have an iphone app that displays images and plays audio selected from a main menu.

The user clicks on a button to select the image/audio combo they want. The code to switch the views with animation works fine.

All of the code to display the image, play, pause, scrub, and stop the audio while in the new view works fine too.

However, when the users clicks the Main Menu button I want the playing audio to stop. I am using viewWillDisappear:(BOOL)animated to call the stop method:

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

This code doesn't stop the sound when the user switches back to the main menu view. Is there a better way to do this? Am I doing something wrong?

Here is the code from the entire class where the snippet above resides:

#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

回答1:


You call methods in objective-c using the following syntax.

[audioPlayer stop];

audioPlayer.stop will not work.

Same goes for other places as well.




回答2:


audioPlayer.stop will not work mostly because it needs an expression after it, e.g. audioPlayer.stop = //expression, stop is a bool, so you can say audioPlayer.stop = YES; or [audioPlayer stop];



来源:https://stackoverflow.com/questions/6714147/why-does-the-avaudioplayer-stop-method-not-work-when-switching-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!