任何人都可以告诉我哪个是ios应用程序中录音的最佳录音功能......是否有任何第三方api可以很好地处理任何噪音捕捉......我试过Novocain但是没有为我工作。可能我做错了。请帮我解决这个问题。
答案 0 :(得分:0)
我不知道,天气是最好的还是最糟糕的,但它对我有用,而且满足了我的要求。
#import "RecoderViewController.h"
#import <AVFoundation/AVFoundation.h>
#import<AudioToolbox/AudioToolbox.h>
#import "PhotoViewController.h"
#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
@interface RecoderViewController ()
{
NSMutableDictionary *recordSetting;
NSString *recorderFilePath;
AVAudioRecorder *recorder;
NSMutableDictionary *editedObject;
NSTimer *timer;
int i,j;
NSString *str;
}
@end
@implementation RecoderViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
i=00;
j=00;
str = @"reco";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)clicktorecord:(id)sender {
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
self.imageRecord.hidden = false;
self.btnStart.hidden = true;
self.btnStop.hidden =false;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err){
NSLog(@"audioSession: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
return;
}
[audioSession setActive:YES error:&err];
err = nil;
if(err){
NSLog(@"audioSession: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
return;
}
recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
// Create a new dated file
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
NSString *caldate = [now description];
recorderFilePath = [NSString stringWithFormat:@"%@/%@.caf", DOCUMENTS_FOLDER, caldate];
NSURL *url = [NSURL fileURLWithPath:recorderFilePath];
err = nil;
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
if(!recorder){
NSLog(@"recorder: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: @"Warning"
message: [err localizedDescription]
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
//prepare to record
[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
BOOL audioHWAvailable = audioSession.inputIsAvailable;
if (! audioHWAvailable) {
UIAlertView *cantRecordAlert =
[[UIAlertView alloc] initWithTitle: @"Warning"
message: @"Audio input hardware not available"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[cantRecordAlert show];
}
// start recording
[recorder recordForDuration:(NSTimeInterval) 10];
}
-(void)updateCountdown{
i++;
if (i>60) {
j=j+1;
i=0;
}
self.lblTimer.text =[NSString stringWithFormat:@"%d : %d",j,i];
}
- (IBAction)ClicktoStop:(id)sender {
self.btnStart.hidden =false;
self.btnStop.hidden =true;
self.imageRecord.hidden = true;
[timer invalidate];
[recorder stop];
NSURL *url = [NSURL fileURLWithPath: recorderFilePath];
NSError *err = nil;
NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
if(!audioData)
NSLog(@"audio data: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
[editedObject setValue:[NSData dataWithContentsOfURL:url] forKey:@"editedFieldKey"];
[self performSegueWithIdentifier:@"gotoshare" sender:self];
}
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully:(BOOL)flag
{
NSLog (@"audioRecorderDidFinishRecording:successfully:");
}
答案 1 :(得分:0)
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController<AVAudioRecorderDelegate,AVAudioPlayerDelegate> {
AVAudioRecorder *recorder;
AVAudioPlayer *player;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
@"youraudiofile.m4a",
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
//setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
//defining recorder settings
NSMutableDictionary *recorderSettings = [[NSMutableDictionary alloc]init];
[recorderSettings setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recorderSettings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recorderSettings setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
//Initiate and preparing recorder
recorder = [[AVAudioRecorder alloc]initWithURL:outputFileURL settings:recorderSettings error:NULL];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
}
-(IBAction)btnVoiceRecordClicked:(id)sender {
if (player.playing) {
[player stop];
}
if (!recorder.recording) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[session setActive:YES error:nil];
//start recording
[recorder record];
}
}
-(IBAction)btnStopRecordClicked:(id)sender {
[recorder stop];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
}
-(IBAction)btnPlayRecordClicked:(id)sender {
if (!recorder.recording) {
NSError *error = nil;
player = [[AVAudioPlayer alloc]initWithContentsOfURL:recorder.url error:&error];
player.delegate = self;
[player play];
if (error != nil) {
NSLog(@"%@",error.localizedDescription);
}
}
}