iPhone - 录制后保存音频的问题

时间:2014-10-12 00:12:17

标签: ios objective-c iphone xcode audio

我是一个按钮,当isNotRecording为false时记录音频,当isNotRecording为true时播放音频。这是我在viewDidLoad函数中的代码:

NSArray *pathComponents = [NSArray arrayWithObjects:
                           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                           @"MyAudioMemo.m4a",
                           nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

// Setup audio session
AVAudioSession *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];

以下是播放声音或录音的代码:

if (isNotRecording==NO){

    if (player.playing) {
        [player stop];
    }

    if (!recorder.recording) {
        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setActive:YES error:nil];

        // Start recording
        [recorder record];


    }


}

else{
    if (!recorder.recording){
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
        [player setDelegate:self];
        [player play];
    }


}

这是我停止播放/录制的代码:

[recorder stop];
[player stop];

现在,一切都运作得非常完美(即我可以录制和播放声音很好)。但是,有两个问题。主要的一点是,在我关闭应用程序并尝试播放声音后,它没有播放声音。我想让它播放以前的录音。这是我的主要问题,但如果我可以减少按下按钮和声音播放时的延迟,那也很棒。有时,如果按下按钮并快速释放按钮,则根本不会播放声音。任何建议都非常感谢。然而,我需要修复的主要问题是,在关闭应用程序后,录制文件无法保存。

1 个答案:

答案 0 :(得分:0)

我认为发生的事情是,当视图加载时初始化AVAudioRecorder然后将删除目标位置的任何现有文件,无论您是否在该阶段开始录制。所以我认为文件是如何丢失的。

最简单的可能就是不要在你这样做时调用[recorder prepareToRecord];,因为我怀疑你的录音被删除了。

或者您可能希望在录制时使用临时文件,并在您通知录制完成时将其移至最终位置,以避免出现此问题。代码类似于:

NSArray *pathComponents = [NSArray arrayWithObjects:
                           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                           @"MyAudioMemoTemp.m4a",
                           nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

// Setup audio session
AVAudioSession *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];

if (isNotRecording==NO){

    if (player.playing) {
        [player stop];
    }

    if (!recorder.recording) {
        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setActive:YES error:nil];

        // Start recording
        [recorder record];
    }
}

else{
    if (!recorder.recording){
        NSArray *pathComponents = [NSArray arrayWithObjects:
                                [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                                @"MyAudioMemo.m4a",
                                nil];
        NSURL *inputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

        player = [[AVAudioPlayer alloc] initWithContentsOfURL:inputFileURL error:nil];
        [player setDelegate:self];
        [player play];
    }

}

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder
                               successfully:(BOOL)flag
{
    if (successfully) {

        // Copy temp file used during recording to replace that used when playing
        //
        pathComponents =  [NSArray arrayWithObjects:
                            [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                            @"MyAudioMemo.m4a",
                            nil];
        NSURL *targetFileURL = [NSURL fileURLWithPathComponents:pathComponents];

        NSError *error = nil;
        [[NSFileManager defaultManager] removeItemAtURL:targetFileURL error:&error];

        error = nil;
        [[NSFileManager defaultManager] copyItemAtURL:recorder.url toURL:targetFileURL error:&error];

    }

}