0

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
wryIP
  • 1
  • 1
  • 1

2 Answers2

2

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

[audioPlayer stop];

audioPlayer.stop will not work.

Same goes for other places as well.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Mugunth
  • 14,461
  • 15
  • 66
  • 94
  • -(void)stop:(id)sender { audioPlayer.stop; [self resetControls]; } – wryIP Jul 17 '11 at 01:47
  • Do I need to change the syntax everywhere - even if it is working? – wryIP Jul 17 '11 at 01:48
  • are you sure your `stop:` method is working? You definitly reset the currentTime, so the music might start over, but in your stop method there is no code for actually stopping the audioPlayer. `audioPlayer.stop` just queries the value of the stop boolean. – marcus Jul 17 '11 at 11:03
  • So I changed my stop method from audioPlayer.stop; to use [audioPlayer stop]; and it works the same as before for my stop button. Both versions stopped the playing audio when I clicked the button. However, putting [audioPlayer stop]; in -(void)viewWillDisappear:(BOOL)animated or - (void)dealloc does nothing. Here is the code: -(void)viewWillDisappear:(BOOL)animated { [audioPlayer stop]; [super viewWillDisappear: animated]; } – wryIP Jul 19 '11 at 05:39
0

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

iGatiTech
  • 2,306
  • 1
  • 21
  • 45
user842059
  • 73
  • 3
  • 12
  • audioPlayer.stop = YES; throws a code error - It says Yes is undeclared – wryIP Jul 19 '11 at 05:40
  • I'm having the same problem. Effectively [audioPlayer stop] does not stop the player. I have set a breakpoint and know it gets to this statement. Using Xcode 4.1 – Ric Aug 02 '11 at 08:05
  • Ok, in my case it was a case of a deactivated object that I did not fully understand the life of. It's not easy to fathom this when you are new but I would like to confirm it is my code, not the Xcode environment. – Ric Aug 03 '11 at 08:07