Sound formats for iOS

This link is useful for understanding sound formats.

For all the sounds that I record, I use .caf format. For pre-recorded sounds I converted them all from .aiff to .m4a using afconvert—a developer tool from the command line. You can also use iTunes.

Here’s the script I use for afconvert. I adopted if from one I found on afconvert will convert to lots of formats, but the Apple Lossless makes the smallest file size for me.

# Reset the field separator to : instead of space so all of the file will be converted
for i in ~/Sounds/*; do
  # creates sound.aifc (IMA4 compression, MONO)
  #afconvert -f AIFC -d ima4 -c 1 $i

  # creates sound.m4a (Apple Lossless compression)
  afconvert -f m4af -d aac -q 127 -c 1 $i

Copy a file to the Documents directory in iOS

This is a simple method that I use to copy a file from the app bundle to the Documents directory. You can’t modify files in the app bundle, but you can modify them if they are in the Documents directory.

- (void)createResultsDocument {
    NSString *documentsDirectory = [self applicationDocumentsDirectory];
    NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"HTML_header.html"];
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *todaysDate = [dateFormatter stringFromDate:[NSDate date]];
    NSString *resultsFileName = [NSString stringWithFormat:@"MPResults_%@.html",todaysDate];
    NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:resultsFileName];
    NSError *error;
    [[NSFileManager defaultManager] copyItemAtPath:sourcePath 
    NSLog(@"The copy error is: %@", error);
    // Check that it worked.
    NSURL *resultsURL = [NSURL fileURLWithPath:destinationPath];
    NSLog(@"The resultsURL is: %@", resultsURL);

Xcode Buttons

This is some code I used to put a reset button on the screen. It’s mostly self-documenting. First, create a rect. I’ve already defined the x and y coordinates—buttonX and distanceFromTop based on the screen width and other buttons on the screen. Likewise, I’ve already defined the width. The rest of the code is just assigning properties to the button. The action is a method in the file that changes the image to a ‘Selected’ image.

// Reset Button
    @synthesize resetButton = resetButton;

    CGRect resetButtonFrame = CGRectMake(buttonX, distanceFromTop,
                                         button_width, 25.0f);            
    self.resetButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.resetButton.titleLabel.font            = [UIFont systemFontOfSize: 16];
    self.resetButton.titleLabel.textColor       = [UIColor blueColor];
    self.resetButton.titleLabel.shadowOffset    = CGSizeMake (1.0, 0.0);
    [self.resetButton setTitle:@"Reset Scoring" forState:UIControlStateNormal];
    [self.resetButton setFrame:resetButtonFrame];
    [self.resetButton addTarget:self 
    [self.view addSubview:self.resetButton];
    // This line makes the icons stay in the center of the screen when you rotate
    self.resetButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

- (IBAction)resetScorekeeper:(UIButton *)sender {
    [self resetResultsFile];
    [sender setImage:[UIImage imageNamed:@"ResetScoringSelected.png"] forState:UIControlStateNormal];