ios - Pausing a NSTimer/Stopwatch -


this question has answer here:

in project working on need have stopwatch pause , continue. far of basic functions work, have not been able find way pause timer , re-start it. fyi, have checked other postings , didn't work. code:

.h:

#import <uikit/uikit.h> #import <avfoundation/avfoundation.h>  @interface timer : uiviewcontroller <avaudiorecorderdelegate, avaudioplayerdelegate> {     avaudiorecorder *recorder;     avaudioplayer *player; }  @property (weak, nonatomic) iboutlet uibutton *recordpausebutton;  @property (weak, nonatomic) iboutlet uibutton *stopbutton;  @property (weak, nonatomic) iboutlet uilabel *stopwatchlabel;   -(ibaction)recordpausetapped:(id)sender;  -(ibaction)stoptapped:(id)sender;   @end 

.m:

#import "timer.h"   @interface songideasrecording ()   @property (strong, nonatomic) nstimer *stopwatchtimer; // store timer fires     after time  @property (strong, nonatomic) nsdate *startdate; // stores date of click on start button   @end    @implementation timer   @synthesize stopbutton, playbutton, recordpausebutton, stopwatchlabel;   - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil  {      self = [super initwithnibname:nibnameornil bundle:nibbundleornil];      if (self) {      // custom initialization  }      return self;  }   - (void)updatetimer  {      // timer 1/10 of second thats add stopwatch nstimeinterval timeinterval = 0.1;  // create date formatter nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"hh:mm:ss.sss"]; [dateformatter settimezone:[nstimezone timezoneforsecondsfromgmt:0.0]];  // take time displayed on stopwatch , add time interval nsdate *olddate = [dateformatter datefromstring:self.stopwatchlabel.text]; nsdate *newdate = [olddate datebyaddingtimeinterval:timeinterval]; //get string representation of new date nsstring *timestring = [dateformatter stringfromdate:newdate]; self.stopwatchlabel.text = timestring;  }   - (ibaction)recordpausetapped:(id)sender {      self.startdate = [nsdate date];      // create stop watch timer fires every 100 ms     self.stopwatchtimer = [nstimer scheduledtimerwithtimeinterval:1.0/10.0                                                        target:self                                                      selector:@selector(updatetimer)                                                      userinfo:nil                                                       repeats:yes];        // stop audio player before recording     if (player.playing) {        [player stop];     }      if (!recorder.recording) {     avaudiosession *session = [avaudiosession sharedinstance];     [session setactive:yes error:nil];      // start recording     [recorder record];     [recordpausebutton settitle:@"pause" forstate:uicontrolstatenormal];      } else {         // pause recording        [self.stopwatchtimer invalidate];        self.stopwatchtimer = nil;        [self updatetimer];        [recorder pause];        [recordpausebutton settitle:@"record" forstate:uicontrolstatenormal];      }        [stopbutton setenabled:yes];       [playbutton setenabled:no];  }  - (ibaction)stoptapped:(id)sender {      [recorder stop];       avaudiosession *audiosession = [avaudiosession sharedinstance];      [audiosession setactive:no error:nil];       [self.stopwatchtimer invalidate];      self.stopwatchtimer = nil;      [self updatetimer];  }   - (void) audiorecorderdidfinishrecording:(avaudiorecorder *)avrecorder successfully: (bool)flag{      [recordpausebutton settitle:@"record" forstate:uicontrolstatenormal];       [stopbutton setenabled:no];      [playbutton setenabled:yes];  }   - (ibaction)playtapped:(id)sender {      if (!recorder.recording){      player = [[avaudioplayer alloc] initwithcontentsofurl:recorder.url error:nil];      [player setdelegate:self];      [player play];      self.startdate = [nsdate date];      stopwatchlabel.text = @"00:00:00.000";      self.stopwatchtimer = [nstimer scheduledtimerwithtimeinterval:1.0/10.0                                                            target:self                                                          selector:@selector(updatetimer)                                                          userinfo:nil                                                           repeats:yes];       }   }   - (void) audioplayerdidfinishplaying:(avaudioplayer *)player successfully:(bool)flag  {      [self.stopwatchtimer invalidate];      self.stopwatchtimer = nil;      [self updatetimer];   }    @end 

in case, calculating value of stopwatch label nsdate record button pressed. there no way pause timer in way, every time recalculate value of stopwatch label, reflect original date of record button pressed. recommend changing method this:

 - (void)updatetimer  {      // timer 1/10 of second thats add stopwatch      nstimeinterval timeinterval = 0.1;      // create date formatter     nsdateformatter *dateformatter = [[nsdateformatter alloc] init];     [dateformatter setdateformat:@"hh:mm:ss.sss"];     [dateformatter settimezone:[nstimezone timezoneforsecondsfromgmt:0.0]];      // take time displayed on stopwatch , add time interval     nsdate *olddate = [dateformatter datefromstring:self.stopwatchlabel.text];     nsdate *newdate = [olddate datebyaddingtimeinterval:timeinterval];     //get string representation of new date , boom pow.     nsstring *timestring = [dateformatter stringfromdate:newdate];     self.stopwatchlabel.text = timestring;  } 

have not tested hope works. wouldn't surprised if there syntax issues too. also, make sure string in self.stopwatchlabel.text follows format start (ex. 00:00:00.000).


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -