ios录音功能的实现

这两天也调了一下ios的录音,原文链接:http://www.iphoneam.com/blog
/index.php?title=using-the-iphone-to-record-audio-a-guide&more=1&c=1&tb=1&pb=1

这里ios的录音功能主要依靠AVFoundation.framework与CoreAudio.framework来实现


在工程内添加这两个framework

我这里给工程命名audio_text

在生成的audio_textViewController.h里的代码如下

view plain

  1. #import <UIKit/UIKit.h>  
  2. #import <AVFoundation/AVFoundation.h>  
  3. #import <CoreAudio/CoreAudioTypes.h>  
  4.   
  5. @interface audio_textViewController : UIViewController {  
  6.   
  7.     IBOutlet UIButton *bthStart;  
  8.     IBOutlet UIButton *bthPlay;  
  9.     IBOutlet UITextField *freq;  
  10.     IBOutlet UITextField *value;  
  11.     IBOutlet UIActivityIndicatorView *actSpinner;  
  12.     BOOL toggle;  
  13.       
  14.     //Variable setup for access in the class  
  15.     NSURL *recordedTmpFile;  
  16.     AVAudioRecorder *recorder;  
  17.     NSError *error;  
  18. }  
  19.   
  20. @property (nonatomic,retain)IBOutlet UIActivityIndicatorView *actSpinner;  
  21. @property (nonatomic,retain)IBOutlet UIButton *bthStart;  
  22. @property (nonatomic,retain)IBOutlet UIButton *bthPlay;  
  23.   
  24. -(IBAction)start_button_pressed;  
  25. -(IBAction)play_button_pressed;  
  26. @end  
audio_textViewController.m

view plain

  1. #import "audio_textViewController.h"  
  2.   
  3. @implementation audio_textViewController  
  4.   
  5.   
  6. - (void)viewDidLoad {  
  7.     [super viewDidLoad];  
  8.       
  9.     //Start the toggle in true mode.  
  10.     toggle = YES;  
  11.     bthPlay.hidden = YES;  
  12.       
  13.     //Instanciate an instance of the AVAudioSession object.  
  14.     AVAudioSession * audioSession = [AVAudioSession sharedInstance];  
  15.     //Setup the audioSession for playback and record.   
  16.     //We could just use record and then switch it to playback leter, but  
  17.     //since we are going to do both lets set it up once.  
  18.     [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];  
  19.     //Activate the session  
  20.     [audioSession setActive:YES error: &error];  
  21.       
  22. }  
  23. /* 
  24. // The designated initializer. Override to perform setup that is required before the view is loaded. 
  25. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
  26.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
  27.     if (self) { 
  28.         // Custom initialization 
  29.     } 
  30.     return self; 
  31. */  
  32.   
  33. /* 
  34. // Implement loadView to create a view hierarchy programmatically, without using a nib. 
  35. - (void)loadView { 
  36. */  
  37.   
  38.   
  39. /* 
  40. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
  41. - (void)viewDidLoad { 
  42.     [super viewDidLoad]; 
  43. */  
  44.   
  45.   
  46. /* 
  47. // Override to allow orientations other than the default portrait orientation. 
  48. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
  49.     // Return YES for supported orientations 
  50.     return (interfaceOrientation == UIInterfaceOrientationPortrait); 
  51. */  
  52.   
  53. - (IBAction)  start_button_pressed{  
  54.       
  55.     if(toggle)  
  56.     {  
  57.         toggle = NO;  
  58.         [actSpinner startAnimating];  
  59.         [bthStart setTitle:@"停" forState: UIControlStateNormal ];     
  60.         bthPlay.enabled = toggle;  
  61.         bthPlay.hidden = !toggle;  
  62.           
  63.         //Begin the recording session.  
  64.         //Error handling removed.  Please add to your own code.  
  65.           
  66.         //Setup the dictionary object with all the recording settings that this   
  67.         //Recording sessoin will use  
  68.         //Its not clear to me which of these are required and which are the bare minimum.  
  69.         //This is a good resource: http://www.totodotnet.net/tag/avaudiorecorder/  
  70.         NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];  
  71.           
  72.         [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];  
  73.           
  74.         [recordSetting setValue:[NSNumber numberWithFloat:[freq.text floatValue]] forKey:AVSampleRateKey];   
  75.         [recordSetting setValue:[NSNumber numberWithInt: [value.text intValue]] forKey:AVNumberOfChannelsKey];  
  76.           
  77.         //Now that we have our settings we are going to instanciate an instance of our recorder instance.  
  78.         //Generate a temp file for use by the recording.  
  79.         //This sample was one I found online and seems to be a good choice for making a tmp file that  
  80.         //will not overwrite an existing one.  
  81.         //I know this is a mess of collapsed things into 1 call.  I can break it out if need be.  
  82.         recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]];  
  83.         NSLog(@"Using File called: %@",recordedTmpFile);  
  84.         //Setup the recorder to use this file and record to it.  
  85.         recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];  
  86.         //Use the recorder to start the recording.  
  87.         //Im not sure why we set the delegate to self yet.    
  88.         //Found this in antother example, but Im fuzzy on this still.  
  89.         [recorder setDelegate:self];  
  90.         //We call this to start the recording process and initialize   
  91.         //the subsstems so that when we actually say "record" it starts right away.  
  92.         [recorder prepareToRecord];  
  93.         //Start the actual Recording  
  94.         [recorder record];  
  95.         //There is an optional method for doing the recording for a limited time see   
  96.         //[recorder recordForDuration:(NSTimeInterval) 10]  
  97.           
  98.     }  
  99.     else  
  100.     {  
  101.         toggle = YES;  
  102.         [actSpinner stopAnimating];  
  103.         [bthStart setTitle:@"开始录音" forState:UIControlStateNormal ];  
  104.         bthPlay.enabled = toggle;  
  105.         bthPlay.hidden = !toggle;  
  106.           
  107.         NSLog(@"Using File called: %@",recordedTmpFile);  
  108.         //Stop the recorder.  
  109.         [recorder stop];  
  110.     }  
  111. }  
  112.   
  113. - (void)didReceiveMemoryWarning {  
  114.     // Releases the view if it doesn't have a superview.  
  115.     [super didReceiveMemoryWarning];  
  116.       
  117.     // Release any cached data, images, etc that aren't in use.  
  118. }  
  119.   
  120. -(IBAction) play_button_pressed{  
  121.       
  122.     //The play button was pressed...   
  123.     //Setup the AVAudioPlayer to play the file that we just recorded.  
  124.     AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];  
  125.     [avPlayer prepareToPlay];  
  126.     [avPlayer play];  
  127.       
  128. }  
  129.   
  130. - (void)viewDidUnload {  
  131.     // Release any retained subviews of the main view.  
  132.     // e.g. self.myOutlet = nil;  
  133.     //Clean up the temp file.  
  134.     NSFileManager * fm = [NSFileManager defaultManager];  
  135.     [fm removeItemAtPath:[recordedTmpFile path] error:&error];  
  136.     //Call the dealloc on the remaining objects.  
  137.     [recorder dealloc];  
  138.     recorder = nil;  
  139.     recordedTmpFile = nil;  
  140. }  
  141.   
  142.   
  143. - (void)dealloc {  
  144.     [super dealloc];  
  145. }  
  146.   
  147. @end  
最后在interface builder里面绘制好界面,如


设置下按键的属性


基本就ok了,可以开始录音了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值