【问题标题】:can't able to save recorded audio iOS?无法保存录制的音频 iOS?
【发布时间】:2014-02-12 01:11:24
【问题描述】:

我想将录制的音频保存在本地文档目录 之后我想播放保存的音频文件

音频文件没有在文档目录中创建?

我尝试过使用以下代码我做错了什么?

NSString *temp1=[NSString stringWithFormat:@"%@.m4a",temp];
NSLog(@"=====>>%@",temp1);
NSArray *pathComponents=[NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)lastObject],temp1, nil];
NSURL *outputFileURL=[NSURL fileURLWithPathComponents:pathComponents];

if (isRecording==NO) {
    isRecording=YES;
    NSLog(@"recording started");
 session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

// Initiate and prepare the recorder
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];

[session setActive:YES error:nil];

// Start recording
[recorder record];

}
else
{
    NSLog(@"recording stopped");
    [recorder stop];
    NSData *audioData=[NSData dataWithContentsOfURL:outputFileURL];
    [audioData writeToURL:outputFileURL atomically:YES];

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:outputFileURL error:nil];
    [player prepareToPlay];
    [player play];
    [player setVolume:1.0];
    isRecording=NO;

}

【问题讨论】:

    标签: objective-c ios7 avfoundation nsdocumentdirectory


    【解决方案1】:

    试试这个代码

    -(IBAction)btnS1AudioPressed:(id)sender
    {
        audioSession = [AVAudioSession sharedInstance];
        NSError *err = nil;
        [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
    
        if(err)
        {
            NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
            return;
         }
        [audioSession setActive:YES error:&err];
        err = nil;
        if(err)
        {
            NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
            return;
        }
    
        NSMutableDictionary *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];
    
        NSString *AudioName=[NSString stringWithFormat:@"TestAudio.caf"];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        audioPath = [documentsDirectory stringByAppendingPathComponent:AudioName];
        NSURL *url = [NSURL fileURLWithPath:audioPath];
        err = nil;
        NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
        if(audioData)
        {
            NSFileManager *fm = [NSFileManager defaultManager];
            [fm removeItemAtPath:[url path] error:&err];
        }
        recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
        if(!recorder)
        {
            NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
            UIAlertView *alert =
            [[UIAlertView alloc] initWithTitle: @"Warning"
                                       message: [err localizedDescription]
                                      delegate: nil
                             cancelButtonTitle:@"OK"
                             otherButtonTitles:nil];
            [alert show];
            return;
        }
        [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];
            return;
        }
        [recorder recordForDuration:(NSTimeInterval) 10000];
    }
    
    - (IBAction) stopRecording:(id)sender
    {
        [recorder stop];
    }
    

    【讨论】:

      【解决方案2】:

      我发现了我犯的错误

      NSArray *pathComponents=[NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)lastObject],temp1, nil]; 
      

      在上面的代码中,我使用了NSDocumentationDirectory 而不是NSDocumentsDirectory

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-24
        • 1970-01-01
        • 2016-03-25
        • 1970-01-01
        • 2012-03-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多