AVAudioRecorder & System Sound Services Dept. of Multimedia Science, Sookmyung Women s University. prof. JongWoo Lee Index AVAudioRecorder? - (AudioRecorder) System Sound Service? - (SysSound)
AVAudioRecorder AVAudioRecorder? ( Audio Queue Services ) NSMutableData object audio recorder - Record until the user stops the recording - Record for a specified duration - Pause and resume a recording - Obtain input audio-level data that you can use to provide level metering AVAudioRecorder Class Inherits Conforms to NSObect NSObect(NSObject) Framework Availability Declared in /System/Library/Frameworks/AVFoundation.framework Available in ios 3.0 and later AVAudioRecorder.h
3 AVAudioSession AVAudioPlayer, AVAudioRecorder AVAudioRecorder : AudioRecorder Begin Recording Stop Recording Start Recording Play Recording
Xcode_ Xcode file-new Project View-based Application choose AudioRecorder Save Interface Builder_UI AudioRecorderViewController.xib Interface Builder View UI 1. View Attributes background 2. Library Round Rect Button 2 Activity Indicator View Label ( ) : Interface Builder View UI Xcode
Xcode_AudioRecorderViewController.h Classes 4 AudioRecorderViewController.h Interface Builder UI #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import <CoreAudio/CoreAudioTypes.h> @interface record_audio_testviewcontroller : UIViewController <AVAudioRecorderDelegate> { IBOutlet UIButton * btnstart; IBOutlet UIButton * btnplay; IBOutlet UIActivityIndicatorView * actspinner; BOOL toggle; // begin/start NSURL * recordedtmpfile; AVAudioRecorder * recorder; NSError * error; @property (nonatomic,retain)iboutlet UIActivityIndicatorView * actspinner; @property (nonatomic,retain)iboutlet UIButton * btnstart; @property (nonatomic,retain)iboutlet UIButton * btnplay; // - (IBAction) start_button_pressed; // - (IBAction) play_button_pressed; // @end : Xcode_AudioRecorderViewController.m Classes 4 AudioRecorderViewController.m 1. viewdidload{ #import "AudioRecorderViewController.h" @implementation AudioRecorderViewController @synthesize actspinner, btnstart, btnplay; //nib - (void)viewdidload { [super viewdidload]; toggle = YES; //toggle true mode btnplay.hidden = YES; // play button //AVAudioSession object instance AVAudioSession * audiosession = [AVAudioSession sharedinstance]; // audiosession //AudioSession AVAudioSesseionCategoryPlayAndRecord [audiosession setcategory:avaudiosessioncategoryplayandrecord error: &error]; [audiosession setactive:yes error: &error]; //
Xcode_AudioRecorderViewController.m 2. start_button_pressed{ - (IBAction) start_button_pressed{ if(toggle) { toggle = NO; [actspinner startanimating]; [btnstart settitle:@"stop Recording" forstate: UIControlStateNormal ]; btnplay.enabled = toggle; btnplay.hidden = toggle; //recording session //dictionary object setup NSMutableDictionary* recordsetting = [[NSMutableDictionary alloc] init]; [recordsetting setvalue :[NSNumber numberwithint:kaudioformatappleima4] forkey:avformatidkey]; // recordedtmpfile = [NSURL fileurlwithpath:[nstemporarydirectory() stringbyappendingpathcomponent: [NSString stringwithformat: @"%.0f.%@", [NSDate timeintervalsincereferencedate] * 1000.0, @"caf"]]]; //Setup the recorder to use this file and record to it. recorder = [[ AVAudioRecorder alloc] initwithurl:recordedtmpfile settings:recordsetting error:&error]; // recorder [recorder setdelegate:self]; // // [recorder preparetorecord]; // [recorder record]; Xcode_AudioRecorderViewController.m 2. start_button_pressed{ ( ) else { toggle = YES; [actspinner stopanimating]; [btnstart settitle:@"start Recording" forstate:uicontrolstatenormal ]; btnplay.enabled = toggle; btnplay.hidden = toggle; //Stop the recorder. [recorder stop]; 3. play_button_pressed{ -(IBAction) play_button_pressed{ //play // AVAudioPlayer AVAudioPlayer * avplayer = [[AVAudioPlayer alloc] initwithcontentsofurl:recordedtmpfile error:&error]; [avplayer preparetoplay]; [avplayer play];
Xcode_AudioRecorderViewController.m 4. viewdidunload{ - (void)viewdidunload { //main view subview // e.g. self.myoutlet = nil; // NSFileManager * fm = [NSFileManager defaultmanager]; [fm removeitematpath:[recordedtmpfile path] error:&error]; // object dealloc [recorder dealloc]; recorder = nil; recordedtmpfile = nil; Build and Run... AVAudioSession audiosession AVAudioSessionCategoryPlayAndRecord kaudioformatappleima4 AVFormatIDKey. Framework - AVFoundation.framework - CoreAudio.framework
Framework AudioRecorderViewController.h import #import <CoreAudio/CoreAudioTypes.h> #import <AVFoundation/AVFoundation.h> Groups & Files Frameworks CoreAudio, AVFoundation Add Xcode Interface Builder (1/4) AudioRecorderViewController.xib Interface Builder 1. View File s Owner. 2. xib File s Owner Outlet. 3. Begin Recording Play Recording. 4. Activity Indicator View. 1. View File s Owner File s Owner View File s Owner control View
Xcode Interface Builder (2/4) 2. xib File s Owner Outlet. File s Owner Inspector Audio Recorder View Controller Connections Xcode Outlet View Activity Indicator Xcode Interface Builder (3/4) 3. Begin Recording Play Recording Begin Recording Inspector Button Connections Touch Up Inside File s Owner start_button_pressed Play Recording Touch Up Inside File s Owner play_button_pressed
Xcode Interface Builder (4/4) 4. Activity Indicator View Activity Indicator View Hide When Stopped Xcode Build & Run &
Q. Simulator...? imac?? ~
System Sound Service System Sound Service(1/3) level, positioning, audio session - 30 - Linear PCM IMA4(IMA/ADPCM) -.caf,.aif.wav playback method - AudioServicesPlaySystemSound ( ) - AudioServicesPlayAlertSound ( ) Declared in AudioServices.h in Audio Toolbox framework
System Sound Service(2/3) Functions by Task Creating and Disposing of System Sound Objects Playing Sounds Adding and Removing System Sound Callbacks AudioServicesCreateSystemSoundID AudioServicesDisposeSystemSoundID AudioServicesPlayAlertSound AudioServicesPlaySystemSound AudioServicesAddSystemSoundCompletion AudioServicesRemoveSystemSoundCompletion AudioServicesPlaySystemSound - very simply play short sound files - Sounds play at the current system audio volume, with no programmatic volume control available - Sounds play immediately - Looping and stereo positioning are unavailable - Simultaneous playback is unavailable: You can play only one sound at a time iphone ( ) - AudioServicesPlaySystemSound ksystemsoundid_vibrate System Sound Service(3/3) AudioServicesPlaySystemSound 1. sound ID object 2. then,. : SysSound tap.aif.
Xcode_ Xcode file-new Project View-based Application choose AudioRecorder Save AudioRecorder Interface Builder SysSoundViewController.xib AudioToolbox.framework Xcode_SysSoundViewController.h Classes 4 SysSoundViewController.h #import <UIKit/UIKit.h> #include <AudioToolbox/AudioToolbox.h> @interface SysSoundViewController : UIViewController { CFURLRef soundfileurlref; SystemSoundID soundfileobject; @property (readwrite) CFURLRef soundfileurlref; @property (readonly) SystemSoundIDsoundFileObject; //UI - (IBAction) playsystemsound: (id) sender; - (IBAction) playalertsound: (id) sender; - (IBAction) vibrate: (id) sender; @end :
Xcode_SysSoundViewController.m Classes 4 SysSoundViewController.m 1. viewdidload{ #import "SysSoundViewController.h" @implementation SysSoundViewController @synthesize soundfileurlref; @synthesize soundfileobject; - (void) viewdidload { [super viewdidload]; // Provide a nice background for the app user interface.( nice background) self.view.backgroundcolor = [UIColor grouptableviewbackgroundcolor]; // URL NSURL *tapsound = [[NSBundle mainbundle] URLForResource: @"tap" withextension: @"aif"]; // CFURLRef instance URL self.soundfileurlref = (CFURLRef) [tapsound retain]; // AudioServicesCreateSystemSoundID ( ); soundfileurlref, &soundfileobject Xcode_SysSoundViewController.m 2. // System Sound button - (IBAction) playsystemsound: (id) sender { AudioServicesPlaySystemSound (soundfileobject); // Alert Sound button - (IBAction) playalertsound: (id) sender { AudioServicesPlayAlertSound (soundfileobject); // Vibrate button, - (IBAction) vibrate: (id) sender { AudioServicesPlaySystemSound (ksystemsoundid_vibrate); // - (void) dealloc { AudioServicesDisposeSystemSoundID (soundfileobject); CFRelease (soundfileurlref); [super dealloc];
Xcode Interface Builder & SysSoundViewController.xib Interface Builder 1. View File s Owner. 2. xib File s Owner Outlet. 3. System Sound, Alert Sound Vibration. Xcode. - wav ( test.wav )