Panoramic AVPlayer Search

I am trying to pan and search back and forth in my AVPlayer. This is a kind of work, but the basic mathematics of determining where the panorama is translated to the length of the asset is incorrect. Can anyone offer help?

- (void) handlePanGesture:(UIPanGestureRecognizer*)pan{ CGPoint translate = [pan translationInView:self.view]; CGFloat xCoord = translate.x; double diff = (xCoord); //NSLog(@"%F",diff); CMTime duration = self.avPlayer.currentItem.asset.duration; float seconds = CMTimeGetSeconds(duration); NSLog(@"duration: %.2f", seconds); CGFloat gh = 0; if (diff>=0) { //If the difference is positive NSLog(@"%f",diff); gh = diff; } else { //If the difference is negative NSLog(@"%f",diff*-1); gh = diff*-1; } float minValue = 0; float maxValue = 1024; float value = gh; double time = seconds * (value - minValue) / (maxValue - minValue); [_avPlayer seekToTime:CMTimeMakeWithSeconds(time, 10) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero]; //[_avPlayer seekToTime:CMTimeMakeWithSeconds(seconds*(Float64)diff , 1024) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero]; } 
+5
source share
2 answers

You do not normalize the location of the touch and the corresponding time values. Is there a 1: 1 ratio between the two? It's impossible.

Take the minimum and maximum values ​​for the location of the touch pan gestures and the minimum and maximum values ​​for the duration of the asset (obviously, from zero to the length of the video), and then apply the following formula to translate the location of the touch to the search time:

 // Map #define map(x, in_min, in_max, out_min, out_max) ((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) 

Here the code I wrote uses this formula:

 - (IBAction)handlePanGesture:(UIPanGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateChanged){ CGPoint location = [sender locationInView:self]; float nlx = ((location.x / ((CGRectGetMidX(self.frame) / (self.frame.size.width / 2.0)))) / (self.frame.size.width / 2.0)) - 1.0; //float nly = ((location.y / ((CGRectGetMidY(self.view.frame) / (self.view.frame.size.width / 2.0)))) / (self.view.frame.size.width / 2.0)) - 1.0; nlx = nlx * 2.0; [self.delegate setRate:nlx]; } } 

I rejected the shortcut that displays the speed and the Play icon that appears during cleaning and that resizes depending on how fast or slow you pan the video. Although you did not ask for it, if you want, just ask.

Oh, the one-two factor is designed to add an acceleration curve to the panorama stiffness value sent to the delegate's setRate method. You can use any formula for this, even the actual curve, such as pow (nlx, 2.0) or something else ...

+3
source

If you want to make it more accurate and useful, you must implement different “sensitivity levels”.

Apple does this with its slider: if you push from the slider, and then on the sides, the pace at which the video changes. The farther you are from the slider, the more accurate it is / the less you can achieve.

0
source

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


All Articles