Check playing time avaudioplayer

Is it correct to check the current playback time of AVAudioPlayer's .

 [audioPlayer play]; float seconds = audioPlayer.currentTime; float seconds = CMTimeGetSeconds(duration); 

How can I encode after

  [audioPlayer play]; 

when currentTime is 11 seconds then

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

and after the first view, when currentTime is 23 seconds

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

Thank you for your responses.

+4
source share
1 answer

You can configure NSTimer to periodically check the time. You will need a method such as:

 - (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 configure the NSTimer object to call this method every second (or any other interval):

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

See the NSTimer documentation for more details. You need to stop (cancel) the timer at the right time when you are done with it:

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html

EDIT : seconds probably won't be exactly 11 or 23; you have to mess with the details.

+7
source

Source: https://habr.com/ru/post/1399272/


All Articles