AVPlayer SeekToTime issue

When using AVPlayer method – SeekToTime, you might find there’s a problem: the position of seeking time is not correct, or, in other word, it’s at the exact time slot we want.

Especially, when you do development on multimedia App with subtitles, this will be a big problem cause users might expect that the people speaking tone can match the subtitle. Or they will be mad at you.

305908d1359615600-madejski-miracle-wtf-shit_640_417_s_c1_center_top_0_0
YES, this is what customers think about your Apps.

I have tried some ways to achieve this goal – precisely put my time cursor on the correct position. But I didn’t get a good result for that.

For example, I had tried this.

[self.avPlayer seekToTime: CMTimeMakeWithSeconds(musicTime, 600)
              toleranceBefore: kCMTimeZero
               toleranceAfter: kCMTimeZero];

Cause I wanna search specific music time for next jump. But the result would get extra .x seconds. After searching and studying, I finally find the answer. You need to get the time scale of current playing asset. That’s the most important thing.

// Get current asset time scale first.
int32_t currentAssetTimeScale = self.avPlayer.currentItem.asset.duration.timescale; 

CMTime exactTime = CMTimeMake(musicTime, timeScale);
[self.player seekToTime:exactTime toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];

After doing that, then you can seek precise time you want.

Happy coding.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s