iOS开发使用AVPlayer、MoviePlayer播放本地、网络视频

Github下载地址: https://github.com/ChinaFishNews/VideoPlay.git

Demo中使用的ALMoviePlayerController是一个框架,继承了系统的MPMoviePlayerController,做了一些扩展和封装 。

代码我就不过多解读了,相对是比较清晰的,仅是为了实现播放功能,虽然很简陋且还有一些问题。

其中在使用AVPlayer播放视频的时候滑动进度条稍微有些不灵敏,之前在做项目的时候因为是使用Swift语言实现的,所以功能没法直接拿出来,就用OC也勉强实现了这个功能。项目中的解决方法是通过在进度条上画了热点,判断当前点击的点或滑动停止的点占总长度的百分比,从而定位到正确的播放位置。代码如下:

画出热点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
func drawHotField()
{
let width:CGFloat = self.view.bounds.size.width
let height:CGFloat = self.view.bounds.size.height

self.buttonMaintain = CGPathCreateMutable()

if self.whetherPortrate == true
{
CGPathMoveToPoint(buttonMaintain,nil, 38, height-60)
CGPathAddLineToPoint(buttonMaintain, nil, width-170, height-60)
CGPathAddLineToPoint(buttonMaintain, nil, width-170, height)
CGPathAddLineToPoint(buttonMaintain, nil, 38, height)
CGPathCloseSubpath(self.buttonMaintain)
}else
{
CGPathMoveToPoint(buttonMaintain,nil, 60, 65)
CGPathAddLineToPoint(buttonMaintain, nil, 60, height-150)
CGPathAddLineToPoint(buttonMaintain, nil, 0, height-150)
CGPathAddLineToPoint(buttonMaintain, nil, 0,65 )
CGPathCloseSubpath(self.buttonMaintain)
}
Logger.info("pathPoint: \(self.buttonMaintain)")
}

点击

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
func controlViewClicked(tapGesture:UITapGestureRecognizer)
{
self.drawHotField()
Logger.info("tap:---------------")
let tapPoint:CGPoint = tapGesture.locationInView(self.view)

if CGPathContainsPoint(self.buttonMaintain,nil,tapPoint,false) == true
{
var currentX = tapPoint.x
var percent = (currentX-38)/self.progessSlider!.frame.size.width

if self.whetherPortrate != true
{
currentX = tapPoint.y
percent = (currentX-65)/self.progessSlider!.frame.size.width
}

let duration: CMTime = self.player!.currentItem!.duration
var currentSecond: Float64 = CMTimeGetSeconds(duration)
currentSecond = currentSecond * Float64(percent)
self.progessSlider?.value = Float( currentSecond / CMTimeGetSeconds(self.player!.currentItem!.duration))
self.player?.currentItem!.seekToTime(CMTimeMakeWithSeconds(Float64(Float(CMTimeGetSeconds(duration)) * self.progessSlider!.value), Int32(CMTimeGetSeconds(duration))))
self.progressLabel?.text = self.getCurrentTime(currentSecond)

self.judgeWhetherEnd()
}
}

滑动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
func controlViewPaned(panGesture:UIPanGestureRecognizer)
{
self.drawHotField()
Logger.info("pan:---------------")
let tapPoint:CGPoint = panGesture.locationInView(self.view)

if panGesture.state == UIGestureRecognizerState.Began || panGesture.state == UIGestureRecognizerState.Changed
{
if CGPathContainsPoint(self.buttonMaintain,nil,tapPoint,false) == true
{
var currentX = tapPoint.x
var percent = (currentX-38)/self.progessSlider!.frame.size.width

if self.whetherPortrate != true
{
currentX = tapPoint.y
percent = (currentX-65)/self.progessSlider!.frame.size.width
}

let duration: CMTime = self.player!.currentItem!.duration
var currentSecond: Float64 = CMTimeGetSeconds(duration)
currentSecond = currentSecond * Float64(percent)
self.progessSlider?.value = Float( currentSecond / CMTimeGetSeconds(self.player!.currentItem!.duration))
self.player?.currentItem!.seekToTime(CMTimeMakeWithSeconds(Float64(Float(CMTimeGetSeconds(duration)) * self.progessSlider!.value), Int32(CMTimeGetSeconds(duration))))
self.progressLabel?.text = self.getCurrentTime(currentSecond)

self.judgeWhetherEnd()
}
}s
}


如有任何疑问或问题请联系我:fishnewsdream@gmail.com,欢迎交流,共同提高!

Objective-C/Swift技术开发交流群201556264,讨论何种技术并不受限,欢迎各位大牛百家争鸣!

微信公众号OldDriverWeekly,欢迎关注并提出宝贵意见

老司机iOS周报,欢迎关注或订阅

刚刚在线工作室,欢迎关注或提出建设性意见!

刚刚在线论坛, 欢迎踊跃提问或解答!

如有转载,请注明出处,谢谢!

本站总访问量 本文总阅读量