我正在开发一个应用程序,因为我想记录用户语音,因为我已经在NSTemporaryDirectory()中记录了语音但是在记录联盟后我想将记录的.caf文件保存在数据库中并从数据库中检索该文件并播放该文件,以帮助我... 我喜欢这样......
-(IBAction)recordbutton:(id)sender
{
if(toggle)
{
toggle = NO;
[btnStart setImage:[UIImage imageNamed:@"stopbtn.png"] forState:UIControlStateNormal];
label1.text = @"Speak now";
dotimageview.image = [UIImage imageNamed:@"record_dot.png"];
timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(hideLabel:) userInfo:nil repeats:YES];
btnPlay.enabled = toggle;
btnPlay.hidden = !toggle;
//Begin the recording session.
//Setup the dictionary object with all the recording settings that this
//This is a good resource: http://www.totodotnet.net/tag/avaudiorecorder/
NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
//Now that we have our settings we are going to instanciate an instance of our recorder instance.
//Generate a temp file for use by the recording.
//This sample was one I found online and seems to be a good choice for making a tmp file that
// recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]];
recordedTmpFile = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]];
NSLog(@"Using File called: %@",recordedTmpFile);
//Setup the recorder to use this file and record to it.
recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
//Use the recorder to start the recording.
[recorder setDelegate:self];
[recorder prepareToRecord];
//Start the actual Recording
[recorder record];
//There is an optional method for doing the recording for a limited time see
//[recorder recordForDuration:(NSTimeInterval) 29];
}
else
{
toggle = YES;
label1.text = @"";
btnPlay.enabled = toggle;
btnPlay.hidden = !toggle;
// stop the timer
[timer invalidate];
timer = nil;
dotimageview.hidden = YES;
label1.text = @"Listen";
self.navigationItem.rightBarButtonItem.enabled = YES;
NSLog(@"Using File called: %@",recordedTmpFile);
NSString *soundFile = [NSString stringWithFormat:@"%@",recordedTmpFile];
NSLog(@"str === %@",soundFile);
//Stop the recorder.
[recorder stop];
}
}
答案 0 :(得分:0)
考虑将文件从临时文件夹移动到用户文档区域,并仅在SQLite中存储文件名。 SQLite不是设计为二进制blob存储区。您将避免在SQLite中存储大型二进制文件时遇到任何内存使用问题。