Gan*_*esh 11 iphone ipod objective-c ipad
我正在使用AVPlayer播放我的视频使用滑块和一些按钮.这是我使用按钮前进和后退的方法.
-(IBAction)MoveForward
{
//int value = timeSlider.value*36000 + 10;
//CMTime newTime = CMTimeMakeWithSeconds(value, playspeed);
//CMTime newTime = CMTimeMake(value,(playspeed*timeSlider.maximumValue));
CMTime newTime = CMTimeMakeWithSeconds(timeSlider.value, playspeed);
newTime.value += 60;
[player seekToTime: newTime];
}
-(IBAction)MoveBackward
{
CMTime newTime = CMTimeMakeWithSeconds(timeSlider.value-1, playspeed);
[player seekToTime: newTime];
}
Run Code Online (Sandbox Code Playgroud)
我的问题是Seek不能正常工作的时间.根据秒数导航到下一帧.我需要小心地移动下一帧.帮我...
Sam*_*art 25
我真的不了解你的代码,你真的不需要单独的方法来向前和向后移动,你可以使用相同的方法.我有一个工作的AVPlayer电影播放器,我会告诉你我是如何做滑块部分的.
-(IBAction)sliding:(id)sender{
CMTime newTime = CMTimeMakeWithSeconds(seeker.value, 1);
[self.player seekToTime:newTime];
}
-(void)setSlider{
sliderTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES]retain];
self.seeker.maximumValue = [self durationInSeconds];
[seeker addTarget:self action:@selector(sliding:) forControlEvents:UIControlEventValueChanged];
seeker.minimumValue = 0.0;
seeker.continuous = YES;
}
- (void)updateSlider {
self.seeker.maximumValue = [self durationInSeconds];
self.seeker.value = [self currentTimeInSeconds];
}
- (Float64)durationInSeconds {
Float64 dur = CMTimeGetSeconds(duration);
return dur;
}
- (Float64)currentTimeInSeconds {
Float64 dur = CMTimeGetSeconds([self.player currentTime]);
return dur;
}
Run Code Online (Sandbox Code Playgroud)
就是这样,这段代码中有两个陷阱,首先,duration属性返回一个CMTime变量,你必须将它转换为一个浮点数,同样,这将返回原始秒数,你必须将其转换为h:mm :ss如果要显示时间标签.其次,updateSlider方法每秒由一个计时器触发.祝好运.
以下代码片段对我有用:
CMTime videoLength = self.mPlayer.currentItem.asset.duration; // Gets the video duration
float videoLengthInSeconds = videoLength.value/videoLength.timescale; // Transfers the CMTime duration into seconds
[self.mPlayer seekToTime:CMTimeMakeWithSeconds(videoLengthInSeconds * [slider value], 1)
completionHandler:^(BOOL finished)
{
dispatch_async(dispatch_get_main_queue(), ^{
isSeeking = NO;
// Do some stuff
});
}];
Run Code Online (Sandbox Code Playgroud)