如何在iOS中使用MpMoviePlayerController在UISlider上显示缓冲数据? [英] How to show buffered data on UISlider using MpMoviePlayerController in iOS?

查看:127
本文介绍了如何在iOS中使用MpMoviePlayerController在UISlider上显示缓冲数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MPMoviePlayerController 来播放音频,我想在这个滑块上显示缓冲数据...

I am using MPMoviePlayerController to play audio and I want to show the buffered data on slider like this ...

我想要显示缓冲区数据,如滑块中的红色部分。我试过去谷歌吧。但我没有得到任何解决方案。
和如何制作滑块自定义?
提前致谢...

I want to show the buffer data like red section in slider. I have tried to google it. But I didn't get any solution for it. and How to make slider customize? Thanks in advance...

推荐答案

是的我们可以使用 MPMoviePlayerController显示流数据通过 playableDuration 。我正在解释所有这些涉及我的步骤,为我的自定义播放器制作 custom_slider ...

Yes We can Show stream data using MPMoviePlayerController by its playableDuration. I am explaining all these steps involved me to make this custom_slider for my customize player...

(你如果你只对编程部分感兴趣,可以直接阅读第4步
这些是以下步骤:

(You Can direct read step 4 if you only interested in programming part) These are the steps:

第1步(第一个设计部分)我使用标准控件完成...

Step 1: (First Design part) i done it using standard Controls...

在Interface Builder中,将UISlider立即置于顶部您的UIProgressView并使它们大小相同。

In Interface Builder place your UISlider immediately on top of your UIProgressView and make them the same size.

在UISlider上,背景水平线称为轨道,诀窍是使其不可见。我们使用透明的PNG和UISlider方法setMinimumTrackImage:forState:和setMaximumTrackImage:forState:。

On a UISlider the background horizontal line is called the track, the trick is to make it invisible. We do this with a transparent PNG and the UISlider methods setMinimumTrackImage:forState: and setMaximumTrackImage:forState:.

在视图控制器的viewDidLoad方法中添加:

In the viewDidLoad method of your view controller add:

[ProgressSlider setMinimumTrackImage:minImage forState:UIControlStateNormal];

[ProgressSlider setMaximumTrackImage:transparentImage forState:UIControlStateNormal];
[progressBar setTrackImage:maxImage];
[progressBar setProgressImage:streamImage];

其中ProgressSlider将您的UISlider和progressBar引用到您的UIProgressView。

where ProgressSlider refers to your UISlider and progressBar to your UIProgressView.

你可以像这样以编程方式制作透明图像。

and you can make transparent image programmatically like this.

 UIGraphicsBeginImageContextWithOptions((CGSize){512,5}, NO, 0.0f);
    UIImage *transparentImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

你可以根据你的进度查看轨迹图像高度宽度改变它的大小...我用{512,5}您可以使用{1,1}作为默认滑块和进度视图。

you can change the size of it according to your progress view track image height width... I use {512,5} you can use {1,1} for default slider and progress view.

从此答案中获取的参考 https://stackoverflow.com/a/4570100/3933302

Reference taken form this answer https://stackoverflow.com/a/4570100/3933302

第2步:

现在我尝试设置UIProgressView的跟踪和进度图像。但它没有显示轨道和进度图像。然后我找到了这个解决方案......使用github中提供的这个库。 https://gist.github.com/JohnEstropia/9482567

Now I tried to set the track and progress image of UIProgressView. But it was not showing the track and progress image. then i found this solution...to use this library available in github. https://gist.github.com/JohnEstropia/9482567

现在我将UIProgressView的出现更改为JEProgressView,包括NIB和故事板中的那些。

Now I changed occurrences of UIProgressView to JEProgressView, including those in NIBs and storyboards.

基本上,您需要强制分配图像直接到UIProgressView的子UIImageViews。

Basically, you'd need to force assigning the images directly to the UIProgressView's children UIImageViews.

需要子类来覆盖layoutSubviews,你可以根据图像大小调整imageViews的高度。

The subclass is needed to override layoutSubviews, where you adjust the heights of the imageViews according to the image sizes.

参考本答案 https://stackoverflow.com/a/22322367/3933302

第3步

但现在的问题是你从哪个为滑块和progressView制作图像。本教程对此进行了解释,您也可以下载psd。

But now the question is from which will you make the image for slider and progressView. This is explained in this tutorial and You can download the psd also.

http://www.raywenderlich.com/32167/photoshop-tutorial-for-developers-creating-a-custom-uislider

第4步:

现在来编程部分 ..
使用可玩的持续时间可以显示流数据..(这是我最大的问题)

Now come to programming part.. by using the playable duration it is possible to show stream data..(which was my biggest problem)

使用 MPMovieLoadStatePlayable 我们可以得到什么时候缓冲区有足够的数据可以开始播放,但是在播放完成之前它可能会耗尽数据。

Using MPMovieLoadStatePlayable we can get when The buffer has enough data that playback can begin, but it may run out of data before playback finishes.

https://developer.apple.com /库/ IOS /文档/ MediaPlayer的/和借鉴e / MPMoviePlayerController_Class / index.html#// apple_ref / c / tdef / MPMovieLoadState

现在在您的PlayAudio方法中...放置此代码..

Now in your PlayAudio method...place this code..

-(void) playAudio:(NSURL *) url {
.............
streamTimer= [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(loadStateChange) userInfo:nil repeats:YES];
..........
}

在这里是loadStateChange的定义

and here is the definition of loadStateChange

-(void)loadStateChange
{
if(self.moviePlayer.loadState == MPMovieLoadStatePlayable){

    [self.moviePlayer play];

    slideTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
                                                  target:self
                                                selector:@selector(updateProgressUI)
                                                userInfo:nil
                                                 repeats:YES];
    [streamTimer invalidate];
}
else if(self.downloadList)
//if you are playing local file then it will show track with full red color
    progressBar.progress= appDel.moviePlayerController.moviePlayer.duration;
}

这里是updateProgressUI的定义

and here is the definition of updateProgressUI

- (void)updateProgressUI{
    if(self.moviePlayer.duration == self.moviePlayer.playableDuration){
        // all done
        [slideTimer invalidate];
    }
    progressBar.progress = self.moviePlayer.playableDuration/self.moviePlayer.duration ;

}

我希望这个完全自定义的滑块可以帮助你制作很多东西自定义播放器...我在步骤中解释了我的所有工作.....谢谢....

I hope this fully customize slider will help you lot in making custom player...I explained all my work in steps.....Thanks ....

这篇关于如何在iOS中使用MpMoviePlayerController在UISlider上显示缓冲数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆