IJKMediaFramework第三方庫的使用


大多數做直播的時候使用 FFMpeg.  IJKMediaFramework也是基於FFMpeg封裝 使用起來比較簡單,個人覺得如果有能力可以使用 FFMpeg , 使用 FFMpeg對 內存的占用比IJKMediaFramework少

關於IJKMediaFramework 集成 可以關注網上別人寫的 githubhttp://blog.csdn.net/github_33362155/article/details/51801499  

 

//

//  PlayerViewController.m

//  LittleLoveLive

//

//  Created by Apple on 16/7/20.

//  Copyright © 2016年 YJ. 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM