【问题标题】:How to add buffering bar in Custom UISlider?如何在自定义 UISlider 中添加缓冲条?
【发布时间】:2015-08-18 06:02:53
【问题描述】:

我的整个视频播放器都处于工作状态,但仍然无法进行缓冲。正在尝试这种方法: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];
}
}

如果有人感兴趣,我可以向您展示更多代码,但只关注我的问题的进度条。知道如何在这里进行缓冲吗?

【问题讨论】:

  • 意味着你看不到缓冲(当视频缓冲时)......状态?你能更具体地解释一下你问题的前两行吗?
  • @ArpitParekh 我实际上是在创建自定义播放器...只是想添加缓冲。我已经尝试了上述不同的方法,但无法做到...因为我无法在 UISlider 中显示另一个栏...一个栏显示最大图像,一个用于当前播放时间...现在我该如何添加另一个PlayableDuration(缓冲)栏
  • 所以,你可以管理两个酒吧。或者你在 Buffering 的编码上有问题?
  • @ArpitParekh 我可以添加 2 条吗?在 UISlider 中...我可以做缓冲我只是不知道如何在滑块中显示它...就像我给滑块值一样:playableduration/duration ...它会给我缓冲时间...但是如果我显示缓冲我将如何显示当前时间
  • MPMoviePlayer 是否有任何工具来显示默认栏,它会在需要时自动显示缓冲栏?

标签: ios objective-c video-streaming progress-bar uislider


【解决方案1】:

好的,我用全新的方式完成了这个,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); 
}

我创建了两个背景Imageview,一个用于灰色背景图像,一个用于浅红色图像,并在Progressbar的MaximumTrackImage处添加了空白png图像。然后我只是根据代码中提到的 playableduration 调整浅红色图像的大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多