iOS 本地視頻和網(wǎng)絡(luò)視頻流播放實(shí)例代碼
需求:最近公司需要做一個樓宇對講的功能:門口機(jī)(連接WIFI)撥號對室內(nèi)機(jī)(對應(yīng)的WIFI)的設(shè)備進(jìn)行呼叫,室內(nèi)機(jī)收到呼叫之后將對收到的數(shù)據(jù)進(jìn)行UDP廣播的轉(zhuǎn)發(fā),手機(jī)(連接對應(yīng)的WIFI)收到視頻流之后,實(shí)時的展示視頻數(shù)據(jù)(手機(jī)可以接聽,掛斷,手機(jī)接聽之后,室內(nèi)機(jī)不展示視頻,只是進(jìn)行轉(zhuǎn)發(fā)。)
簡單點(diǎn)說就是手機(jī)客戶端需要做一個類似于直播平臺的軟件,可以實(shí)時的展示視頻,實(shí)時的播放接收到的聲音數(shù)據(jù),并且實(shí)時將手機(jī)麥克風(fēng)收到的聲音回傳給室內(nèi)機(jī),室內(nèi)機(jī)負(fù)責(zé)轉(zhuǎn)發(fā)給門口機(jī)。
之前從來做過視頻播放都是本地文件的直接播放,從來沒有做過網(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)入吧!!!

第二步: 拖入一個視頻文件到你的項(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)建一個視圖播放的圖層
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

第五步:添加依賴庫
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個
完成之后如圖所示:

第六步: 使用框架
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)建播放視圖,模擬器測試會有問題!!!真機(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ī)測試
Command + R 運(yùn)行報(bào)錯
在工程設(shè)置中,Setting搜索bitcode,將Yes修改為No
如果出現(xiàn)下圖錯誤,將對應(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個拖入到你的工程中
第三步: 編寫代碼
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
注意點(diǎn):方式一和方式二只能使用一個,因?yàn)樗麄儍蓚€會有沖突,暫時沒有找到解決方案!!!(個人感覺應(yīng)該是方式二中的.a與系統(tǒng)的.tbd有沖突)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS實(shí)現(xiàn)實(shí)時檢測網(wǎng)絡(luò)狀態(tài)的示例代碼
- iOS 檢測網(wǎng)絡(luò)狀態(tài)的兩種方法
- iOS實(shí)時監(jiān)控網(wǎng)絡(luò)狀態(tài)的改變
- iOS中如何判斷當(dāng)前網(wǎng)絡(luò)環(huán)境是2G/3G/4G/5G/WiFi
- iOS 12+ 中檢測網(wǎng)絡(luò)訪問的方法
- 詳解iOS AFNetworking取消正在進(jìn)行的網(wǎng)絡(luò)請求
- iOS中從網(wǎng)絡(luò)獲取數(shù)據(jù)的幾種方法的比較
- iOS中多網(wǎng)絡(luò)請求的線程安全詳解
- 詳解IOS判斷當(dāng)前網(wǎng)絡(luò)狀態(tài)的三種方法
相關(guān)文章
淺談iOS中幾個常用協(xié)議 NSCopying/NSMutableCopying
下面小編就為大家分享一篇淺談iOS中幾個常用協(xié)議 NSCopying/NSMutableCopying,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
iOS中幾種定時器的實(shí)現(xiàn)小結(jié)
這篇文章主要介紹了iOS中幾種定時器的實(shí)現(xiàn)小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
iOS開發(fā)之UITableView左滑刪除等自定義功能
今天來給大家介紹下iOS開發(fā)中UITableView左滑實(shí)現(xiàn)微信中置頂,刪除等功能。對大家開發(fā)iOS具有一定的參考借鑒價值,有需要的朋友們一起來看看吧。2016-09-09
IOS9.0 LaunchScreen.StroyBoard自定義啟動圖片詳解
這篇文章主要介紹了IOS9.0 LaunchScreen.StroyBoard自定義啟動圖片詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02
Flutter?Widgets之標(biāo)簽類控件Chip詳解
這篇文章主要為大家介紹了Flutter?Widgets之標(biāo)簽類控件Chip詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
iOS應(yīng)用開發(fā)中UIScrollView滾動視圖的基本用法總結(jié)
這篇文章主要介紹了iOS應(yīng)用開發(fā)中UIScrollView滾動視圖的基本用法總結(jié),作者還介紹了重寫UIScrollView中的hitTest方法來解決長按的事件問題,需要的朋友可以參考下2016-02-02

