سؤال

I've created a class of object called ARecorder. (ARecorder.h / ARecorder.m) Implementation has a few methods:

-void(setupAndRecord) 
-(void)stop 

etc.

I setup an AVAudioSession shared instance, I setup the file with NSURL, alloc and init *recorder object of AVAudioRecorder, prepare to record. then [recorder record] or [recorder recordforduration(NSTimer] - i've tried both.

When I instantiate *MyRecorder as an object of class ARecorder from say... the viewcontroller and call [... setupAndRecord] method - the object is apparently created and released before the AVAudioRecorder object inside the MYRecorder instantiation has a chance to do anything... it creates the file, begins to record, and by then the object MYRecorder is apparently immediately released.

If i set a loop, or use @selector afterdelay in *MYRecorder, it will record...this is a hack and not the proper way.

I've set *recorder as property:

@property (strong, nonatomic) AVAudioRecorder *recorder 

I've declared it as an instance var as well. Nothing makes it stay allocated. I understand I need to use a property instead of allocate and init the AVAudioRecorder *recorder object inside a method? So how do I turn this

"recorder = [[AVAudioRecorder alloc] initWithContentsOfURL:myURL error:&err];" 

into a property instead of a local instance?

If I move all this code to the viewcontroller - obviously the viewcontroller is retained in memory, so a -void(setupAndRecord) method that instantiates an AVAudioRecorder object will stay retained until finished - but isn't the point of the MVC pattern to separate model from view?

I also want to use the audioRecorderdidfinishrecording delegate in a viewcontroller - but I can't figure out how to get the instance of AVAudioRecorder *recorder in a MYRecorder object to use the delegate in ViewController.

I want the viewcontroller to be the delegate of *recorder and do something when audioRecorderDidFinishRecording is called at the end of [recorder recordforduration...]

Obviously the delegate method will not be called if the object is not staying in memory long enough to even record. But lets say MYRecorder object was being retained for some crazy reason, at the end of [... recordforduration], how can it use the delegate method that is in the viewcontroller and not in MYRecorder? All I want the MYRecorder object to do is record and tell the didfinishrecording delegate in the viewcontroller when its done. Doesn't seem like it should be that difficult.

هل كانت مفيدة؟

المحلول

It sounds like the problem is ownership of your MyRecorder object.

I suggest you make MyRecorder a singleton. Do a search here on SO for the singleton design pattern in Objective-C. You should find plenty of examples.

Your strong property syntax is correct. If your MyRecorder has a property:

@property (strong, nonatomic) AVAudioRecorder *recorder 

Then in your setup code in MyRecorder, you'd use code like this:

self.recorder = [[AVAudioRecorder alloc] initWithContentsOfURL:myURL error:&err];

Note that it reads self.recorder, not recorder. That uses the property's setter rather than accessing the instance variable directly, which you should get in the habit of doing.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top