0

我正在开发一个应用程序,我想记录用户的声音,我已经在 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];


    }

}
4

1 回答 1

0

考虑将文件从临时文件夹移动到用户文档区域,并仅将文件名存储在 SQLite 中。SQLite 并非设计为二进制 blob 存储。在 SQLite 中存储大型二进制文件时,您将避免任何内存使用问题。

于 2012-11-14T13:16:57.753 回答