0

I am using AVAudioRecorder in my mac app. In my .m file I declare

@interface RecordViewController ()
{
    AVAudioRecorder *audioRecorder;
...
}
@end

further down I then alloc init the object like so:

audioRecorder = [[[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recorderSettings error:&error] retain];
...
[audioRecorder prepareToRecord];
...
[audioRecorder record];

I can see that the file gets created, but it only has 4KB. I do not get an error but when I test if [audioRecorder isRecording] I can see that [audioRecorder is no longer recording. I am assuming that the object got released, even though I am not using ARC and the dealloc method was not called.

I had a similar problem with the AVAudioPlayer but the additional retain] appended to the alloc init solved that.

What am I doing wrong?

Thanks

UPDATE 1:

I have even tried to do the following (all in the same method):

[audioRecorder record];
NSLog(@"recording is ... %d", [audioRecorder isRecording]);

and the result is 0. ???

Do I have to use self ? or lazily instantiate audioRecorder?

UPDATE 2: I recreated the project - called it TEST and copied the source code from my old project. Now it works?!?! Go figure. The source codes are line for line the same. Is this a known problem with XCode?

Joseph
  • 9,171
  • 8
  • 41
  • 67
  • Did you run zombies on the program to check the retain count? If `audioRecorder` was deallocated it seems like you would be getting a bad access error if you call `isRecording` on it. – Dustin Jul 24 '12 at 13:24
  • If I run it in instruments no NSZombie gets created. I also don't get an error or bad access call when I call `isRecording`. – Joseph Jul 24 '12 at 13:40

2 Answers2

1

First of all, based on your comment, if you can still access the instance means that your object isn't dealloc.

AVAudioRecorder provide a delegate (AVAudioRecorderDelegate) have you tried to use the delegate for log if the recording is successfully or you get some error?

Particularly the method audioRecorderEncodeErrorDidOccur:error: should give you some error if occurred while recording.

BTW the additions retain] is not need because whit alloc init you've got the ownership of the object and you should release.

Luca Bernardi
  • 4,191
  • 2
  • 31
  • 39
  • Had/have that delegate method implemented no error gets logged. I only added the retain because I noticed that a `AVAudioPlayer` object would not stay retained unless I did it. It would start playing for a split second and then cease. Once I added the `retain]` it played normally. I have even become paranoid and added the compiler flags `-fno-objc-arc` just to make sure that ARC really isn't active. – Joseph Jul 24 '12 at 14:04
  • I also have `-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag` implemented and this never gets called. – Joseph Jul 24 '12 at 14:05
  • And one more thing for clarification. When I call `[audioRecorder isRecording]` i.e. in an NSLog I get `0` as a result - but no error. – Joseph Jul 24 '12 at 14:07
0

It can't be released unless you release it, because you alloc and init and you don't release.( it's not autoreleased also.)

If ARC is enabled, it must be strong.

Please look at http://chinki-singh.blogspot.com/2011/07/how-to-record-audio-using.html

Can
  • 536
  • 2
  • 7
  • 22
  • The only time I call `[audioRecorder release]` is in the `dealloc` method, which does not get called (I have a NSLog in the method to make sure). As mentioned I created a project without ARC being checked. – Joseph Jul 24 '12 at 13:51
  • Ivars are `__strong` by default. – Jonathan Grynspan Jul 24 '12 at 13:52