Can't record or play with a simple recorder?

Posted by user1280535 on Stack Overflow See other posts from Stack Overflow or by user1280535
Published on 2012-06-10T15:22:32Z Indexed on 2012/06/10 16:40 UTC
Read the original article Hit count: 157

Filed under:

I have 2 classes, a record and a player. In my main scene, I create an instance of them and play and record. But, as I see, it only records and somehow does not play (the file is not there!)

Here is the code for both :

-(void)record {
    NSArray *dirPaths;
    NSString *docsDir;
    NSString *sound= @"sound0.caf" ;
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ;
    docsDir = [dirPaths objectAtIndex:0];
    NSString *soundFilePath = [docsDir stringByAppendingPathComponent:sound];
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

    NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
      [NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
      [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
      [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
      [NSNumber numberWithInt: AVAudioQualityMax],
      AVEncoderAudioQualityKey, nil];

    NSError *error;
    myRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:settings error:&error];

    if (myRecorder)  {
        NSLog(@"rec");
        [myRecorder prepareToRecord];
        myRecorder.meteringEnabled = YES;
        [myRecorder record];
    } else
        NSLog( @"error"  );
}

I can see the log of rec.

-(void)play {
  NSArray *dirPaths;
  NSString *docsDir;
  dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
    NSUserDomainMask, YES);
  docsDir = [dirPaths objectAtIndex:0];
  NSString *soundFilePath1 =  @"sound0.caf" ;
  NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath1];
  BOOL isMyFileThere = [[NSFileManager defaultManager] fileExistsAtPath:soundFilePath1];
  if(isMyFileThere) {
    NSLog(@"PLAY"); 
    avPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:NULL];
    avPlayer1.volume = 8.0;
    avPlayer1.delegate = self;
    [avPlayer1 play];
 }
}

I DONT SEE THE LOG OF PLAY !

I call them both with:

recInst=[recorder alloc]; //to rec
[recInst record];

plyInst=[player alloc]; //play
[plyInst play];

and to stop the recorder:

- (void)stopRecorder {
    NSLog(@"stopRecordings");
    [myRecorder stop];
    //[myRecorder release];    
}

What's wrong here? Thanks.

© Stack Overflow or respective owner

Related posts about objective-c