AVAudioPlayer

Discussion in 'iOS Programming' started by hotkarl, Nov 29, 2010.

  1. hotkarl macrumors newbie

    hotkarl

    Joined:
    Nov 19, 2010
    Location:
    Northern Utah
    #1
    When I press the button the else statement only the else statement works, what am I missing from the if part? Every time I press the button the song plays and starts playing over itself. I'm trying to avoid that. I read the entire section in about the AVAudioPlayer, but I think I am missing something much more fundamental. Any help would be tremendously appreciated.

    Code:
    -(IBAction) playLick: (id) sender { 
            NSString *musicPath = [[NSBundle mainBundle] pathForResource:titleForButton ofType:@"mp3"];
    	theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath] error:NULL];
    	
    	if (theAudio.playing) {
    		[theAudio pause];
    		[sender setTitle: @"Play" forState: UIControlStateHighlighted];
    		[sender setTitle: @"Play" forState: UIControlStateNormal];
    	}
    	else {
    		[theAudio play];
    		[sender setTitle: @"Pause" forState: UIControlStateHighlighted];
    		[sender setTitle: @"Pause" forState: UIControlStateNormal];
    	}
    	
    	[musicPath release];
    }
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    theAudio is being set to a brand-new, just allocated object every time. I very much doubt that's what you want.
     
  3. hotkarl thread starter macrumors newbie

    hotkarl

    Joined:
    Nov 19, 2010
    Location:
    Northern Utah
    #3
    I figured it out... I think. That is to say it works the way I want it to.

    Anyone see any reason why I wouldn't want to do it like this:
    Code:
    #import "_ViewController.h"
    @implementation _ViewController
    @synthesize scrollView;
    @synthesize theAudio;
    @synthesize playBtn;
    
    -(IBAction) selectLick:(id)sender {
    	titleForButton = [sender titleForState: UIControlStateNormal];
    	imagePath = [[NSString alloc] initWithFormat:@"%@.png", titleForButton];
    	imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imagePath]];
    	[imagePath release];
    	[scrollView setBackgroundColor:[UIColor blackColor]];
        [scrollView setCanCancelContentTouches:NO];
        scrollView.clipsToBounds = YES; 
        scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    	[[scrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    	[scrollView addSubview:imageView];
        [scrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
    	[imageView release];
        [scrollView setScrollEnabled:YES];
    	[theAudio stop];
    	[playBtn setTitle:@"Play" forState:(UIControlState)UIControlStateNormal];
    	musicPath = [[NSBundle mainBundle] pathForResource:titleForButton ofType:@"mp3"];
    	fileURL = [[NSURL alloc] initFileURLWithPath: musicPath];
    	theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
    	[fileURL release];
    	[theAudio setDelegate: self];
    	[theAudio prepareToPlay];
    }
    
    -(IBAction) playLick: (id) sender {
    	if (theAudio.isPlaying) {
    		[theAudio pause];
    		[sender setTitle:@"Play" forState:(UIControlState)UIControlStateNormal];
    	}
    	else {
    		[theAudio play];
    		[sender setTitle:@"Pause" forState:(UIControlState)UIControlStateNormal];
    	}
    }
    
    - (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)theAudio successfully:(BOOL)flag 
    { 
    	[playBtn setTitle:@"Play" forState:(UIControlState)UIControlStateNormal];
    }
    
     
  4. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #4
    Better, but a lot of the instance variables you are assigning newly allocated objects to in your selectLick action are going to leak each time it is called.

    You need to make sure you release any objects before assigning new ones to the same instance variable. You could do this manually, or use properties to save you a lot of the work.
     
  5. hotkarl thread starter macrumors newbie

    hotkarl

    Joined:
    Nov 19, 2010
    Location:
    Northern Utah
    #5
    Is it just the variable theAudio or is there something else you see there that would be leaking. I tried to release everything else.
     
  6. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #6
    On second glance it does look like its just theAudio that leaks.

    It seems like a lot of those instance variables could be local, any reason why they aren't?
     
  7. hotkarl thread starter macrumors newbie

    hotkarl

    Joined:
    Nov 19, 2010
    Location:
    Northern Utah
    #7
    Yeah, there is a good reason... because I don't know what the F&%$ I'm doing. This is my first programming language and I'm just trying to find the answers as I go along. I have an application with 56 buttons. Each button displays an image and allows you to listen to a corresponding sound file. This seemed like the least amount of work to make it do what I want. I'm still trying to figure out the theAudio thing. How to do it without leakin'. I love that you guys are all here and willing to help the rest of us dips along the way. If I ever get good at this I swear I'll do the same. Thanks!
     

Share This Page