Segmented Control

The play button is disabled until we have asound file to play. The red recording light goes on when we press the record button.

Source code in Segmented.zip

  1. main.m
  2. Class SegmentedAppDelegate
  3. Class View

Create the project

SegmentedAppDelegate.h imports AVFoundation.h. The application delegate fulfills three protocols.

The size of the control

The height of the UISegmentedControl defaults to 29 pairs of pixels. But this fact is nowhere in Apple’s documentation and may therefore change without warning.

Class AVAudioRecorder and protocol AVAudioRecorderDelegate

See this document.

The NSNumber objects in the left column of the dictionary can be created with Objective-C literals.

	NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
		@(kAudioFormatAppleIMA4), AVFormatIDKey,
		@44100.0,                 AVSampleRateKey,
		@2,                       AVNumberOfChannelsKey,
		nil
	];
2013-11-06 20:41:09.805 Segmented[4052:a0b] inputNumberOfChannels == 2
2013-11-06 20:41:09.806 Segmented[4052:a0b] ouputNumberOfChannels == 2
2013-11-06 20:41:09.807 Segmented[4052:a0b] sampleRate == 44100
2013-11-06 20:41:09.808 Segmented[4052:a0b] preferredSampleRate == 0
2013-11-06 20:41:09.808 Segmented[4052:a0b] preferredIOBufferDuration == 0
2013-11-06 20:41:09.809 Segmented[4052:a0b] filename == "/Users/myname/Library/Application Support/iPhone Simulator/7.0.3-64/Applications/FBE0988A-0D72-40FF-9A18-5372706262D2/tmp/recording.caf"
2013-11-06 20:41:09.810 Segmented[4052:a0b] url == "file:///Users/myname/Library/Application%20Support/iPhone%20Simulator/7.0.3-64/Applications/FBE0988A-0D72-40FF-9A18-5372706262D2/tmp/recording.caf"
2013-11-06 20:41:10.069 Segmented[4052:a0b] Application windows are expected to have a root view controller at the end of application launch
2013-11-06 20:41:23.074 Segmented[4052:a0b] audioRecorderDidFinishRecording:successfully: 1
2013-11-06 20:41:27.694 Segmented[4052:a0b] audioPlayerDidFinishPlaying:successfully: 1

Things to try

  1. Replace the array of strings with an array of images. Copy the files usa.png, canada.png, and mexico.png into the Supporting Files folder.

    In the initWithFrame: method of class View, change the array of items to the following.

    	NSArray *items = [NSArray arrayWithObjects:
    		[UIImage imageNamed: @"usa.png"],
    		[UIImage imageNamed: @"canada.png"],
    		[UIImage imageNamed: @"mexico.png"],
    		nil
    	];
    
    Each .png file is 32 × 20 pixels, so you might want to change the size of the UISegmentedControl by assigning to its bounds property.

  2. Create a set of enumeration values which will be symbolic names for the button numbers. Define them immediately after the #imports at the start of SegmentedAppDelegate.h.
    enum {
    	segRecord,
    	segStop,
    	segPlay
    };
    
    which means approximately the same thing as
    int segRecord = 0;
    int segStop = 1;
    int segPlay = 2;
    
    Then in the switch statement in the valueChanged: method of class SegmentedAppDelegate, change
    case 0:
    to
    case segRecord:
    and change
    case 1:
    to
    case segStop:
    etc. Change the 2 to segPlay in audioRecorderDidFinishRecording:successfully:. In View.m, import SegmentedAppDelegate.h and change the 2 in the call to setEnabled:forSegmentAtIndex: to segPlay.

  3. Add a Pause button.