0

Hi I have a very strange problem in the simulator works perfect but when I charge the app to the iPad it does not work. Any idea?

Here I create all the variables to make it work.

- (void)viewDidLoad {

[super viewDidLoad];

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:AVAudioQualityMin],
                                AVEncoderAudioQualityKey,
                                [NSNumber numberWithInt:16],
                                AVEncoderBitRateKey,
                                [NSNumber numberWithInt: 2],
                                AVNumberOfChannelsKey,
                                [NSNumber numberWithFloat:44100.0],
                                AVSampleRateKey,
                                nil];

NSError *error = nil;

audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL
                                            settings:recordSettings
                                               error:&error];
if (error)
{
    NSLog(@"error: %@", [error localizedDescription]);
} else {
    [audioRecorder prepareToRecord];
}

}

I have some buttons for record, play and stop. So these are the codes for all of them.

-(void) recordAudio{
if (!audioRecorder.recording){
    recordButton.enabled = NO;
    playButton.enabled = NO;
    saveButton.enabled = NO;
    stopButton.enabled = YES;
    [audioRecorder record];
}
}

-(void)stop{
recordButton.enabled = YES;
playButton.enabled = YES;
saveButton.enabled = YES;
stopButton.enabled = NO;    
if (audioRecorder.recording){
    [audioRecorder stop];
}
else if (audioPlayer.playing){
    [audioPlayer stop];
}
}


-(void) playAudio{
if (!audioRecorder.recording){
    recordButton.enabled = NO;
    stopButton.enabled = YES;
    saveButton.enabled = NO;        
    if (audioPlayer)
        audioPlayer=nil;
    NSError *error;

    audioPlayer = [[AVAudioPlayer alloc]
                   initWithContentsOfURL:audioRecorder.url
                   error:&error];

    audioPlayer.delegate = self;

    if (error)
        NSLog(@"Error: %@", [error localizedDescription]);
    else
        [audioPlayer play];
}
}
andres83
  • 310
  • 1
  • 12

1 Answers1

0

finally I found a solution. To my previous code I add this lines inside the ViewdidLoad.

AVAudioSession * audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
[audioSession setActive:YES error: &error];

and for the settings I added this:

[NSNumber numberWithInt:kAudioFormatAppleIMA4], AVFormatIDKey

I copy the code from http://www.iphoneam.com/blog/index.php?title=using-the-iphone-to-record-audio-a-guide&more=1&c=1&tb=1&pb=1 and the idea of using AVAudioSession came from How to record and play sound in iPhone app?

If you need the complete code is this one.

- (void)viewDidLoad {

[super viewDidLoad];

NSError *error = nil;

AVAudioSession * audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
[audioSession setActive:YES error: &error];

playButton.enabled = NO;
stopButton.enabled = NO;
saveButton.enabled = NO;

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
docsDir = [HSAudioController pathToDocumentsFolder];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:kAudioFormatAppleIMA4], AVFormatIDKey,
                                [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
                                [NSNumber numberWithInt:16], AVEncoderBitRateKey,
                                [NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
                                [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
                                nil];

audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL
                                            settings:recordSettings
                                               error:&error];
NSLog(@"%@",audioRecorder.url);
if (error)
{
    NSLog(@"error: %@", [error localizedDescription]);
} else {
    [audioRecorder prepareToRecord];
}
}
Community
  • 1
  • 1
andres83
  • 310
  • 1
  • 12