我已经完成了整个视频播放器的工作状态,但仍然无法进行缓冲。正在尝试这种方法:How to show buffered data on UISlider using MpMoviePlayerController in iOS?但它不起作用。由于代码错误,我甚至在第一步都失败了。这是我的滑块代码。
-(void) playMethod {
NSURL *url = [[NSURL alloc] initWithString:urlStr.text];
movie = [[MPMoviePlayerController alloc]initWithContentURL:url];
[movie setMovieSourceType:MPMovieSourceTypeFile];
[movie.view setFrame:self.view.frame];
[movie prepareToPlay];
movie.controlStyle = MPMovieControlStyleNone;
movie.fullscreen = YES;
movie.shouldAutoplay = YES;
[movie setScalingMode:MPMovieScalingModeAspectFill];
//[movie setCurrentPlaybackRate:2.f];
[self.movie setFullscreen:YES animated:NO];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(MPMoviePlayerLoadStateDidChange:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
[self.view addSubview:movie.view];
progressbar=[[UISlider alloc]init];
[progressbar setFrame:CGRectMake(75,self.view.frame.size.height - 35 + 2, self.view.frame.size.width - 75 - 105 - 10, 20)];
[progressbar setMinimumTrackImage:[UIImage imageNamed:@"RedLine1.png"] forState:UIControlStateNormal];
[progressbar setMaximumTrackImage:[UIImage imageNamed:@"GrayLine1.png"] forState:UIControlStateNormal];
[progressbar setThumbImage:[UIImage imageNamed:@"Thumb.png"] forState:UIControlStateNormal];
[progressbar addTarget:self action:@selector(sliderforprogressbar:) forControlEvents:UIControlEventTouchUpInside];
[playerView addSubview:progressbar];
}
- (IBAction)sliderforprogressbar:(id)sender
{
movie.currentPlaybackTime = progressbar.value * movie.duration;
}
- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification
{
if ((movie.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK)
{
int seconds = (int)movie.duration;
int remainder = seconds % 3600;
int hours = seconds / 3600;
int minutes = remainder / 60;
int forSeconds = remainder % 60;
timeDisplay.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours,minutes,forSeconds];
}
}
如果有兴趣的话,我可以向您展示更多代码,但只关注我的问题的进度条。不知道我怎么能在这里做缓冲?
答案 0 :(得分:0)
好的,我已经用全新的方式做到了这一点,IDK如果有人有更好的想法那么请分享。
UIImageView *progressbarBG = [[UIImageView alloc]initWithFrame:CGRectMake(75+5,self.view.frame.size.height - 35 + 5, self.view.frame.size.width - 75 - 105 - 10 - 10, 20 - 5)];
progressbarBG.image = [UIImage imageNamed:@"GrayLine1.png"];
[playerView addSubview:progressbarBG];
//progressbarBuffering = [[UIImageView alloc]initWithFrame:CGRectMake(80,self.view.frame.size.height - 30, self.view.frame.size.width - 200, 15)];
progressbarBuffering = [[UIImageView alloc]init];
progressbarBuffering.image = [UIImage imageNamed:@"LightRedLine.png"];
[playerView addSubview:progressbarBuffering];
progressbar=[[UISlider alloc]init];
[progressbar setFrame:CGRectMake(75,self.view.frame.size.height - 35 + 2, self.view.frame.size.width - 75 - 105 - 10, 20)];
[progressbar setMinimumTrackImage:[UIImage imageNamed:@"RedLine1.png"] forState:UIControlStateNormal];
//[progressbar setMaximumTrackImage:[UIImage imageNamed:@"GrayLine1.png"] forState:UIControlStateNormal];
[progressbar setMaximumTrackImage:[UIImage imageNamed:@"BlankBar.png"] forState:UIControlStateNormal];
[progressbar setThumbImage:[UIImage imageNamed:@"Thumb.png"] forState:UIControlStateNormal];
[progressbar addTarget:self action:@selector(sliderforprogressbar:) forControlEvents:UIControlEventTouchUpInside];
[playerView addSubview:progressbar];
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updatePlaybackTime:) userInfo:nil repeats:YES];
- (void)updatePlaybackTime:(NSTimer*)theTimer {
progressbarBuffering.frame = CGRectMake(80,self.view.frame.size.height - 30, (self.view.frame.size.width - 200)*(movie.playableDuration/movie.duration), 15);
}
我创建了两个背景图像视图,一个用于灰色背景图像,另一个用浅红色图像,并在Progressbar的MaximumTrackImage中添加了空白png图像。然后我只是根据代码中提到的playableduration重新调整浅红色图像。