Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hotkarl

macrumors newbie
Original poster
Nov 19, 2010
29
1
Northern Utah
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];
}
 
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];
}
 
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.
 
Is it just the variable theAudio or is there something else you see there that would be leaking. I tried to release everything else.
 
Is it just the variable theAudio or is there something else you see there that would be leaking. I tried to release everything else.

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?
 
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.