Hi,
I followed the short sounds tutorial and then modified it to use buttons and multiple sounds.
All of my sounds are in wav format and less than 5 seconds.
I add the first sound, test it press the button, it works great.
Then I repeat the process for the second and third sound, great they work too.
However when I go to add a fourth sound, the app compiles and shows no errors but the screen just stays back and then after about two minutes the app closes with no errors.
I can only guess that it might be some kind of memory leak but I don't really know.
Could someone help me please?
Here is my code:
Main.h
Code:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@class SoundEffect;
@interface MainView : UIView {
SoundEffect *soundEffect;
SoundEffect *soundEffect2;
SoundEffect *soundEffect3;
SoundEffect *soundEffect4;
}
- (IBAction)sound1;
- (IBAction)sound2;
- (IBAction)sound3;
- (IBAction)sound4;
@end
Main.m
Code:
#import "MainView.h"
#import "SoundEffect.h"
@implementation MainView
-(void)awakeFromNib {
NSBundle *mainBundle = [NSBundle mainBundle];
soundEffect = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"absobloody" ofType:@"wav"]];
soundEffect2 = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"fire" ofType:@"wav"]];
soundEffect3 = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"butter" ofType:@"wav"]];
soundEffect4 = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Cashback" ofType:@"wav"]];
}
- (IBAction)sound1 {
[soundEffect play];
}
- (IBAction)sound2 {
[soundEffect2 play];
}
- (IBAction)sound3 {
[soundEffect3 play];
}
- (IBAction)sound4 {
[soundEffect4 play];
}
@end
SoundEffect.m
Code:
//
// SoundEffect.m
// sound
//
// Created by Tim Hugall on 11/05/2009.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "SoundEffect.h"
@implementation SoundEffect
- (id)initWithContentsOfFile:(NSString *)path
{
self = [super init];
if (self != nil) {
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
}
return self;
}
-(void)play {
AudioServicesPlaySystemSound(soundID);
}
@end
SoundEffect.h
Code:
//
// SoundEffect.h
// sound
//
// Created by Tim Hugall on 11/05/2009.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioServices.h>
@interface SoundEffect : NSObject {
SystemSoundID soundID;
}
- (id)initWithContentsOfFile:(NSString *)path;
-(void)play;
@end
soundAppDelegate.h
Code:
//
// soundAppDelegate.h
// sound
#import <UIKit/UIKit.h>
@class soundViewController;
@interface soundAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
soundViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet soundViewController *viewController;
@end
soundAppDelegate.m
Code:
//
// soundAppDelegate.m
// sound
#import "soundAppDelegate.h"
#import "soundViewController.h"
@implementation soundAppDelegate
@synthesize window;
@synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
soundViewController.h
Code:
//
// soundViewController.h
// sound
#import <UIKit/UIKit.h>
@interface soundViewController : UIViewController {
}
@end
soundViewController.m
Code:
#import "soundViewController.h"
@implementation soundViewController
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[super dealloc];
}
@end
Please help. I am trying to make this app as a birthday present but it's not going well.