Programando Sons - Parte Final
domingo, outubro 25th, 2009Agora, vamos definir cada som que será executado. Escolhi fazer esta definição no momento que a aplicação é carregada. Ou seja, no método viewDidLoad vamos definir cada som que será executado no momento que o usuário tocar em um dos botões. Vamos implementar no arquivo MainViewController.m, colocando o seguinte código:
#import “MainViewController.h”
#import “MainView.h”
@implementation MainViewController
@synthesize btnDrums;
@synthesize btnIntro;
@synthesize btnLove;
@synthesize btnLaugh;
@synthesize btnOhhh;
@synthesize btnTrhill;
@synthesize soundFileURLRefIntro;
@synthesize soundFileObjectIntro;
@synthesize soundFileURLRefOhhh;
@synthesize soundFileObjectOhhh;
@synthesize soundFileURLRefLove;
@synthesize soundFileObjectLove;
@synthesize soundFileURLRefDrums;
@synthesize soundFileObjectDrums;
@synthesize soundFileURLRefThrill;
@synthesize soundFileObjectThrill;
@synthesize soundFileURLRefLaugh;
@synthesize soundFileObjectLaugh;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
- (void) viewDidLoad {
// Intro sound
id sndpathIntro = [[NSBundle mainBundle] pathForResource:@”abertura 3″ ofType:@”wav”];
CFURLRef baseURLIntro = (CFURLRef)[[NSURL alloc] initFileURLWithPath:sndpathIntro];
AudioServicesCreateSystemSoundID(baseURLIntro, &soundFileObjectIntro);
// Ohhh sound
id sndpathOhhh = [[NSBundle mainBundle] pathForResource:@”oh” ofType:@”wav”];
CFURLRef baseURLOhhh = (CFURLRef)[[NSURL alloc] initFileURLWithPath:sndpathOhhh];
AudioServicesCreateSystemSoundID(baseURLOhhh, &soundFileObjectOhhh);
// Love sound
id sndpathLove = [[NSBundle mainBundle] pathForResource:@”love10″ ofType:@”wav”];
CFURLRef baseURLLove = (CFURLRef)[[NSURL alloc] initFileURLWithPath:sndpathLove];
AudioServicesCreateSystemSoundID(baseURLLove, &soundFileObjectLove);
// Laugh sound
id sndpathLaugh = [[NSBundle mainBundle] pathForResource:@”laugh” ofType:@”wav”];
CFURLRef baseURLLaugh = (CFURLRef)[[NSURL alloc] initFileURLWithPath:sndpathLaugh];
AudioServicesCreateSystemSoundID(baseURLLaugh, &soundFileObjectLaugh);
// Drums sound
id sndpathDrums = [[NSBundle mainBundle] pathForResource:@”rim_shot 1″ ofType:@”wav”];
CFURLRef baseURLDrums = (CFURLRef)[[NSURL alloc] initFileURLWithPath:sndpathDrums];
AudioServicesCreateSystemSoundID(baseURLDrums, &soundFileObjectDrums);
// Thrill sound
id sndpathThrill = [[NSBundle mainBundle] pathForResource:@”suspense” ofType:@”wav”];
CFURLRef baseURLThrill = (CFURLRef)[[NSURL alloc] initFileURLWithPath:sndpathThrill];
AudioServicesCreateSystemSoundID(baseURLThrill, &soundFileObjectThrill);
[super viewDidLoad];
}
- (IBAction)playIntro:(id)sender {
AudioServicesPlaySystemSound (soundFileObjectIntro);
}
- (IBAction)playDrums:(id)sender {
AudioServicesPlaySystemSound (soundFileObjectDrums);
}
- (IBAction)playLaugh:(id)sender {
AudioServicesPlaySystemSound (soundFileObjectLaugh);
}
- (IBAction)playLove:(id)sender {
AudioServicesPlaySystemSound (soundFileObjectLove);
}
- (IBAction)playOhhh:(id)sender {
AudioServicesPlaySystemSound (soundFileObjectOhhh);
}
- (IBAction)playThrill:(id)sender {
AudioServicesPlaySystemSound (soundFileObjectThrill);
}
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller {
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)showInfo {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@”FlipsideView” bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn’t have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren’t in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
Estamos terminando, agora só falta relacionar cada ação que criamos com os botões que estão na Main View, lá no Interface Builder.

Bom, agora é só executar e curtir a aplicação, como dissemos na edição passada, a idéia é passar o conceito de como montar uma aplicação e não lhe ensinar tão somente em fazer igual. Qualquer dúvida entre em contato com o editor da MAC+.
(Matéria publicada na revista MAC+)
(by Ademar Varela)




