2015-02-10 286 views
0

我正在使用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]; 
     } 
    } 
+0

您可以分享您保存文件的代码,并访问音频文件,上面的代码与文件获取无关。 – Saif 2015-02-10 14:36:44

+0

我再次公布,请参阅下请 – 2015-02-10 15:08:24

回答

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

希望这有助于

+0

你能解释一下我清楚如何添加此代码到我的代码 – 2015-02-11 10:31:47

+0

谢谢bhayya其工作 – 2015-02-11 10:59:49

+0

你欢迎@Maheshreddy :) – Saif 2015-02-11 11:00:58

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]; 
    } 
} 
+0

请看看这段代码 – 2015-02-10 15:12:51

+0

哟男子有isuues当我们点击tablelist行 – 2015-09-24 11:48:18