保存多个录制音频文件并获取所有录制音频

时间:2015-02-10 14:30:27

标签: ios objective-c ios7 ios8

我正在使用AVAudiofoundation进行录音,音频存储和正常播放,但我的问题是我必须录制多个音频,我必须在表格中显示,现在我只获得一个网址。请帮我找到解决方案。

此按钮显示暂停录音,我想在这里我想为我的问题做点什么

    - (IBAction)recordPauseTapped:(id)sender
    {
        // Stop the audio player before recording
        if (player.playing)
        {
            [player stop];
        }

        if (!recorder.recording)
        {
            AVAudioSession *session = [AVAudioSession sharedInstance];
            [session setActive:YES error:nil];
            // Start recording
            [recorder record];
            [recordPauseButton setTitle:@"Pause" forState:UIControlStateNormal];
        }
        else
        {
            // Pause recording
            [recorder pause];
            [recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];
        }

        [stopButton setEnabled:YES];
        [playButton setEnabled:NO];
    }

此按钮用于停止录制

    - (IBAction)stopTapped:(id)sender
    {
        [recorder stop];
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setActive:NO error:nil];
        NSString *urr=[recorder.url absoluteString];
        [recordeddata addObject:urr];

    }

此按钮用于播放录制的音频

    - (IBAction)playTapped:(id)sender {
        if (!recorder.recording)
        {
             player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
            [player setDelegate:self];
            [player play];
            [_tableview reloadData];
        }
    }

2 个答案:

答案 0 :(得分:0)

- (void)viewDidLoad
{
    [super viewDidLoad];
    recordeddata=[[NSMutableArray alloc]init];
    // Disable Stop/Play button when application launches
    [stopButton setEnabled:NO];
    [playButton setEnabled:NO];

    // Set the audio file
    NSMutableArray *pathComponents = [NSMutableArray 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:nil];
    recorder.delegate = self;
    recorder.meteringEnabled = YES;
    [recorder prepareToRecord];

    _tableview.dataSource=self;
    _tableview.delegate=self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)recordPauseTapped:(id)sender
{
    // Stop the audio player before recording
    if (player.playing)
    {
        [player stop];
    }

    if (!recorder.recording)
    {
        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setActive:YES error:nil];
        // Start recording
        [recorder record];
        [recordPauseButton setTitle:@"Pause" forState:UIControlStateNormal];
    }
    else
    {
        // Pause recording
        [recorder pause];
        [recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];
    }

    [stopButton setEnabled:YES];
    [playButton setEnabled:NO];
}

- (IBAction)stopTapped:(id)sender
{
    [recorder stop];
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setActive:NO error:nil];
    NSString *urr=[recorder.url absoluteString];
    [recordeddata addObject:urr];

}

- (IBAction)playTapped:(id)sender {
    if (!recorder.recording)
    {
         player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
        [player setDelegate:self];
        [player play];
        [_tableview reloadData];
    }
}

#pragma mark - AVAudioRecorderDelegate

- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag{
    [recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];
    [stopButton setEnabled:NO];
    [playButton setEnabled:YES];    
}

#pragma mark - AVAudioPlayerDelegate

- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Done"
                               message: @"Finish playing the recording!"
                              delegate: nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
      [alert show];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return recordeddata.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text=[recordeddata objectAtIndex:indexPath.row];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str=[recordeddata objectAtIndex:indexPath.row];
    NSURL *url=[NSURL URLWithString:str];
    if (!recorder.recording)
    {
        player=[[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [player setDelegate:self];
        [player play];
    }
}

答案 1 :(得分:0)

您正在使用一个文件路径初始化录音机,因此您一次只能保存一个音频

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

将此名称MyAudioMemo.m4a设为动态,

当您想录制新音频时

- (int)numberOfFilesInDocPath
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSFileManager *filemgr = [NSFileManager defaultManager];
    NSArray *filelist= [filemgr directoryContentsAtPath: docPath];
    int count = [filelist count];
    return count;
}
- (void)recordNewAudioWithFileName
{
    int numberOfFiles = [self numberOfFilesInDocPath];
    NSString *newFileName = [NSString stringWithFormat:@"MyAudioMemo_%d.m4a",numberOfFiles];
    NSMutableArray *pathComponents = [NSMutableArray arrayWithObjects:
                                      [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                                  newFileName,
                                  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:nil];
    recorder.delegate = self;
    recorder.meteringEnabled = YES;
    [recorder prepareToRecord];


}

对于整个实施,check this

希望这有帮助,