实例编程iPhone 录音和播放
实例编程iPhone 录音和播放是本文要介绍的内容,最近准备做一个关于录音和播放的项目!查了一些资料,很简单的做了一个,下面我就分享一下iPhone的录音和播放的使用心得。iPhone的录音和播放使用到了media层的内容,media层处于cocoa层之下,用到的很大一部分都是c语言的结构。1、引入框架。
#import <AVFoundation/AVFoundation.h>
2、创建录音项。
[*]- (void) prepareToRecord
[*]
[*]{
[*]
[*]AVAudioSession *audioSession = ;
[*]
[*]NSError *err = nil;
[*]
[*];
[*]
[*]if(err){
[*]
[*] NSLog(@&quot;audioSession: %@ %d %@&quot;, , , [ description]);
[*]
[*] return;
[*]
[*]}
[*]
[*];
[*]
[*]err = nil;
[*]
[*]if(err){
[*]
[*] NSLog(@&quot;audioSession: %@ %d %@&quot;, , , [ description]);
[*]
[*] return;
[*]
[*]}
[*]
[*]recordSetting = [ init];
[*]
[*] forKey:AVFormatIDKey];
[*]
[*] forKey:AVSampleRateKey];
[*]
[*] forKey:AVNumberOfChannelsKey];
[*]
[*] forKey:AVLinearPCMBitDepthKey];
[*]
[*] forKey:AVLinearPCMIsBigEndianKey];
[*]
[*] forKey:AVLinearPCMIsFloatKey];
[*]
[*]// Create a new dated file
[*]NSDate *now = ;
[*]NSString *caldate = ;
[*]recorderFilePath = [ retain];
[*]NSURL *url = ;
[*]err = nil;
[*]recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
[*]if(!recorder){
[*] NSLog(@&quot;recorder: %@ %d %@&quot;, , , [ description]);
[*] UIAlertView *alert =
[*] [ initWithTitle: @&quot;Warning&quot;
[*]message:
[*]delegate: nil
[*]cancelButtonTitle:@&quot;OK&quot;
[*]otherButtonTitles:nil];
[*] ;
[*] ;
[*] return;
[*]}
[*]//prepare to record
[*];
[*];
[*]recorder.meteringEnabled = YES;
[*]BOOL audioHWAvailable = audioSession.inputIsAvailable;
[*]if (! audioHWAvailable) {
[*] UIAlertView *cantRecordAlert =
[*] [ initWithTitle: @&quot;Warning&quot;
[*]message: @&quot;Audio input hardware not available&quot;
[*]delegate: nil
[*]cancelButtonTitle:@&quot;OK&quot;
[*]otherButtonTitles:nil];
[*] ;
[*] ;
[*] return;
[*]}
[*]}
以上这个方法就是创建了录音项,其中包括录音的路径和一些音频属性,但只是准备录音还没有录,如果要录的话还要加入以下的方法:
[*](void)startrecorder
[*]{
[*];
[*]}
这样就在我们创建的路径下开始了录音。完成录音很简单:
[*](void) stopRecording{
[*];
[*]}
这里顺便提一下录音的代理方法:
[*]- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully:(BOOL)flag
[*]{
[*]NSLog(@&quot;recorder successfully&quot;);
[*]UIAlertView *recorderSuccessful = [ initWithTitle:@&quot;&quot; message:@&quot;录音成功&quot;
[*]delegate:self cancelButtonTitle:@&quot;OK&quot; otherButtonTitles:nil];
[*];
[*];
[*]}
[*]
[*]- (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)arecorder error:(NSError *)error
[*]{
[*]btnRecorder.enabled = NO;
[*]UIAlertView *recorderFailed = [ initWithTitle:@&quot;&quot; message:@&quot;发生错误&quot;
[*]delegate:self cancelButtonTitle:@&quot;OK&quot; otherButtonTitles:nil];
[*];
[*];
[*]}
以上两个代理方法分别指定了录音的成功或失败。
录音中有一个的录音对象有一个averagePowerForChannel和peakPowerForChannel的属性分别为声音的最高振幅和平均振幅,有了他们就可以做一个动态的振幅的录音效果。
[*]- (void) updateAudioDisplay {
[*]
[*]if (isStart == NO) {
[*]
[*]currentTimeLabel.text = @&quot;--:--&quot;;
[*]
[*]} else {
[*]
[*]double currentTime = recorder.currentTime;
[*]
[*]currentTimeLabel.text = [NSString stringWithFormat: @&quot;d:d&quot;,
[*]
[*](int) currentTime/60,
[*]
[*](int) currentTime%60];
[*]
[*]//START:code.RecordViewController.setlevelmeters
[*]
[*];
[*]
[*]
[*]
[*]peak: ];
[*]
[*]if (! rightLevelMeter.hidden) {
[*]
[*]
[*]
[*]peak: ];
[*]
[*]}
[*]
[*]//END:code.RecordViewController.setlevelmeters
[*]
[*]}
[*]
[*]}
[*]
[*]以上就是录音相关的内容。
[*]
[*]下面说一下播放的方法:
[*]
[*]void SystemSoundsDemoCompletionProc (
[*]SystemSoundIDsoundID,
[*]void *clientData)
[*]{
[*]AudioServicesDisposeSystemSoundID (soundID);
[*]((AudioRecorderPlayerAppDelegate*)clientData).statusLabel.text = @&quot;Stopped&quot;;
[*]}
[*]-(void)playAudio
[*]{
[*]//START:code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound
[*]// create a system sound id for the selected row
[*]SystemSoundID soundID;
[*]OSStatus err = kAudioServicesNoError;
[*]// special case: vibrate//震动
[*]//soundID = kSystemSoundID_Vibrate; //<label id=&quot;code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.vibratesystemsound&quot;/>
[*]
[*]// find corresponding CAF file
[*]
[*]//NSString *cafName = ; //<label id=&quot;code.SystemSoundsDemo.
[*]SystemSoundsDemoViewController.createsystemsound.rowtonumberstring&quot;/>
[*]
[*]NSURL *url = ;
[*]//NSString *cafPath =
[*]//[ pathForResource:cafName ofType:@&quot;caf&quot;]; //<label id=&quot;code.SystemSoundsDemo.
[*]SystemSoundsDemoViewController.createsystemsound.findcafinbundle&quot;/>
[*]//NSURL *cafURL = ; //<label id=&quot;code.SystemSoundsDemo.SystemSoundsDemoViewController.
[*]createsystemsound.fileurlwithpath&quot;/>
[*]err = AudioServicesCreateSystemSoundID((CFURLRef) url, &soundID); //<label id=&quot;code.SystemSoundsDemo.
[*]SystemSoundsDemoViewController.createsystemsound.createsystemsound&quot;/>
[*]//END:code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound
[*]//START:code.SystemSoundsDemo.SystemSoundsDemoViewController.registercompletionprocandplaysound
[*]if (err == kAudioServicesNoError) {
[*]
[*]// set up callback for sound completion
[*]err = AudioServicesAddSystemSoundCompletion //<label id=&quot;code.SystemSoundsDemo.SystemSoundsDemoViewController.
[*]createsystemsound.addcompletionproc&quot;/>
[*](soundID,// sound to monitor
[*]NULL,// run loop (NULL==main)
[*]NULL,// run loop mode (NULL==default)
[*]SystemSoundsDemoCompletionProc, // callback function //<label id=&quot;code.SystemSoundsDemo.
[*]SystemSoundsDemoViewController.createsystemsound.completionprocroutine&quot;/>
[*]self // data to provide on callback
[*]); //<label id=&quot;code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.addcompletionprocend&quot;/>
[*]statusLabel.text = @&quot;Playing&quot;; //<label id=&quot;code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.setlabel&quot;/>
[*]AudioServicesPlaySystemSound (soundID); //<label id=&quot;code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.playsound&quot;/>
[*]}
[*]if (err != kAudioServicesNoError) { //<label id=&quot;code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.errorblockstart&quot;/>
[*]CFErrorRef error = CFErrorCreate(NULL, kCFErrorDomainOSStatus, err, NULL); //<label id=&quot;code.SystemSoundsDemo.
[*]SystemSoundsDemoViewController.createsystemsound.createcferror&quot;/>
[*]NSString *errorDesc = (NSString*) CFErrorCopyDescription (error); //<label id=&quot;code.SystemSoundsDemo.
[*]SystemSoundsDemoViewController.createsystemsound.copycferrordescription&quot;/>
[*]UIAlertView *cantPlayAlert =
[*][ initWithTitle:@&quot;Cannot Play:&quot;
[*]message: errorDesc
[*]delegate:nil
[*]cancelButtonTitle:@&quot;OK&quot;
[*]otherButtonTitles:nil];
[*];
[*];
[*]; //<label id=&quot;code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.releaseerrordescription&quot;/>
[*]CFRelease (error); //<label id=&quot;code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.releaseerror&quot;/>
[*]} //<label id=&quot;code.SystemSoundsDemo.SystemSoundsDemoViewController.createsystemsound.errorblockend&quot;/>
[*]//END:code.SystemSoundsDemo.SystemSoundsDemoViewController.registercompletionprocandplaysound
[*]}
通过以上的方法就应该能够实现播放,播放的时候也是可以加入振幅过程的,大家可以试试!这样一个iPhone录音机就做好了!哈哈
小结:实例编程iPhone 录音和播放的内容介绍完了,希望本文对你有所帮助。
【编辑推荐】
[*]如何使用iPhone 播放 MIDI 文件方法
[*]详解iPhone真机调试教程 图文并茂
[*]浅谈iPhone 中Push 功能原理 推送通知
[*]深度解析iPhone多任务需求
[*]详解iPhone SDK 开发之 UIKit 使用
页:
[1]