保存多个录制音频文件并获取所有录制音频
Posted
技术标签:
【中文标题】保存多个录制音频文件并获取所有录制音频【英文标题】:save multiple recording audio files and fetch all recording audios 【发布时间】:2015-02-10 14:30:26 【问题描述】:我正在使用 AVAudiofoundation 进行音频录制,音频存储和播放正常,但我的问题是我必须录制多个音频,我必须在表格中显示,现在我只得到一个 url 地址。请帮助我找到解决方案。
这个按钮显示暂停录音,我想在这里我想为我的问题做点什么
- (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];
【问题讨论】:
你能分享一下你保存文件和访问音频文件的代码吗,上面的代码与文件获取无关。 我又发了,请看下方 【参考方案1】:你正在用一个文件路径初始化录音机,所以你一次只能保存一个音频
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
希望这会有所帮助,
【讨论】:
【参考方案2】:- (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];
【讨论】:
以上是关于保存多个录制音频文件并获取所有录制音频的主要内容,如果未能解决你的问题,请参考以下文章
录制音频并保存到文档目录后无法使用 AVAudioPlayer 播放音频文件
NAudio : 保留最后一个 ex: 5s 录制的音频并随时保存