溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS中本地視頻和網(wǎng)絡(luò)視頻流播放的示例分析

發(fā)布時間:2021-07-09 09:43:23 來源:億速云 閱讀:462 作者:小新 欄目:移動開發(fā)

小編給大家分享一下iOS中本地視頻和網(wǎng)絡(luò)視頻流播放的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

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

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

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

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

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

使用AVFoundation.framework

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

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

iOS中本地視頻和網(wǎng)絡(luò)視頻流播放的示例分析

第二步: 拖入一個視頻文件到你的項目中 

第三步: 代碼實現(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)建一個視圖播放的圖層
 AVPlayerLayer * layer = [AVPlayerLayer playerLayerWithPlayer:player];
 
 //5.設(shè)置圖層的大小
 layer.frame = CGRectMake(0, 0, EYScreenWidth, EYScreenHeight);
 
 //6.添加到控制器的view的圖層上面
 [self.view.layer addSublayer:layer];
 
 //7.開始播放
 [player play];
}

@end

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

方式一:MobileVLCKit.framework

第一步: 下載MobileVLCKit.framework

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

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

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

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

iOS中本地視頻和網(wǎng)絡(luò)視頻流播放的示例分析

第四步: 選擇finish

iOS中本地視頻和網(wǎng)絡(luò)視頻流播放的示例分析

第五步:添加依賴庫

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個

完成之后如圖所示:

iOS中本地視頻和網(wǎng)絡(luò)視頻流播放的示例分析

第六步: 使用框架

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

//視頻流的路徑,外界傳過來的視頻流的地址
@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)建播放視圖,模擬器測試會有問題!!!真機可以正常播放
 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.開始播放
 [self.player play];
}

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

@end

第七步: 真機測試

Command + R 運行報錯

iOS中本地視頻和網(wǎng)絡(luò)視頻流播放的示例分析 

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

iOS中本地視頻和網(wǎng)絡(luò)視頻流播放的示例分析 

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

 iOS中本地視頻和網(wǎng)絡(luò)視頻流播放的示例分析

再次運行就是OK了!!!

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

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

方式二: IJKMediaFramework 

第一步: 下載IJKMediaFramework

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

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

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

 第二步: 將下載下來的IJK.zip解壓,IJK文件夾中的

1、IJKMediaFramework.framework

2、libcrypto.a

3、librtmp.a

4、libssl.a

總共4個拖入到你的工程中

第三步: 編寫代碼 

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è)置打印級別, 測試發(fā)現(xiàn)沒有什么效果
 [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

注意點:方式一和方式二只能使用一個,因為他們兩個會有沖突,暫時沒有找到解決方案!!!(個人感覺應(yīng)該是方式二中的.a與系統(tǒng)的.tbd有沖突)

以上是“iOS中本地視頻和網(wǎng)絡(luò)視頻流播放的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI