自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

實(shí)例編程iPhone 錄音和播放

移動開發(fā) iOS
本文介紹的是實(shí)例編程iPhone 錄音和播放,本文幫友們實(shí)現(xiàn)一個錄音的效果,很有趣,我們一起來看!

實(shí)例編程iPhone 錄音播放是本文要介紹的內(nèi)容,最近準(zhǔn)備做一個關(guān)于錄音播放的項(xiàng)目!查了一些資料,很簡單的做了一個,下面我就分享一下iPhone錄音播放的使用心得。iPhone的錄音和播放使用到了media層的內(nèi)容,media層處于cocoa層之下,用到的很大一部分都是c語言的結(jié)構(gòu)。

1、引入框架。

#import <AVFoundation/AVFoundation.h>

2、創(chuàng)建錄音項(xiàng)。

  1. - (void) prepareToRecord  
  2.  
  3. {  
  4.  
  5. AVAudioSession *audioSession = [AVAudioSession sharedInstance];  
  6.  
  7. NSError *err = nil;  
  8.  
  9. [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];  
  10.  
  11. if(err){  
  12.  
  13.         NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);  
  14.  
  15.         return;  
  16.  
  17. }  
  18.  
  19. [audioSession setActive:YES error:&err];  
  20.  
  21. err = nil;  
  22.  
  23. if(err){  
  24.  
  25.         NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);  
  26.  
  27.         return;  
  28.  
  29. }  
  30.  
  31. recordSetting = [[NSMutableDictionary alloc] init];  
  32.  
  33. [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];  
  34.  
  35. [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];   
  36.  
  37. [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];  
  38.  
  39. [recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];  
  40.  
  41. [recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];  
  42.  
  43. [recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];  
  44.  
  45. // Create a new dated file  
  46. NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];  
  47. NSString *caldate = [now description];  
  48. recorderFilePath = [[NSString stringWithFormat:@"%@/%@.caf", DOCUMENTS_FOLDER, caldate] retain];  
  49. NSURL *url = [NSURL fileURLWithPath:recorderFilePath];  
  50. err = nil;  
  51. recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];  
  52. if(!recorder){  
  53.         NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);  
  54.         UIAlertView *alert =  
  55.         [[UIAlertView alloc] initWithTitle: @"Warning"  
  56.   message: [err localizedDescription]  
  57.   delegate: nil  
  58. cancelButtonTitle:@"OK"  
  59. otherButtonTitles:nil];  
  60.         [alert show];  
  61.         [alert release];  
  62.         return;  
  63. }  
  64. //prepare to record  
  65. [recorder setDelegate:self];  
  66. [recorder prepareToRecord];  
  67. recorder.meteringEnabled = YES;  
  68. BOOL audioHWAvailable = audioSession.inputIsAvailable;  
  69. if (! audioHWAvailable) {  
  70.         UIAlertView *cantRecordAlert =  
  71.         [[UIAlertView alloc] initWithTitle: @"Warning"  
  72.   message: @"Audio input hardware not available"  
  73.   delegate: nil  
  74. cancelButtonTitle:@"OK"  
  75. otherButtonTitles:nil];  
  76.         [cantRecordAlert show];  
  77.         [cantRecordAlert release];   
  78.         return;  
  79. }  

以上這個方法就是創(chuàng)建了錄音項(xiàng),其中包括錄音的路徑和一些音頻屬性,但只是準(zhǔn)備錄音還沒有錄,如果要錄的話還要加入以下的方法:

  1. (void)startrecorder  
  2. {  
  3. [recorder record];  

這樣就在我們創(chuàng)建的路徑下開始了錄音。完成錄音很簡單:

  1. (void) stopRecording{  
  2. [recorder stop];  

這里順便提一下錄音的代理方法:

  1. - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully:(BOOL)flag  
  2. {  
  3. NSLog(@"recorder successfully");  
  4. UIAlertView *recorderSuccessful = [[UIAlertView alloc] initWithTitle:@"" message:@"錄音成功"
  5. delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  
  6. [recorderSuccessful show];  
  7. [recorderSuccessful release];  
  8. }  
  9.  
  10. - (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)arecorder error:(NSError *)error  
  11. {  
  12. btnRecorder.enabled = NO;  
  13. UIAlertView *recorderFailed = [[UIAlertView alloc] initWithTitle:@"" message:@"發(fā)生錯誤"
  14. delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  
  15. [recorderFailed show];  
  16. [recorderFailed release];  

以上兩個代理方法分別指定了錄音的成功或失敗。

錄音中有一個的錄音對象有一個averagePowerForChannel和peakPowerForChannel的屬性分別為聲音的最高振幅和平均振幅,有了他們就可以做一個動態(tài)的振幅的錄音效果。

  1. - (void) updateAudioDisplay {  
  2.  
  3. if (isStart == NO) {  
  4.  
  5. currentTimeLabel.text = @"--:--";  
  6.  
  7. } else {  
  8.  
  9. double currentTime = recorder.currentTime;  
  10.  
  11. currentTimeLabel.text = [NSString stringWithFormat: @"d:d",  
  12.  
  13. (int) currentTime/60,  
  14.  
  15. (int) currentTime%60];  
  16.  
  17. //START:code.RecordViewController.setlevelmeters  
  18.  
  19. [recorder updateMeters];  
  20.  
  21. [leftLevelMeter setPower: [recorder averagePowerForChannel:0]  
  22.  
  23. peak: [recorder peakPowerForChannel: 0]];  
  24.  
  25. if (! rightLevelMeter.hidden) {  
  26.  
  27. [rightLevelMeter setPower: [recorder averagePowerForChannel:1]  
  28.  
  29. peak: [recorder peakPowerForChannel: 1]];  
  30.  
  31. }  
  32.  
  33. //END:code.RecordViewController.setlevelmeters  
  34.  
  35. }  
  36.  
  37. }  
  38.  
  39. 以上就是錄音相關(guān)的內(nèi)容。  
  40.  
  41. 下面說一下播放的方法:  
  42.  
  43. void SystemSoundsDemoCompletionProc (  
  44. SystemSoundID  soundID,  
  45. void           *clientData)  
  46. {  
  47. AudioServicesDisposeSystemSoundID (soundID);  
  48. ((AudioRecorderPlayerAppDelegate*)clientData).statusLabel.text = @"Stopped";  
  49. }  
  50. -(void)playAudio  
  51. {  
  52. //START:code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound  
  53. // create a system sound id for the selected row  
  54. SystemSoundID soundID;  
  55. OSStatus err = kAudioServicesNoError;  
  56. // special case: vibrate//震動  
  57. //soundID = kSystemSoundID_Vibrate; //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.vibratesystemsound"/> 
  58.  
  59. // find corresponding CAF file  
  60.  
  61. //NSString *cafName = [NSString stringWithFormat: @"%@",recorderFilePath]; //<label id="code.SystemSoundsDemo.
  62. SystemSoundsDemoViewController.createsystemsound.rowtonumberstring"/> 
  63.  
  64. NSURL *url = [NSURL fileURLWithPath:recorderFilePath];  
  65. //NSString *cafPath =   
  66. //[[NSBundle mainBundle] pathForResource:cafName ofType:@"caf"]; //<label id="code.SystemSoundsDemo.
  67. SystemSoundsDemoViewController.createsystemsound.findcafinbundle"/> 
  68. //NSURL *cafURL = [NSURL fileURLWithPath:url]; //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.
  69. createsystemsound.fileurlwithpath"/> 
  70. err = AudioServicesCreateSystemSoundID((CFURLRef) url, &soundID); //<label id="code.SystemSoundsDemo.
  71. SystemSoundsDemoViewController.createsystemsound.createsystemsound"/> 
  72. //END:code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound  
  73. //START:code.SystemSoundsDemo.SystemSoundsDemoViewController.registercompletionprocandplaysound  
  74. if (err == kAudioServicesNoError) {  
  75.  
  76. // set up callback for sound completion  
  77. err = AudioServicesAddSystemSoundCompletion //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.
  78. createsystemsound.addcompletionproc"/> 
  79. (soundID,// sound to monitor  
  80. NULL,// run loop (NULL==main)  
  81. NULL,// run loop mode (NULL==default)  
  82. SystemSoundsDemoCompletionProc, // callback function //<label id="code.SystemSoundsDemo.
  83. SystemSoundsDemoViewController.createsystemsound.completionprocroutine"/> 
  84. self // data to provide on callback  
  85. ); //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.addcompletionprocend"/> 
  86. statusLabel.text = @"Playing"; //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.setlabel"/> 
  87. AudioServicesPlaySystemSound (soundID); //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.playsound"/> 
  88. }  
  89. if (err != kAudioServicesNoError) { //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.errorblockstart"/> 
  90. CFErrorRef error = CFErrorCreate(NULL, kCFErrorDomainOSStatus, err, NULL); //<label id="code.SystemSoundsDemo.
  91. SystemSoundsDemoViewController.createsystemsound.createcferror"/> 
  92. NSString *errorDesc = (NSString*) CFErrorCopyDescription (error); //<label id="code.SystemSoundsDemo.
  93. SystemSoundsDemoViewController.createsystemsound.copycferrordescription"/> 
  94. UIAlertView *cantPlayAlert =  
  95. [[UIAlertView alloc] initWithTitle:@"Cannot Play:"  
  96.   message: errorDesc  
  97.   delegate:nil  
  98. cancelButtonTitle:@"OK"  
  99. otherButtonTitles:nil];  
  100. [cantPlayAlert show];  
  101. [cantPlayAlert release];   
  102. [errorDesc release]; //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.releaseerrordescription"/> 
  103. CFRelease (error); //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.releaseerror"/> 
  104. } //<label id="code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.errorblockend"/> 
  105. //END:code.SystemSoundsDemo.SystemSoundsDemoViewController.registercompletionprocandplaysound  

通過以上的方法就應(yīng)該能夠?qū)崿F(xiàn)播放,播放的時候也是可以加入振幅過程的,大家可以試試!這樣一個iPhone錄音機(jī)就做好了!哈哈

小結(jié):實(shí)例編程iPhone 錄音和播放的內(nèi)容介紹完了,希望本文對你有所幫助。

責(zé)任編輯:zhaolei 來源: 博客園
相關(guān)推薦

2021-07-09 09:24:41

鴻蒙HarmonyOS應(yīng)用

2011-07-26 15:56:53

iPhone 游戲 啟動畫面

2011-08-18 10:32:13

iPhone編程視圖

2011-07-28 14:19:12

iPhone 網(wǎng)絡(luò)編程 聊天程序

2011-07-26 11:08:23

iOS 錄像 錄音

2016-12-21 16:42:15

androidmediaplayer

2011-08-10 15:58:58

iPhone視頻

2011-07-25 18:02:51

iPhone LibFetion 移植

2012-06-21 09:28:47

jQuery

2009-07-09 00:25:00

ScalaListTuple

2009-07-09 00:25:00

ScalaSet類Map類

2011-07-21 16:48:19

iPhone 游戲

2011-08-08 16:56:44

iPhone 字符處理 視圖

2009-09-07 20:40:48

LINQ子查詢

2011-07-27 09:50:31

iPhone AVAudioPla 音頻

2011-07-20 16:21:20

iPhone 視頻 播放器

2011-08-02 16:58:15

iPhone AVAudioPla 音頻播放

2011-08-17 14:57:31

iPhone應(yīng)用視頻播放

2011-08-08 18:19:09

iPhone音頻播放

2011-07-08 20:32:57

iPhone midi
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號