2012-06-26 19 views
0

我正在開發一個應用程序,因爲我想記錄用戶聲音,因爲我在NSTemporaryDirectory()中記錄了聲音,但是在記錄聯盟之後,我想要保存記錄文件的.caf在數據庫,並從數據庫中檢索該文件並播放該文件,以便幫助我...... 我做這樣的...我們如何保存在sqlite3數據庫中的AudioRecorded文件(.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

考慮將文件從臨時文件夾移動到用戶文檔區域,並且只存儲SQLite中的文件名。 SQLite的設計不是一個二進制BLOB存儲。您將避免任何在SQLite中存儲大型二進制文件的內存使用問題。

相關問題