iOS 本地視頻和網(wǎng)絡(luò)視頻流播放實(shí)例代碼
需求:最近公司需要做一個(gè)樓宇對(duì)講的功能:門口機(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ī)可以接聽,掛斷,手機(jī)接聽之后,室內(nèi)機(jī)不展示視頻,只是進(jìn)行轉(zhuǎn)發(fā)。)
簡(jiǎn)單點(diǎn)說就是手機(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ā)給門口機(jī)。
之前從來做過視頻播放都是本地文件的直接播放,從來沒有做過網(wǎng)絡(luò)視頻流的播放,百度了很多都是介紹框架怎么使用的,按著它的流程是行不通的,沒有一個(gè)詳細(xì)的使用流程!!!想哭呀!!!
這篇文章說一下本地視頻文件播放和網(wǎng)絡(luò)視頻播放以及三方框架的使用,有不對(duì)的地方歡迎指正!!!
#pragma mark -- 本地視頻文件播放
使用AVFoundation.framework
第一步:導(dǎo)入框架AVFoundation.framework
//經(jīng)過測(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.開始播放
[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
第二步: 將下載下來的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 //視頻流的路徑,外界傳過來的視頻流的地址 @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ì)有問題!!!真機(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.開始播放
[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
第二步: 將下載下來的IJK.zip解壓,IJK文件夾中的
1、IJKMediaFramework.framework
2、libcrypto.a
3、librtmp.a
4、libssl.a
總共4個(gè)拖入到你的工程中
第三步: 編寫代碼
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)沒有什么效果
[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í)沒有找到解決方案!!!(個(gè)人感覺應(yīng)該是方式二中的.a與系統(tǒng)的.tbd有沖突)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS實(shí)現(xiàn)實(shí)時(shí)檢測(cè)網(wǎng)絡(luò)狀態(tài)的示例代碼
- iOS 檢測(cè)網(wǎng)絡(luò)狀態(tài)的兩種方法
- iOS實(shí)時(shí)監(jiān)控網(wǎng)絡(luò)狀態(tài)的改變
- iOS中如何判斷當(dāng)前網(wǎng)絡(luò)環(huán)境是2G/3G/4G/5G/WiFi
- iOS 12+ 中檢測(cè)網(wǎng)絡(luò)訪問的方法
- 詳解iOS AFNetworking取消正在進(jìn)行的網(wǎng)絡(luò)請(qǐng)求
- iOS中從網(wǎng)絡(luò)獲取數(shù)據(jù)的幾種方法的比較
- iOS中多網(wǎng)絡(luò)請(qǐng)求的線程安全詳解
- 詳解IOS判斷當(dāng)前網(wǎng)絡(luò)狀態(tài)的三種方法
相關(guān)文章
iOS實(shí)現(xiàn)控制屏幕常亮不變暗的方法示例
最近在工作中遇到了要將iOS屏幕保持常亮的需求,所以下面這篇文章主要給大家介紹了關(guān)于利用iOS如何實(shí)現(xiàn)控制屏幕常亮不變暗的方法,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
IOS倒計(jì)時(shí)設(shè)置UIButton標(biāo)題title的抖動(dòng)問題
這篇文章主要介紹了IOS倒計(jì)時(shí)設(shè)置UIButton標(biāo)題title的抖動(dòng)問題,在發(fā)送驗(yàn)證碼后,button狀態(tài)需要變?yōu)閐isable,每隔一秒顯示倒計(jì)時(shí)時(shí)間,下面通過本文給大家介紹設(shè)置方法,一起看看吧2016-12-12
移動(dòng)端固定輸入框在底部會(huì)被鍵盤遮擋的解決方法(必看篇)
下面小編就為大家分享關(guān)于移動(dòng)端固定輸入框在底部會(huì)被鍵盤遮擋的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
CAMediaTiming ( 時(shí)間協(xié)議)詳解及實(shí)例代碼
這篇文章主要介紹了CAMediaTiming / 時(shí)間協(xié)議詳解及實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下2016-12-12
iOS瀑布流的簡(jiǎn)單實(shí)現(xiàn)(Swift)

