美文网首页
IJKMediaFramework第三方库的使用

IJKMediaFramework第三方库的使用

作者: 宁梓茞 | 来源:发表于2018-01-10 22:10 被阅读0次

    大多数做直播的时候使用 FFMpeg. IJKMediaFramework也是基于FFMpeg封装 使用起来比较简单,个人觉得如果有能力可以使用 FFMpeg , 使用 FFMpeg对 内存的占用比IJKMediaFramework少

    关于IJKMediaFramework 集成 可以关注网上别人写的 github:http://blog.csdn.net/github_33362155/article/details/51801499

    //
    //  PlayerViewController.m
    //  LittleLoveLive
    //
    //  Created by Apple on 16/7/20.
    //  Copyright © 2016年 Beryl. All rights reserved.
    //
    
    #import "PlayerViewController.h"
    #import <IJKMediaFramework/IJKMediaFramework.h>
    #import <Accelerate/Accelerate.h>
    
    @interface PlayerViewController ()
    
    @property (atomic, strong) NSURL *url;
    @property (atomic, retain) id <IJKMediaPlayback> player;
    @property (weak, nonatomic) UIView *PlayerView;
    @property (nonatomic, strong)UIImageView *dimIamge;
    
    @end
    
    @implementation PlayerViewController
    
    -(void)viewWillAppear:(BOOL)animated{
        
        [super viewWillAppear:YES];
        
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        
        if (![self.player isPlaying]) {
            [self.player prepareToPlay];
        }
    }
    
    - (void)viewDidDisappear:(BOOL)animated {
    
        [super viewDidDisappear:YES];
        [self.navigationController setNavigationBarHidden:NO animated:NO];
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.view.backgroundColor = [UIColor blackColor];
    
        self.url = [NSURL URLWithString:@"http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8"];
        _player = [[IJKFFMoviePlayerController alloc] initWithContentURL:self.url withOptions:nil];
        UIView *playerView = [self.player view];
        UIView *displayView = [[UIView alloc] initWithFrame:self.view.bounds];
        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];
    
        [self loadingView];
        [self changeBackBtn];
    
    }
    
    //关闭
    - (void)goBackBtnClick {
    
        //停止播放
        [self.player pause];
    
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    #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 {
        
        _dimIamge.hidden = YES;
        
        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)changeBackBtn
    {
        // 返回
        UIButton * backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        backBtn.frame = CGRectMake(kScreenWidth-50.0f, 10.0f, 40.0f, 40.0f);
        [backBtn setImage:[UIImage imageNamed:@"close_btn_show_view.png"] forState:(UIControlStateNormal)];
        [backBtn addTarget:self action:@selector(goBackBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
        backBtn.layer.shadowColor = [UIColor blackColor].CGColor;
        backBtn.layer.shadowOffset = CGSizeMake(0, 0);
        backBtn.layer.shadowOpacity = 0.5;
        backBtn.layer.shadowRadius = 1;
        [self.view addSubview:backBtn];
    }
    
    //加载拉丝模糊视图
    - (void)loadingView {
    
        self.dimIamge = [[UIImageView alloc] initWithFrame:self.view.bounds];
        [self.dimIamge sd_setImageWithURL:[NSURL URLWithString:self.urlStr] placeholderImage:IMAGENAMED(@"bg_loading.png") options:SDWebImageCacheMemoryOnly];
        UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        visualEffectView.frame = _dimIamge.bounds;
        [_dimIamge addSubview:visualEffectView];
        [self.view addSubview:_dimIamge];
    
    }
    
    - (void)dealloc {
        
        [self.player shutdown];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    
    }
    
    
    @end
    

    相关文章

      网友评论

          本文标题:IJKMediaFramework第三方库的使用

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