美文网首页iOS
视频拉流实现

视频拉流实现

作者: 充满活力的早晨 | 来源:发表于2018-10-23 15:55 被阅读253次

    从上一篇我们知道了视频拉流就是播放从服务器拉取资源进行资源播放。这篇文章就是讲解如何播放视频。

    看了好多文章,普遍的都是用的IJKPlayer播放器进行播放。是菜鸟嘛,一点点学。先用IJKPlayer播放个视频玩玩再说。

    这里我采用别人编译好的框架进行播放。

    这里我采用的是CocoaPods,我使用pod search IJKPlayer,发现有好多人都包装了改框架,因此我就选了一个YWVideoPlayer

    集成方法

    • xcode 创建一个新工程
    • cd 到工程目录下
    • pod init
    • vim Podfile
    • 在打开的Podfile文件中添加pod 'YWVideoPlayer', '~> 1.0.3'
    • esc ,回车+q,wq 保存
    • pod install(文件比较大,下载比较慢,耐心等待)
    • open .
    • 打开工程 ,将target->general->deployment target 的目标版本改成9.0

    看下工程结构



    看工程结构,我们知道该作者这里只用了个sdwebimage库。并且IJKMediaFramework是编译好的文件可以直接使用。

    这里我们ViewController.h 文件修改如下

    
    #import "ViewController.h"
    #import "YWMediaPlayerView.h"
    @interface ViewController ()
    @property (nonatomic, strong)YWMediaPlayerView  *mediaPlayerView;
    @property (nonatomic, strong)UIView             *playerView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor whiteColor];
        
        _playerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, YWSCREEN_WIDTH, YWMinPlayerHeight)];
        _playerView.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview:_playerView];
        _mediaPlayerView = [[YWMediaPlayerView alloc]init];
        
        // 测试链接 mp4、rtmp、m3u8
        // NSString *testUrl = @"http://flv2.bn.netease.com/videolib3/1604/28/fVobI0704/SD/fVobI0704-mobile.mp4";
        // NSString *testUrl = @"rtmp://live.hkstv.hk.lxdns.com/live/hks";
    //    NSString *testUrl = @"http://dlhls.cdn.zhanqi.tv/zqlive/49427_jmACJ.m3u8";
        NSString * testUrl = @"rtmp://localhost:1935/rtmplive/room";
        [_mediaPlayerView playerViewWithUrl:testUrl WithTitle:@"视频的标题" WithView:_playerView  WithDelegate:self];
        
        // 这里如果是直播,则隐藏进度条以及时间等控件
        _mediaPlayerView.mediaControl.totalDurationLabel.hidden = YES;
        _mediaPlayerView.mediaControl.mediaProgressSlider.hidden = YES;
        _mediaPlayerView.mediaControl.currentTimeLabel.hidden = YES;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 自动播放
            [_mediaPlayerView.mediaControl playControl];
        });
    }
    @end
    

    运行程序,程序是现在的状态


    接着我们启动推流服务器推流数据

    ffmpeg -re -i abc.mp4 -vcodec libx264 -acodec aac -strict -2 -f flv rtmp://localhost:1935/rtmplive/room
    

    我们发现模拟器上在播放视频了。

    如果想在手机上播放,那么这里的
    rtmp://localhost:1935/rtmplive/room
    替换成局域网内的电脑的ip地址

    我们成功播放了视频。这其实是抛砖引玉了。我们还是需要简单的分析下该作者的源码实现。

    核心代码在这

    - (void)preparePlayer {
        
    #ifdef DEBUG
        [IJKFFMoviePlayerController setLogReport:YES];
        [IJKFFMoviePlayerController setLogLevel:k_IJK_LOG_DEBUG];
    #else
        [IJKFFMoviePlayerController setLogReport:NO];
        [IJKFFMoviePlayerController setLogLevel:k_IJK_LOG_INFO];
    #endif
        
        
        
        [IJKFFMoviePlayerController checkIfFFmpegVersionMatch:YES];
        
        
        IJKFFOptions *options = [IJKFFOptions optionsByDefault];
        
        if (self.player) {
            [self.player shutdown];
            [self.player.view removeFromSuperview];
    
            self.player=nil;
        }
        
        self.player = [[IJKFFMoviePlayerController alloc] initWithContentURL:self.url withOptions:options];
        self.player.view.frame = self.view.bounds;
        self.player.scalingMode = IJKMPMovieScalingModeAspectFit;
        self.player.shouldAutoplay = YES;
        
        [self.view  insertSubview:self.player.view belowSubview:self.mediaControl];
        
        self.mediaControl.delegatePlayer = self.player;
      
    }
    

    编译IJKPlayer

    官方提供的编译

    git clone https://github.com/Bilibili/ijkplayer.git ijkplayer-ios
    cd ijkplayer-ios
    git checkout -B latest k0.8.8
    
    ./init-ios.sh
    
    cd ios
    ./compile-ffmpeg.sh clean
    ./compile-ffmpeg.sh all
    
    # Demo
    #     open ios/IJKMediaDemo/IJKMediaDemo.xcodeproj with Xcode
    # 
    # Import into Your own Application
    #     Select your project in Xcode.
    #     File -> Add Files to ... -> Select ios/IJKMediaPlayer/IJKMediaPlayer.xcodeproj
    #     Select your Application's target.
    #     Build Phases -> Target Dependencies -> Select IJKMediaFramework
    #     Build Phases -> Link Binary with Libraries -> Add:
    #         IJKMediaFramework.framework
    #
    #         AudioToolbox.framework
    #         AVFoundation.framework
    #         CoreGraphics.framework
    #         CoreMedia.framework
    #         CoreVideo.framework
    #         libbz2.tbd
    #         libz.tbd
    #         MediaPlayer.framework
    #         MobileCoreServices.framework
    #         OpenGLES.framework
    #         QuartzCore.framework
    #         UIKit.framework
    #         VideoToolbox.framework
    #
    #         ... (Maybe something else, if you get any link error)
    # 
    

    一看就是采用终端的方式进行

    • 1.打开终端
    • 2.cd ~/Desktop/
    • 3.mkdir ijk
    • 4.cd ijk/
    • 5.git clone https://github.com/Bilibili/ijkplayer.git ijkplayer-ios
    • 6.cd ijkplayer-ios
      +7.git checkout -B latest k0.8.8
      +8. ./init-ios.sh (要是下载速度几kb,那么ctrl+c 取消重新运行试试看)
      +9. cd ios
      +10. ./compile-ffmpeg.sh clean
      +11. ./compile-ffmpeg.sh all

    第十一步骤出现下列错误


    image.png

    解决如下

    打开compile-ffmpeg.sh 文件,将FF_ALL_ARCHS_IOS8_SDK 改成arm64 i386 x86_64
    即FF_ALL_ARCHS_IOS8_SDK="arm64 i386 x86_64"

    如何将源文件导入到工程中引用编译不报错

    我用的xcode版本是10.0

    • 1.创建一个新的工程

    • 2.File -> Add Files to ... -> Select ios/IJKMediaPlayer/IJKMediaPlayer.xcodeproj


    • 3.选择工程的 target.

    • 4.选择Build Phases -> Target Dependencies -> Select IJKMediaFramework


      1. 选择 Build Phases -> Link Binary with Libraries -> Add:


    • 6.将ViewController.m文件改成ViewController.mm 文件

    • 7修改viewControlloer.m文件如下

    #import "ViewController.h"
    #import <IJKMediaFramework/IJKMediaFramework.h>
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        id<IJKMediaPlayback> playback = [[IJKFFMoviePlayerController alloc] initWithContentURL:nil  withOptions:nil];
        
        [playback shutdown];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    
    @end
    
    • 8.command+R 编译不出错,说明编译成功。

    使用IJKPlayer进行播放视频

    将ViewController修改成如下代码

    
    
    #import "ViewController.h"
    #import <IJKMediaFramework/IJKMediaFramework.h>
    
    @interface ViewController ()
    
    @property (atomic, strong) NSURL *url;
    @property (atomic, retain) id <IJKMediaPlayback> player;
    @property (weak, nonatomic) UIView *PlayerView;
    
    - (IBAction)play_btn:(id)sender;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        //网络视频
    //        self.url = [NSURL URLWithString:@"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
        //    _player = [[IJKAVMoviePlayerController alloc] initWithContentURL:self.url];
        
        //直播视频
    //    self.url = [NSURL URLWithString:@"http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8"];
        self.url = [NSURL URLWithString:@"rtmp://localhost:1935/rtmplive/room"];
        
        _player = [[IJKFFMoviePlayerController alloc] initWithContentURL:self.url withOptions:nil];
        
        UIView *playerView = [self.player view];
        
        UIView *displayView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, 180)];
        self.PlayerView = displayView;
        self.PlayerView.backgroundColor = [UIColor blackColor];
        [self.view addSubview:self.PlayerView];
        
        playerView.frame = self.PlayerView.bounds;
        playerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
        [self.PlayerView insertSubview:playerView atIndex:1];
        [_player setScalingMode:IJKMPMovieScalingModeAspectFill];
        [self installMovieNotificationObservers];
        
    }
    
    -(void)viewWillAppear:(BOOL)animated{
        if (![self.player isPlaying]) {
            [self.player prepareToPlay];
        }
    }
    
    #pragma Selector func
    
    - (void)loadStateDidChange:(NSNotification*)notification {
        IJKMPMovieLoadState loadState = _player.loadState;
        
        if ((loadState & IJKMPMovieLoadStatePlaythroughOK) != 0) {
            NSLog(@"LoadStateDidChange: IJKMovieLoadStatePlayThroughOK: %d\n",(int)loadState);
        }else if ((loadState & IJKMPMovieLoadStateStalled) != 0) {
            NSLog(@"loadStateDidChange: IJKMPMovieLoadStateStalled: %d\n", (int)loadState);
        } else {
            NSLog(@"loadStateDidChange: ???: %d\n", (int)loadState);
        }
    }
    
    - (void)moviePlayBackFinish:(NSNotification*)notification {
        int reason =[[[notification userInfo] valueForKey:IJKMPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
        switch (reason) {
            case IJKMPMovieFinishReasonPlaybackEnded:
                NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonPlaybackEnded: %d\n", reason);
                break;
                
            case IJKMPMovieFinishReasonUserExited:
                NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonUserExited: %d\n", reason);
                break;
                
            case IJKMPMovieFinishReasonPlaybackError:
                NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonPlaybackError: %d\n", reason);
                break;
                
            default:
                NSLog(@"playbackPlayBackDidFinish: ???: %d\n", reason);
                break;
        }
    }
    
    - (void)mediaIsPreparedToPlayDidChange:(NSNotification*)notification {
        NSLog(@"mediaIsPrepareToPlayDidChange\n");
    }
    
    - (void)moviePlayBackStateDidChange:(NSNotification*)notification {
        switch (_player.playbackState) {
            case IJKMPMoviePlaybackStateStopped:
                NSLog(@"IJKMPMoviePlayBackStateDidChange %d: stoped", (int)_player.playbackState);
                break;
                
            case IJKMPMoviePlaybackStatePlaying:
                NSLog(@"IJKMPMoviePlayBackStateDidChange %d: playing", (int)_player.playbackState);
                break;
                
            case IJKMPMoviePlaybackStatePaused:
                NSLog(@"IJKMPMoviePlayBackStateDidChange %d: paused", (int)_player.playbackState);
                break;
                
            case IJKMPMoviePlaybackStateInterrupted:
                NSLog(@"IJKMPMoviePlayBackStateDidChange %d: interrupted", (int)_player.playbackState);
                break;
                
            case IJKMPMoviePlaybackStateSeekingForward:
            case IJKMPMoviePlaybackStateSeekingBackward: {
                NSLog(@"IJKMPMoviePlayBackStateDidChange %d: seeking", (int)_player.playbackState);
                break;
            }
                
            default: {
                NSLog(@"IJKMPMoviePlayBackStateDidChange %d: unknown", (int)_player.playbackState);
                break;
            }
        }
    }
    
    #pragma Install Notifiacation
    
    - (void)installMovieNotificationObservers {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(loadStateDidChange:)
                                                     name:IJKMPMoviePlayerLoadStateDidChangeNotification
                                                   object:_player];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(moviePlayBackFinish:)
                                                     name:IJKMPMoviePlayerPlaybackDidFinishNotification
                                                   object:_player];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(mediaIsPreparedToPlayDidChange:)
                                                     name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
                                                   object:_player];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(moviePlayBackStateDidChange:)
                                                     name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
                                                   object:_player];
        
    }
    
    - (void)removeMovieNotificationObservers {
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:IJKMPMoviePlayerLoadStateDidChangeNotification
                                                      object:_player];
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:IJKMPMoviePlayerPlaybackDidFinishNotification
                                                      object:_player];
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
                                                      object:_player];
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
                                                      object:_player];
        
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (IBAction)play_btn:(id)sender {
        if (![self.player isPlaying]) {
            [self.player play];
        }else{
            [self.player pause];
        }
    }
    
    @end
    

    结果是


    image.png

    编译成framework 好多博客都有,这里不做多余赘述

    参考
    YWVideoPlayer
    IJKPlayer源码编译教程
    IJKPlayer github源码地址

    相关文章

      网友评论

        本文标题:视频拉流实现

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