欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS 本地視頻和網(wǎng)絡(luò)視頻流播放實(shí)例代碼

 更新時(shí)間:2017年07月03日 09:20:59   作者:人生為代碼而活  
本篇文章主要介紹了iOS 本地視頻和網(wǎng)絡(luò)視頻流播放實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

需求:最近公司需要做一個(gè)樓宇對(duì)講的功能:門(mén)口機(jī)(連接WIFI)撥號(hào)對(duì)室內(nèi)機(jī)(對(duì)應(yīng)的WIFI)的設(shè)備進(jìn)行呼叫,室內(nèi)機(jī)收到呼叫之后將對(duì)收到的數(shù)據(jù)進(jìn)行UDP廣播的轉(zhuǎn)發(fā),手機(jī)(連接對(duì)應(yīng)的WIFI)收到視頻流之后,實(shí)時(shí)的展示視頻數(shù)據(jù)(手機(jī)可以接聽(tīng),掛斷,手機(jī)接聽(tīng)之后,室內(nèi)機(jī)不展示視頻,只是進(jìn)行轉(zhuǎn)發(fā)。)

簡(jiǎn)單點(diǎn)說(shuō)就是手機(jī)客戶端需要做一個(gè)類似于直播平臺(tái)的軟件,可以實(shí)時(shí)的展示視頻,實(shí)時(shí)的播放接收到的聲音數(shù)據(jù),并且實(shí)時(shí)將手機(jī)麥克風(fēng)收到的聲音回傳給室內(nèi)機(jī),室內(nèi)機(jī)負(fù)責(zé)轉(zhuǎn)發(fā)給門(mén)口機(jī)。

之前從來(lái)做過(guò)視頻播放都是本地文件的直接播放,從來(lái)沒(méi)有做過(guò)網(wǎng)絡(luò)視頻流的播放,百度了很多都是介紹框架怎么使用的,按著它的流程是行不通的,沒(méi)有一個(gè)詳細(xì)的使用流程!!!想哭呀!!!

這篇文章說(shuō)一下本地視頻文件播放和網(wǎng)絡(luò)視頻播放以及三方框架的使用,有不對(duì)的地方歡迎指正!!!

 #pragma mark -- 本地視頻文件播放

使用AVFoundation.framework

 第一步:導(dǎo)入框架AVFoundation.framework

//經(jīng)過(guò)測(cè)試:不導(dǎo)入這個(gè)框架也能播放,在第三步使用的時(shí)候?qū)刖托辛?為了不出現(xiàn)未知的BUG還是乖乖的導(dǎo)入吧!!!

第二步: 拖入一個(gè)視頻文件到你的項(xiàng)目中 

第三步: 代碼實(shí)現(xiàn)

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h> //需要導(dǎo)入框架

#define EYScreenWidth [[UIScreen mainScreen] bounds].size.width
#define EYScreenHeight [[UIScreen mainScreen] bounds].size.height

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];
 
 //1.從mainBundle獲取test.mp4的具體路徑
 NSString * path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
 //2.文件的url
 NSURL * url = [NSURL fileURLWithPath:path];
 
 //3.根據(jù)url創(chuàng)建播放器(player本身不能顯示視頻)
 AVPlayer * player = [AVPlayer playerWithURL:url];
 
 //4.根據(jù)播放器創(chuàng)建一個(gè)視圖播放的圖層
 AVPlayerLayer * layer = [AVPlayerLayer playerLayerWithPlayer:player];
 
 //5.設(shè)置圖層的大小
 layer.frame = CGRectMake(0, 0, EYScreenWidth, EYScreenHeight);
 
 //6.添加到控制器的view的圖層上面
 [self.view.layer addSublayer:layer];
 
 //7.開(kāi)始播放
 [player play];
}

@end

#pragma mark -- 網(wǎng)絡(luò)視頻流播放

方式一:MobileVLCKit.framework

第一步: 下載MobileVLCKit.framework

 1. 可以去百度官網(wǎng)地址,上面有詳細(xì)的編譯步驟,GitHub上面也有詳細(xì)的教程!!!--->之后直接進(jìn)行第六步!!!

 2. 我已經(jīng)編譯好了 真機(jī)和模擬器都可以使用的: MobileVLCKit.framework

 鏈接: https://pan.baidu.com/s/1pLz7DTx密碼: te5p

第二步: 將下載下來(lái)的zip解壓,MobileVLCKit文件夾中的MobileVLCKit.framework 拖入到你的工程中

第四步: 選擇finish

第五步:添加依賴庫(kù)

1:  AudioToolbox.framework

2:  VideoToolbox.framework

3:  CoreMedia.framework

4:  CoreVideo.framework

5:  CoreAudio.framework

6:  AVFoundation.framework

7:  MediaPlayer.framework

8:  libstdc++.6.0.9.tbd

9:  libiconv.2.tbd

10: libc++.1.tbd

11: libz.1.tbd

12: libbz2.1.0.tbd

 共12個(gè)

完成之后如圖所示:

第六步: 使用框架

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

//視頻流的路徑,外界傳過(guò)來(lái)的視頻流的地址
@property (nonatomic, copy) NSString * rtspPath;

@end

 ViewController.m

#import "ViewController.h"
#import <MobileVLCKit/MobileVLCKit.h>

//屏幕寬高的宏
#define EYScreenWidth [[UIScreen mainScreen] bounds].size.width
#define EYScreenHeight [[UIScreen mainScreen] bounds].size.height

@interface ViewController ()

//視頻播放
@property (nonatomic, strong) VLCMediaPlayer *player;

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];

 //1.創(chuàng)建播放視圖,模擬器測(cè)試會(huì)有問(wèn)題!!!真機(jī)可以正常播放
 UIView *videoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EYScreenWidth, EYScreenHeight)];
 [self.view addSubview:videoView];

 //2.創(chuàng)建播放器
 self.player = [[VLCMediaPlayer alloc] initWithOptions:nil];

 //3.設(shè)置播放圖層
 self.player.drawable = videoView;

 //4.設(shè)置播放的路徑
 self.player.media = [VLCMedia mediaWithURL:[NSURL URLWithString:self.rtspPath]];

 //5.開(kāi)始播放
 [self.player play];
}

- (void)dealloc
{
 if (self.player.isPlaying) {
  [self.player stop];
 }
}

@end

第七步: 真機(jī)測(cè)試

Command + R 運(yùn)行報(bào)錯(cuò)

 

 在工程設(shè)置中,Setting搜索bitcode,將Yes修改為No

 

如果出現(xiàn)下圖錯(cuò)誤,將對(duì)應(yīng)文件的第38行代碼注釋掉就行了!

 

再次運(yùn)行就是OK了!!!

如果不好使嘗試將ViewController.m----->ViewController.mm

如果上面的路徑是本地路徑的話,是可以播放本地視頻的!!!

方式二: IJKMediaFramework 

第一步: 下載IJKMediaFramework

 1. 可以去百度官網(wǎng)地址,上面有詳細(xì)的編譯步驟,GitHub上面也有詳細(xì)的教程!!! -->之后直接進(jìn)行第三步!!!

 2. 我已經(jīng)編譯好了 真機(jī)和模擬器都可以使用的:IJKMediaFramework

鏈接: https://pan.baidu.com/s/1o8G4ETG密碼: 3cbr   

 第二步: 將下載下來(lái)的IJK.zip解壓,IJK文件夾中的

1、IJKMediaFramework.framework

2、libcrypto.a

3、librtmp.a

4、libssl.a

總共4個(gè)拖入到你的工程中

第三步: 編寫(xiě)代碼 

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

//視頻流的路徑
@property (nonatomic, copy) NSString * rtspPath;

@end

ViewController.m

#import "ViewController.h"
#import <IJKMediaFramework/IJKMediaFramework.h>

// 宏定義
#define EYScreenBounds [UIScreen mainScreen].bounds

@interface ViewController ()

@property (nonatomic, strong) IJKFFMoviePlayerController * ijkPlayer;

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];

 //初始化播放控制器
 self.ijkPlayer = [[IJKFFMoviePlayerController alloc] initWithContentURLString:self.rtspPath withOptions:nil];

 //設(shè)置打印級(jí)別, 測(cè)試發(fā)現(xiàn)沒(méi)有什么效果
 [IJKFFMoviePlayerController setLogLevel:k_IJK_LOG_DEBUG];

 //設(shè)置控制器的view大小
 self.ijkPlayer.view.frame = EYScreenBounds;

 //控制器的view添加到自身的view上面
 [self.view addSubview:self.ijkPlayer.view];
}

- (void)viewWillAppear:(BOOL)animated
{
 [super viewWillAppear:animated];

 if (!self.ijkPlayer.isPlaying) {
  //播放
  [self.ijkPlayer prepareToPlay];
 }
}

- (void)viewWillDisappear:(BOOL)animated
{
 [super viewWillDisappear:animated];

 if (self.ijkPlayer.isPlaying) {
  //關(guān)閉
  [self.ijkPlayer shutdown];
 }
}

@end

注意點(diǎn):方式一和方式二只能使用一個(gè),因?yàn)樗麄儍蓚€(gè)會(huì)有沖突,暫時(shí)沒(méi)有找到解決方案!!!(個(gè)人感覺(jué)應(yīng)該是方式二中的.a與系統(tǒng)的.tbd有沖突)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS瀑布流的簡(jiǎn)單實(shí)現(xiàn)(Swift)

    iOS瀑布流的簡(jiǎn)單實(shí)現(xiàn)(Swift)

    這篇文章主要介紹了iOS瀑布流的簡(jiǎn)單實(shí)現(xiàn),說(shuō)到瀑布流, 或許大家都不陌生, 瀑布流的實(shí)現(xiàn)也有很多種! 本文使用兩種方法介紹,有興趣的可以了解一下。
    2016-11-11
  • 最新評(píng)論