IOS使用ijkPlayer
2016-04-12 18:12
465 查看
前言
最近要做一个视频直播的项目,刚刚进入视频坑,开始自己琢磨AVPlayer,使用起来发现还挺多限制的,虽然当然知道要先把基础打好,相关基础链接:
网络播放器相关
http://blog.csdn.net/Matthew_Fan/article/category/1200896
HLS流媒体开发
http://blog.csdn.net/sdvch/article/details/14047281
HLS 比较全的中文博客
http://blog.csdn.net/Matthew_Fan/article/category/1200896
苹果 iOS 开发文档 (HLS)
https://developer.apple.com/streaming/
正文
但是急于做出一个demo交工,就简略的查了查攻略- -,发现B站的开源项目ijkPlayer是一个很好借鉴的项目,赶紧下下来试试, 链接:https://github.com/Bilibili/ijkplayer
具体怎么编译就不一一细说了,自己参照一下文档,这里我主要针对导入ijkplayer包并且使用做一些简单的说明。
完成github上的步骤之后,我们打开ios文件夹下的IJKMediaPlayer工程,发现他的output是一个framework包,那样就轻松了,
build&run以后,将framework包复制取出,可以从右侧栏直接查看路径,
或者直接右键进入路径
既然已经取出了framework文件,那么我们就将它导入到我们需要的工程里面,当然,前提是你不打算修改里面的东西,你也可以通过建立workplace加上建立依赖关系导入整个工程文件,省的每次修改都要重复导入包。ps:其他ijkplayerdemo上的包也最好一起导入了,不然可能会编译出错
4000
之后的内容可以参考ijkdemo中的ijkMoviePlayerViewController实现调用播放器,如果你不需要这么多东西也可以直接像我这样简单写:
ViewController
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;"><IJKMediaFramework/IJKMediaFramework.h></span></span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@interface</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">ViewController</span> : <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">UIViewController</span> </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span> (atomic, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">strong</span>) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> *url; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span> (atomic, retain) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> <IJKMediaPlayback> player; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">weak</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">IBOutlet</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIView</span> *PlayerView; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">weak</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">IBOutlet</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIButton</span> *playButton;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
然后就通过初试化一些基本设置就可以播放了:
1、url
2、PlayView
3、notification
4、prepareToPlay
简单把界面上的东西部署一下,url等初始化
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.url</span> = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> URLWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8"</span>]; _player = [[IJKFFMoviePlayerController alloc] initWithContentURL:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.url</span> withOptions:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIView</span> *playerView = [_player view]; playerView<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.frame</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.PlayerView</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.frame</span>; playerView<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.autoresizingMask</span> = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.PlayerView</span> insertSubview:playerView atIndex:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>]; [_player setScalingMode:IJKMPMovieScalingModeAspectFill]; [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> installMovieNotificationObservers]; </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>
别忘记了这个
<code class="hljs ini has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 102, 102);">[self.player prepareToPlay]</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
最后在你需要的地方调用播放等接口
<code class="hljs ini has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 102, 102);">[self.player play]</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
demo链接:http://download.csdn.net/detail/atomic123/9402481
相关文章推荐
- ios - loadView与viewDidload
- [IOS]Protocol协议及委托代理(Delegate)传值-用代码寻找storyboard相应视图
- ShareSDK在iOS9下注意事项
- iOS设备唯一标识(可以用版)
- iOS 关于信鸽推送点击推送通知的处理
- iOS 最简单的资料
- iOS 客户端MD5加密方法
- iOS中 imageNamed方法 非常多图片占用大量内存问题
- 应用之间跳转进行传值
- iOS之JSON解析和视频的简单播放
- 纪念一下我的短暂的ios时光
- iOS Core Animation系列之CADisplayLink
- ios-常见的循环引用
- iOS多线程编程之NSOperation
- iOS开发之百度地图大头针的自定义解决方法
- iOS中的initialize与load两个类方法简单理解
- ios开发常用的技巧
- IOS-日期处理
- iOS之轻松上手block(下)
- iOS之轻松上手block(上)