Why is AVAudioPlayer failing to initialise? With OSStatus error 1685348671? - ios

I am recording audio in a view controller (a voice memo). This works. I can play back the recorded audio as long as the user doesn't dismiss the view controller.
However, if the user dismisses the view controller, and opens the view controller again, initialising the AVAudioPlayer fails with OSStatus error 1685348671.
I also tried to init the player with NSData:
NSData *data = [[NSData alloc] initWithContentsOfURL:_outputFileURL];
_player = [[AVAudioPlayer alloc] initWithData:data error:&error2];, but that would not work either
I have tried .caf, followed every tutorial, but somehow I can not make it work...
This error seems to refer to invalid file or so, though I am not sure. However if I download the app in the Organizer and open it on my Mac, I can play it, so the file itself is not corrupted.
Here is my record method:
- (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:#"Aufnahme" forState:UIControlStateNormal];
}
[stopButton setEnabled:YES];
[playButton setEnabled:NO];
}
Recording works, and the file exists in the Documents directory.
Here is my play method, which works if the file was recorded immediately before, with without the user leaving the view controller:
- (IBAction)playTapped:(id)sender {
if (!_recorder.recording){
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:YES error:nil];
NSError *error;
BOOL success = [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
if(!success)
{
NSLog(#"error doing outputaudioportoverride - %#", [error localizedDescription]);
}
NSError *error2;
NSLog(#"_outputFileURL: %#", _outputFileURL);
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:_outputFileURL error:&error2];
if (error2) {
NSLog(#"error initialising player - %#", [error2 localizedDescription]);
}
[_player setDelegate:self];
[_player play];
}
}
And here is my viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
[stopButton setEnabled:NO];
// Set the audio file
if ([_audioFileName isEqualToString:#""]) {
NSString *guid = [[NSUUID new] UUIDString];
_audioFileName = [NSString stringWithFormat:#"audio-%#.m4a", guid];
[playButton setEnabled:NO];
} else {
[playButton setEnabled:YES];
}
NSLog(#"_audioFileName: %#", _audioFileName);
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
_audioFileName,
nil];
_outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[session setActive:YES 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];
}

Your problem is that you have [_recorder prepareToRecord]; into the viewLoad, which means that
it will be overwritten each time that you load the view controller.
If you don't wish this behaviour you should remove this line and put it at the beginning of
recordPauseTapped for example.
That should fix your issues.

Related

Delegates Of AVAudioRecorderDelegate are Not Calling

Actually I want To Record The Audio and Store Into Document, i haveFound FilePath and even attached the audio Files, But i want To attach actual Audio into that Path, It's Not Working, I write that Code in DidFinish like This.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = #"Recording Area";
[btnRecord setBackgroundImage:[UIImage imageNamed:#"Record.png"] forState:UIControlStateNormal];
session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
audioRecorder.delegate = self;
NSDate *today = [[NSDate alloc]init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"hh:mm:ss"];
NSString *currentTime = [dateFormatter stringFromDate:today];
NSString *outputPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
outputPath = [outputPath stringByAppendingString:[NSString stringWithFormat:#"%#.m4a",currentTime]];
NSMutableDictionary *recordDictionary = [[NSMutableDictionary alloc] init];
[recordDictionary setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordDictionary setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordDictionary setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
audioRecorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:outputPath] settings:recordDictionary error:nil];
audioRecorder.meteringEnabled = YES;
[audioRecorder prepareToRecord];
}
and For Recording Audio
-(IBAction)onClickRecord:(id)sender
{
if(player.playing)
{
[player stop];
}
if([[btnRecord backgroundImageForState:UIControlStateNormal]isEqual:[UIImage imageNamed:#"Record.png"]])
{
if (!audioRecorder.recording)
{
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
[audioRecorder record];
[btnRecord setBackgroundImage:[UIImage imageNamed:#"Pause.png"] forState:UIControlStateNormal];
}
}
else if([[btnRecord backgroundImageForState:UIControlStateNormal]isEqual:[UIImage imageNamed:#"Pause.png"]])
{
[audioRecorder pause];
[btnRecord setBackgroundImage:[UIImage imageNamed:#"Record.png"] forState:UIControlStateNormal];
}
}
For Saving That Recorded Audio
-(IBAction)onClickSave:(id)sender
{
[audioRecorder stop];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
}
And Finally that Is the Delegates Where My code is Not Working actually i am Not able To call This method
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
[btnRecord setBackgroundImage:[UIImage imageNamed:#"Record.png"] forState:UIControlStateNormal];
[arrayFiles addObject:recorder.url.absoluteString];
NSLog(#"Array %#",arrayFiles);
}
So Please Specify Me where i am Wrong.
You are setting the delegate before initialising the AVAudioRecorder
Move the
audioRecorder.delegate = self; after init
audioRecorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:outputPath] settings:recordDictionary error:nil];
audioRecorder.meteringEnabled = YES;
audioRecorder.delegate = self;
[audioRecorder prepareToRecord];

How store iOS audio recordings on Parse?

Newbie iOS coder here, apologies if the answer is really simple.
So I set up my audio recording in viewDidLoad
// Set the audio file
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];
I have a bar button that records new audio files:
// 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];
} else {
// Pause recording
[recorder pause];
}
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Stop" style:UIBarButtonItemStylePlain target:self
action:#selector(stopTapped)];
Then the start becomes a stop button
[recorder stop];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"New" style:UIBarButtonItemStylePlain target:self
action:#selector(actionNew)];
How can I add this as a PFFile and save in a dictionary in Parse?
I've read through a lot of the Parse documentation but still don't really get the hang of it. Any help much appreciated.
It looks like the simplest solution is to load the file from the phone when the user is finished recording instead of trying to save the recording directly to an NSData object. If you want to look into recording straight to an NSData object check out apple docs and this stackoverflow question.
First you want to get an instance of NSData after the user has finished recording :
NSData *audioData = [[NSData alloc] initWithContentsOfFile:filePath];
Make a new PFFile :
PFFile *file = [PFFile fileWithName:#"resume.txt" data:data];
And then save it with your desired saving method.

save multiple recording audio files and fetch all recording audios

I am working on audio recording using AVAudiofoundation,audio is storing and playing properly, But my problem is i have to record multiple audios and i have to show that in table, Now i am getting one url address only. please help me to find the solution.
This button is showing for paused the audio recording,i think here i want to do some thing for my problem
- (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];
}
this button is for stop the recording
- (IBAction)stopTapped:(id)sender
{
[recorder stop];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
NSString *urr=[recorder.url absoluteString];
[recordeddata addObject:urr];
}
This button is for play the recorded audio
- (IBAction)playTapped:(id)sender {
if (!recorder.recording)
{
player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
[player setDelegate:self];
[player play];
[_tableview reloadData];
}
}
You are initializing the audio recorder with only one file path, So you can only save one audio at a time
NSMutableArray *pathComponents = [NSMutableArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
#"MyAudioMemo.m4a",
nil];
make this name MyAudioMemo.m4a dynamic,
when you want to record a new audio
- (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];
}
For entire implementation, check this
Hope this helps,
- (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];
}
}

iPhone - Problems Saving Audio After Recording

I a button that records audio when isNotRecording is false and plays audio when isNotRecording is true. Here is my code in the viewDidLoad function:
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];
Here is my code to play the sound or record:
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];
}
}
And here is my code to stop the playing/recording:
[recorder stop];
[player stop];
Now, everything works almost perfectly (i.e. I can record and play sounds fine). However, there are two issues. The major one is that after I close out the app and try to play the sound, it does not have a sound to play. I want it to play the previous recording. That is my main issue, but it would also be great if I could reduce the lag from when I press the button and the sound plays. Sometimes, if I press the button and release it very fast, no sound plays at all. Any suggestions are greatly appreciated. However, again, the main issue I need fixed is that the recording doesn't save after I close out the app.
I think what's happening is that when you initialize your AVAudioRecorder when the view loads then that will delete any existing file at the target location, irrespective of whether you start recording at that stage. So I think that's how the file is going missing.
Simplest may be just not to call [recorder prepareToRecord]; when you do, as I suspect that's when your recording is getting deleted.
Or you might want to use a temporary file while recording and move it to the final location when you're notified that the recording is finished, in order to avoid that issue. Code is something like:
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];
}
}

How to name recorded file from AVAudioRecorder

I am making an iOS app where I need to record speech using AVAudioRecorder and then I want to rename the file. Right now, I can successfully record the speech but I don't know how to give the file a custom name. How can I give the recorded file a custom name? Any help is appreciated.
In the viewDidLoad:
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
#"MyAudioMemo.m4a",
nil];
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];
and all the other methods:
- (IBAction)recordPressed:(id)sender {
if (!recorder.recording) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
// Start recording
[recorder record];
NSLog(#"Recording...");
} else {
// Pause recording
[recorder pause];
NSLog(#"Paused...");
}
}
- (IBAction)stopPressed:(id)sender {
[recorder stop];
NSLog(#"Stopped...");
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
}
You can simple move your file to another path, when you do that, you can specify a new name for the file, is the exactly same behavior of the mv command in shell.
There is a very simple snippet to you understand how to rename your file using move the FileManager moveItemAtPath:toPath:error method:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *newPath = [docsDir stringByAppendingPathComponent:#"newname.m4a"];
// Try to move your file and the current path to newPath
if ([fileManager moveItemAtPath:currentPath toPath:newPath error:&error] != YES)
{
NSLog(#"Error: %#", [error localizedDescription]);
}
else
{
// File moved success
}

Resources