Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check avaudioplayer's current playback time

Is this the right way to check AVAudioPlayer's current playback time.

[audioPlayer play];

float seconds = audioPlayer.currentTime;

float seconds = CMTimeGetSeconds(duration); 

How I can code after

  [audioPlayer play]; 

that when currentTime is equal to 11 seconds then

[self performSelector:@selector(firstview) withObject:nil]; 

and after firstview when currentTime is equal to 23 seconds

[self performSelector:@selector(secondview) withObject:nil];
like image 501
user1120133 Avatar asked Mar 01 '12 22:03

user1120133


1 Answers

You could set up a NSTimer to check the time periodically. You would need a method like:

- (void) checkPlaybackTime:(NSTimer *)theTimer
{
  float seconds = audioPlayer.currentTime;
  if (seconds => 10.5 && seconds < 11.5) {
    // do something
  } else if (seconds >= 22.5 && seconds < 23.5) {
    // do something else
  }
}

Then set up the NSTimer object to call this method every second (or whatever interval you like):

NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 
  target:self
  selector:@selector(checkPlaybackTime:)
  userInfo:nil
  repeats:YES];

Check the NSTimer documentation for more details. You do need to stop (invalidate) the timer at the right time when you're through with it:

https://developer.apple.com/documentation/foundation/nstimer

EDIT: seconds will probably not be exactly 11 or 23; you'll have to fiddle with the granularity.

like image 187
gregheo Avatar answered Oct 27 '22 09:10

gregheo