//
//  SystemSoundsDemoViewController.m
//  SystemSoundsDemo
//
//  Created by Chris Adamson on 12/26/08.
//  Copyright Subsequently and Furthermore, Inc. 2008. All rights reserved.
//
//
//  Licensed with the Apache 2.0 License
//  http://apache.org/licenses/LICENSE-2.0
//


#import "SystemSoundsDemoViewController.h"
 

@implementation SystemSoundsDemoViewController

@synthesize statusLabel;

//START:code.SystemSoundsDemo.SystemSoundsDemoViewController.tabledataconstants
NSString *soundNames[] = {
	@"Vibrate", @"One", @"Two", @"Three", @"Four",
	@"Five", @"Six", @"Seven", @"Eight", @"Nine"
};
NSInteger SOUND_COUNT = 10;
//END:code.SystemSoundsDemo.SystemSoundsDemoViewController.tabledataconstants




/*
// 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);
}
*/


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"SoundCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
	
//START:code.SystemSoundsDemo.SystemSoundsDemoViewController.setupcell
	// Set up the cell...
	cell.textLabel.text = soundNames[indexPath.row];
	return cell;
//END:code.SystemSoundsDemo.SystemSoundsDemoViewController.setupcell
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return SOUND_COUNT;
}

/* simple version without callbacks
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	// create a system sound id for the selected row
	SystemSoundID soundID;
	
	if (indexPath.row == 0) {
		// special case: vibrate
		soundID = kSystemSoundID_Vibrate;
	} else {
		// find corresponding CAF file
		NSString *cafName = [NSString stringWithFormat: @"%d", indexPath.row];
		NSString *cafPath = [[NSBundle mainBundle] pathForResource:cafName ofType:@"caf"];
		NSURL *cafURL = [NSURL fileURLWithPath:cafPath];
		OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef) cafURL, &soundID);
		// TODO handle error
	}

	AudioServicesPlaySystemSound (soundID);
}
*/

// status callback function (notice this is a c function, not an obj-c method)
//START:code.SystemSoundsDemo.SystemSoundsDemoViewController.systemsoundsdemocompletionproc
void SystemSoundsDemoCompletionProc (
		 SystemSoundID  soundID,
		 void           *clientData)
{
	AudioServicesDisposeSystemSoundID (soundID);
	((SystemSoundsDemoViewController*)clientData).statusLabel.text = @"Stopped";
};
//END:code.SystemSoundsDemo.SystemSoundsDemoViewController.systemsoundsdemocompletionproc


// deluxe version with callbacks
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//START:code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound
	// create a system sound id for the selected row
	SystemSoundID soundID;

	OSStatus err = kAudioServicesNoError;
	if (indexPath.row == 0) {
		// special case: vibrate
		soundID = kSystemSoundID_Vibrate; //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.vibratesystemsound"/>
	} else {
		// find corresponding CAF file
		NSString *cafName = [NSString stringWithFormat: @"%d", indexPath.row]; //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.rowtonumberstring"/>
		NSString *cafPath = 
			[[NSBundle mainBundle] pathForResource:cafName ofType:@"caf"]; //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.findcafinbundle"/>
		NSURL *cafURL = [NSURL fileURLWithPath:cafPath]; //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.fileurlwithpath"/>
		err = AudioServicesCreateSystemSoundID((CFURLRef) cafURL, &soundID); //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.createsystemsound"/>
	}
//END:code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound
//START:code.SystemSoundsDemo.SystemSoundsDemoViewController.registercompletionprocandplaysound
	if (err == kAudioServicesNoError) {
		// set up callback for sound completion
		 err = AudioServicesAddSystemSoundCompletion //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.addcompletionproc"/>
			(soundID,		// sound to monitor
			NULL,			// run loop (NULL==main)
			NULL,			// run loop mode (NULL==default)
			SystemSoundsDemoCompletionProc, // callback function //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.completionprocroutine"/>
			self			// data to provide on callback
		); //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.addcompletionprocend"/>
		statusLabel.text = @"Playing"; //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.setlabel"/>
		AudioServicesPlaySystemSound (soundID); //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.playsound"/>
	}
	if (err != kAudioServicesNoError) { //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.errorblockstart"/>
		CFErrorRef error = CFErrorCreate(NULL, kCFErrorDomainOSStatus, err, NULL); //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.createcferror"/>
		NSString *errorDesc = (NSString*) CFErrorCopyDescription (error); //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.copycferrordescription"/>
		UIAlertView *cantPlayAlert =
			[[UIAlertView alloc] initWithTitle:@"Cannot Play:"
				message: errorDesc
				delegate:nil
				cancelButtonTitle:@"OK"
				otherButtonTitles:nil];
		[cantPlayAlert show];
		[cantPlayAlert release]; 
		[errorDesc release]; //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.releaseerrordescription"/>
		CFRelease (error); //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.releaseerror"/>
	} //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.errorblockend"/>
//END:code.SystemSoundsDemo.SystemSoundsDemoViewController.registercompletionprocandplaysound
}


- (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
