2015-02-10 184 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