Mihir B Mihir B - 1 year ago 35
Objective-C Question

I am not able to record audio by longpress button with circularProgressBar with timer. button is disabled while launch of application

---Hello I'm new to objective C. I am not able to record audio while pressing buttonTapped method below listed- buttontapped is disabled while application is launched , thus i cannot record and hence I cannot play the recorded sound. kindly guide with this.

--Here is my files.

-- I have imported two files of CircularProgressBarTimer from github and imported those file in the .h file.

----This is my .h file

@interface ViewController : UIViewController<AVAudioRecorderDelegate,AVAudioPlayerDelegate,UIGestureRecognizerDelegate>{
NSTimer *timer;
NSInteger globalTimer;
NSInteger counter;
NSInteger minutesLeft;
NSInteger secondsLeft;
UIRefreshControl *refreshControl;
CircularProgressTimer *progressTimerView;
@property (weak, nonatomic) IBOutlet UIButton *buttonRecord;
@property (strong, nonatomic) IBOutlet UILongPressGestureRecognizer *longpressGesture;
@property (weak, nonatomic) IBOutlet CircularProgressTimer *circularProgressView;

- (IBAction)buttonTapped:(id)sender;
-(void)longPressed:(UIGestureRecognizer *)longPress;

And this is my .m file

@interface ViewController (){
AVAudioRecorder *recorder;
AVAudioPlayer *player;



--In my viewDidLoad method i have initialise audio player, longesturerecognizer

@implementation ViewController
@synthesize buttonRecord;
@synthesize longpressGesture;
@synthesize circularProgressView;
- (void)viewDidLoad {
[super viewDidLoad];

UILongPressGestureRecognizer *gesture1 = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressed:)];
gesture1.delegate =self;
[gesture1 setMinimumPressDuration:(NSTimeInterval)10];
[self.buttonRecord addGestureRecognizer:gesture1];

//Set the audio file
NSArray *pathComponents =[NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject],@"MyAudioDemo.m4a", nil];
NSURL *outputFileURL =[NSURL fileURLWithPathComponents:pathComponents];

//Set the 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];
[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
//Initiate and prepare the recorder
recorder=[[AVAudioRecorder alloc]initWithURL:outputFileURL settings:recordSetting error:nil];
recorder.meteringEnabled =YES;
[recorder prepareToRecord];
//Disable play button when application launches
[buttonRecord setEnabled:NO];

---And i have aldo define the circularprogress bar method about the size allocation.

- (void)drawCircularProgressBarWithMinutesLeft:(NSInteger)minutes secondsLeft:(NSInteger)seconds
// Removing unused view to prevent them from stacking up
for (id subView in [self.view subviews]) {
if ([subView isKindOfClass:[CircularProgressTimer class]]) {
[subView removeFromSuperview];

// Init our view and set current circular progress bar value
CGRect progressBarFrame = CGRectMake(0, 0, 180, 180);
progressTimerView = [[CircularProgressTimer alloc] initWithFrame:progressBarFrame];
[progressTimerView setCenter:CGPointMake(160, 210)];
[progressTimerView setPercent:seconds];
if (/*minutes == 0 &&*/ seconds == 0) {
[progressTimerView setInstanceColor:[UIColor redColor]];

// Here, setting the minutes left before adding it to the parent view
//[progressTimerView setMinutesLeft:minutesLeft];
[progressTimerView setSecondsLeft:secondsLeft];
[self.view addSubview:progressTimerView];
progressTimerView = nil;

- (void)startTimer
timer = [NSTimer scheduledTimerWithTimeInterval:1.0

- (void)updateCircularProgressBar
// Values to be passed on to Circular Progress Bar
if (globalTimer > 0 && globalTimer <= 1200) {
// minutesLeft = globalTimer / 60;
secondsLeft = globalTimer % 43;

[self drawCircularProgressBarWithMinutesLeft:minutesLeft secondsLeft:secondsLeft];
NSLog(@"Time left:%02ld", (long)secondsLeft);
} else {
[self drawCircularProgressBarWithMinutesLeft:0 secondsLeft:0];
[timer invalidate];

- (void)remoteControlReceivedWithEvent:(UIEvent *)event{
switch (event.subtype) {
case UIEventSubtypeRemoteControlPause:
[self.circularProgressView pause];
case UIEventSubtypeRemoteControlPlay:
[self.circularProgressView play];


- (BOOL)shouldAutorotate{
return YES;

----I know I have done wrong code in buttonRecord event. but when I debug the application the button is disabled and cannot be pressed. Please guide me with this, that would be appreciated.

- (IBAction)buttonTapped:(id)sender {
if (player.playing) {
[player stop];

if (!recorder.recording) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];

// Start recording
[recorder record];
[buttonRecord setTitle:@"Pause" forState:UIControlStateNormal];

} else {

// Pause recording
[recorder pause];
[buttonRecord setTitle:@"Record" forState:UIControlStateNormal];

//[stopButton setEnabled:YES];
[_playButton setEnabled:NO];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0

if (!recorder.recording) {
player =[[AVAudioPlayer alloc]initWithContentsOfURL:recorder.url error:nil];
//[player setDelegate:self];
[buttonRecord setEnabled:YES];
[recorder recordForDuration:(NSTimeInterval)10];
[recorder record];

- (IBAction)playbuttontTapped:(id)sender {
if (!recorder.recording){
player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
[player setDelegate:self];
[player play];


The last line in your viewDidLoad is causing your record button to be dissabled, why have yo added this [buttonRecord setEnabled:NO];

You may try changing it to [buttonRecord setEnabled:YES]; or removing that line completely