繁体   English   中英

如何在iPhone中保存录音文件

[英]how to save the audio recording file in iphone

我已经在iPhone中实现了录音功能,但是在保存录音文件时遇到了问题,并且对检索录音文件感到困惑。

这是我的源代码

-(void)Record
{

delayTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                      target:self
                                                    selector:@selector(delayTimerFired:)
                                                    userInfo:nil
                                                     repeats:YES];
        //toggle = NO;
        NSMutableDictionary *rs = [[NSMutableDictionary alloc] init];
        [rs setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
        [rs setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
        [rs setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];


        recordedTmpFile = [[NSURL alloc] initFileURLWithPath:[NSString stringWithFormat:@"%@/Record.caf", documentsDirectory]];

        NSLog(@"%@", recordedTmpFile);

        recorder = [[AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:rs error:&error];
        [recorder setDelegate:self];
        [recorder prepareToRecord];
        [recorder record];
}

而检索代码是

-(void)RetriveRecordFile
{
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];

    NSFileManager *fm = [NSFileManager defaultManager];
    NSArray *contents = [fm contentsOfDirectoryAtPath:docDir error:nil];
    NSLog(@"*******%@", contents);
    NSString *fileFormat = @"caf";

    NSMutableArray *lRecord = [[NSMutableArray alloc] init];
    NSString *lRecordFile;
    for (lRecordFile in contents)
    {
        if ([[lRecordFile pathExtension] isEqualToString:fileFormat])
        {
            NSLog(@"%@", lRecordFile);
            [lRecord addObject:lRecordFile];
        }
    }
}

这是用于记录音频的代码,单击停止按钮后,音频记录器停止并存储音频文件。

-(IBAction)startRecording
{ 
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH.mm.SS"];

NSDate *now = [[NSDate alloc] init];

theDate = [dateFormat stringFromDate:now];
theTime = [timeFormat stringFromDate:now];

NSString *filename =[NSString stringWithFormat:@"|%@|%@|",theDate,theTime];
theDate=[[NSString alloc]initWithString:filename];

[dateFormat release];
[timeFormat release];
[now release];

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];

NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];
if(recordEncoding == ENC_PCM4)
{
    [recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
    [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
    [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
    [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];   

}
else
{
    NSNumber *formatObject;
    switch (recordEncoding) 
    {
        case (ENC_AAC4): 
            formatObject = [NSNumber numberWithInt: kAudioFormatMPEG4AAC];
            break;
        case (ENC_ALAC4):
            formatObject = [NSNumber numberWithInt: kAudioFormatAppleLossless];
            break;
        case (ENC_IMA44):
            formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
            break;
        case (ENC_ILBC4):
            formatObject = [NSNumber numberWithInt: kAudioFormatiLBC];
            break;
        case (ENC_ULAW4):
            formatObject = [NSNumber numberWithInt: kAudioFormatULaw];
            break;
        default:
            formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
    }
    [recordSettings setObject:formatObject forKey: AVFormatIDKey];
    [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
    [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
    [recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
    [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Recording"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; 
NSString *filePath =[NSString stringWithFormat:@"%@/%@.caf",dataPath,theDate];

NSURL *url = [NSURL fileURLWithPath:filePath];
app.audiofilepath=filePath;
NSError *error = nil;
audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
if ([audioRecorder prepareToRecord] == YES)
{
    [audioRecorder record];
}
else 
{
    int errorCode = CFSwapInt32HostToBig ([error code]); 
    NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode); 
 }
}
-(IBAction)stopRecording
{
 [audioRecorder stop];
recordEncoding=1;
[recdelegate viewpic:self didSelectValue:theDate didSelectEvent:@"STOP"];
 }

----要检索音频-

 -(IBAction)playRecording
 {
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

    // NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *filename=theDate;
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Recording"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; 
    NSString *filePath =[NSString stringWithFormat:@"%@/%@.caf",dataPath,filename];

    NSURL *url = [NSURL fileURLWithPath:filePath];
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = 0;
    [audioPlayer play];
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM