iOS视频播放之ijkplayer使用

作者: long弟弟 | 来源:发表于2017-04-16 17:41 被阅读6620次

iOS视频播放器,如果给一个URL视频地址,没要求的话,推荐使用ZFPlayer,这里也简单叙述一下

  1. GitHub官网下载ZFPlayer
    https://github.com/renzifeng/ZFPlayer 然后直接解压运行Player.xcworkspace即可
  2. 新建自己的工程项目
    添加Podfile
platform :ios, '9.0'
target ‘TestZFPlayer’ do
pod 'ZFPlayer'
end

打开终端命令行cd进入工程目录执行pod install
打开工程目录中cocopods生成的xcworkspace文件

  1. 在Viewcontroller中导入头文件
#import <ZFPlayer.h>

因为用到了布局,所以导入masonry

#import <Masonry.h>
@property (weak, nonatomic) IBOutlet ZFPlayerView *playView;
@property (nonatomic, strong) ZFPlayerView *playerView;
@property (atomic, strong) NSURL *url;
  1. viewDidLoad中
    self.playerView = [[ZFPlayerView alloc] init];
    [self.view addSubview:self.playerView];
    [self.playerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.left.right.equalTo(self.view);
        // Here a 16:9 aspect ratio, can customize the video aspect ratio
        make.height.equalTo(self.playView.mas_width).multipliedBy(9.0f/16.0f);
    }];
    self.url = [NSURL URLWithString:@"http://v1.mukewang.com/a45016f4-08d6-4277-abe6-bcfd5244c201/L.mp4"];
    ZFPlayerControlView *controlView = [[ZFPlayerControlView alloc] init];
    // model
    ZFPlayerModel *playerModel = [[ZFPlayerModel alloc] init];
    playerModel.fatherView = _playerView;
    playerModel.videoURL = self.url;
    playerModel.title = @"这是title";
    [self.playView playerControlView:controlView playerModel:playerModel];
    // delegate
    self.playView.delegate = self;
    // auto play the video
    [self.playView autoPlayTheVideo];
  1. 这样就可以播放视频了,同时带有暂定、播放、快进、快退、预览、全屏;真机可以调整音量、以及屏幕亮度等等功能
    注:ZFPlayer支持IB,所以拉的playerView,以上也对ZFPlayer作者开源精神表示感谢!

由于项目中计划使用ijkplayer,所以从一无所知到播放这个流程在此记录一下,特此感谢StarGazer_C

  1. 百度ijkplayer
  • 搜索ijkplayer.png
  1. 点击GitHub-Bilibili。。。进入GitHub官网
  • GitHub下载.png
  1. 下载zip文件,并解压,尝试运行Demo,因为demo不能允许的话,何况你的。。。
    Demo路径:../ijkplayer-master/ios/IJKMediaDemo
    Build一下就报错了,查看官方文档需要Build环境
  • 尝试运行Demo.png
  1. 安装git(mac自带)、yasm
  • Before Build.png
  1. Build iOS
  • Build iOS.png
  1. 打开终端cd到init-ios.sh文件的父目录执行./init-ios.sh,根据网络情况要等~~
    然后cd到ios文件夹执行./complie-ffmpeg.sh clean,这个很快
    再执行./complie-ffmpeg.sh all,执行这个电脑有点卡,稍后就OK了
    再次运行IJKMediaDemo.xcodeproj就OK了。。。只有Online Samples可以播放一下,播放效果
  • 运行Demo.png
  1. 以上证明环境OK
  2. 运行IJKMediaPlayer.xcodeproj生成IJKMediaFramework.framework
  • 运行Player.png
  1. 模拟器运行出来的只能在模拟器上运行,真机调试运行的只能在真机上运行,这里就需要合并framework
    这是两个不同的运行环境,需要合并IJKMediaFramework.framework下的IJKMediaFramework
    运行命令行:
lipo -create /Users/mac/Desktop/Debug-iphonesimulator/IJKMediaFramework.framework /Users/mac/Desktop/Debug-iphoneos/IJKMediaFramework.framework/IJKMediaFramework -output /Users/mac/Desktop/lipo/IJKMediaFramework
  • 合并framework.png

    验证是否合并成功就是lipo中的IJKMediaFramework是否生产,还有就是使用lipo -info查看文件属性吧算是会提示x86_64 arm64
    arm64表示是真机的文件,x86_64表示是模拟器支持的文件,显示两个就表示合并成功

  • 合并成功标识.png
  1. 注意区分debug版本还是release版本,调试阶段使用debug就行,这个切换在Xcode中product->scheme->edit scheme中更改
  2. 有了库文件就可以拖到自己的工程中了,导入#import <IJKMediaFramework/IJKMediaFramework.h>
    Build一下 success,然后导入视频地址测试播放,编译link错误
  • link_error.png
  1. 导入ijkplayer的readme中提到的相关库
  • 导入相关库.png
  1. 再次Build还是link错误,不过这次编程了__gxx_personality_v0…,
  • Paste_Image.png
  1. 这是还差一个库libstdc++.tbd,导入即可;故事板中拉个uiview(显示播放界面)、button(控制播放、暂停)
    整个Viewcontroller就一行代码,别的什么都没有!
#import "ViewController.h"
#import <IJKMediaFramework/IJKMediaFramework.h>
@interface ViewController ()
@property (atomic, retain) id <IJKMediaPlayback> player;
@property (weak, nonatomic) IBOutlet UIView *playView;
@property (weak, nonatomic) IBOutlet UIButton *playBtn;
@end
@implementation ViewController 
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (![_player isPlaying]) {
        [self.player prepareToPlay];
    }
}
-(void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/a45016f4-08d6-4277-abe6-bcfd5244c201/L.mp4"];
    _player = [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:nil];
    UIView *playerView = [_player view];
    playerView.frame = self.playView.frame;
    [self.view addSubview:playerView];
}
-(IBAction)clickedAction:(id)sender {
    if (![_player isPlaying]) {
        [self.player play];
        [self.playBtn setTitle:@"Pause" forState:UIControlStateNormal];
    }else {
        [self.player pause];
        [self.playBtn setTitle:@"Play" forState:UIControlStateNormal];
    }
}
@end
  1. 综上所述,只要有合成好的framework,把它拖到工程当中,Link相关库即可播放,所以为了省时省力,我将我编译好的framework放到百度网盘上了,需要的朋友可以下载使用,提取密码:cat9,如有问题,欢迎反馈,谢谢!

相关文章

网友评论

  • RWz_my:楼主你好,我合并framework也导入到项目中了,但只能播放http格式的视频,换成ftsp就只剩下个黑框了,你有遇到这种情况吗?求解救....:sob:
  • 冰三尺:想请教一个问题, 播放直播视频(比如花椒直播)和我们平时看视频(比如看腾讯视频)在播放层面有什么区别?
  • 不会游泳的飞鱼:楼主,我想问一下怎么播放一段本地不完整的视频
  • qizhuo10:你合成打包的framwork有问题,没有图像,目前我xcode9.3,直接自己合成时候有问题,直接用你打好的有问题,你可以试试看
  • 然哥哥:朋友,这个框架能实现缓存功能不。
    long弟弟:@然哥哥 ijkplayer本身没有缓存机制,可以选择使用AVPlayer来实现这个功能,详细步骤可参看博客http://sky-weihao.github.io/2015/10/06/Video-streaming-and-caching-in-iOS/
  • d0afe70ea42f:请问一下 我在执行./compile-ffmpeg.sh all这句话之后报以下错误 是那个版本不对么,都是按照流程走的。。。

    If you think configure made a mistake, make sure you are using the latest
    version from Git. If the latest version fails, report the problem to the
    ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.
    Include the log file "ffbuild/config.log" produced by configure as this will help solve the problem
  • 不辣先生:请问如何获取当前使用的ijk版本号??我不知道项目中使用的是哪个版本的
  • 指尖上的代码:avi格式的视频 用ijkPlayer 播放,要配置什么吗
  • a2f037f7bfbb:为何在模拟器上可以播放,真机不行
  • 风子007:想问一下 这个在 视频播放的时候 会导致 内存的增加 这个 不知道你这边意识到这个问题了没,求教一下
  • 微光星芒:从网盘里下载解压出错. 无法解压
    CC_iOS:你下的哪个框架
    微光星芒:@long弟弟 :joy: 我重新下了一个框架 整好了.
    long弟弟:@微光星芒 ...要不你再下载一遍试试?
  • 61a13fde3f8e:用ijk更换源 必须得杀掉播放器重新创建新的嘛
    b650b0446e24:有没有别的方法,这样是不是太麻烦了,切换视频源就要杀死当前播放器
    本人ios小白
  • 陈永发:请你一下怎么设置视频播放格式呀
    long弟弟: @陈永发 抱歉,具体设置格式我目前也在学习中,播放的参数就是一个完整的url包含着格式呀。

本文标题:iOS视频播放之ijkplayer使用

本文链接:https://www.haomeiwen.com/subject/egdoattx.html