IOS local video and network video stream playback, ios video network video stream

Source: Internet
Author: User
Tags file url

IOS local video and network video stream playback, ios video network video stream

Requirement: Recently, the company needs to implement a building intercom function: The Door Machine (connected to WIFI) dialing calls the indoor device (corresponding to WIFI, after receiving the call, the indoor host forwards the received data through UDP broadcast. After the mobile phone (connected to the corresponding WIFI) receives the video stream, the mobile phone displays the video data in real time (the mobile phone can answer and stop, after the phone receives the answer, the indoor host does not display the video, but forwards the video .)

Simply put, the mobile client needs to build a software similar to the live broadcasting platform to display videos and play received audio data in real time, in addition, the voice received by the phone microphone is sent back to the indoor machine in real time, and the indoor machine is responsible for forwarding the voice to the door machine.

Previously, videos were played directly by local files, but never by network video streams. Many of Baidu's articles on how to use the framework won't work, there is no detailed procedure !!! Yes !!!

This article describes how to play local video files, play online videos, and use third-party frameworks. If you have any questions, please correct them !!!

 

# Pragma mark-Playback of local video files

 

 

Use AVFoundation. framework

 

Step 1: import the framework AVFoundation. framework

// Tested: You can also play the video without importing the framework. You can just import it when you use it in step 3. Please import it without any unknown bugs !!!

 

Step 2: drag a video file to your project

 

Step 3: code implementation

 

# Import "ViewController. h "# import <AVFoundation/AVFoundation. h> // you need to import the framework # define EYScreenWidth [[UIScreen mainScreen] bounds]. size. width # define EYScreenHeight [[UIScreen mainScreen] bounds]. size. height @ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // 1.obtain the path NSString * path of test.mp4 from mainbundle= [[NSBundle mainBundle] pathForResource: @ "test" ofType: @ "mp4"]; // 2. file url NSURL * url = [NSURL fileURLWithPath: path]; // 3. create a player Based on the url (the player itself cannot display the video) AVPlayer * player = [AVPlayer playerWithURL: url]; // 4. create a view playback layer AVPlayerLayer * layer = [AVPlayerLayer playerLayerWithPlayer: player]; // 5. set the layer size. frame = CGRectMake (0, 0, EYScreenWidth, EYScreenHeight); // 6. added to the view layer of the controller [self. view. layer addSublayer: layer]; // 7. start playing [player play];} @ end

 

 

# Pragma mark-network video stream playback

 

Method 1: MobileVLCKit. framework

 

Step 1: Download MobileVLCKit. framework

1. You can go to the Baidu official website for detailed compilation steps and detailed tutorials on GitHub !!! ---> Go directly to step 6 !!!

2. I have compiled the MobileVLCKit. framework that can be used by both the real machine and the simulator.

Link: https://pan.baidu.com/s/1o8hsgg6 password: zswp if the connection fails, Please mail: lieryangios@126.com or below message !!!

 

Step 2: Decompress the downloaded zip file and drag MobileVLCKit. framework in the MobileVLCKit folder to your project.

 

 

 

Step 4: Select finish

 

 

Step 5: Add a dependency Library

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 in total

After completion:

 

 

 

Step 6: use the framework

 

ViewController. h

 

# Import <UIKit/UIKit. h> @ interface ViewController: UIViewController // specifies the video stream path. The address of the video stream transmitted from outside China @ property (nonatomic, copy) NSString * rtspPath; @ end

 

ViewController. m

 

# Import "ViewController. h "# import <MobileVLCKit/MobileVLCKit. h> // macro with high screen width # define EYScreenWidth [[UIScreen mainScreen] bounds]. size. width # define EYScreenHeight [[UIScreen mainScreen] bounds]. size. height @ interface ViewController () // video playback @ property (nonatomic, strong) VLCMediaPlayer * player; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];
// 1. Create a playback view. The simulator test may be faulty !!! The real machine can play UIView * videoView = [[UIView alloc] initWithFrame: CGRectMake (0, 0, EYScreenWidth, EYScreenHeight)]; [self. view addSubview: videoView]; // 2. create a player self. player = [[VLCMediaPlayer alloc] initWithOptions: nil]; // 3. set the playback layer self. player. drawable = videoView; // 4. set the playback path self. player. media = [VLCMedia mediaWithURL: [NSURL URLWithString: self. rtspPath]; // 5. start playing [self. player play];}-(void) dealloc {if (self. player. isPlaying) {[self. player stop] ;}}@ end

 

Step 7: real machine test

 

Command + R running error

 

 

In the Project Settings, Setting searches for bitcode and changes Yes to No.

 

 

If an error occurs, just comment out the 38th lines of code in the corresponding file!

 

 

 

 

Running again is OK !!!

If not, try to convert ViewController. m -----> ViewController. mm

If the above path is a local path, you can play the local video !!!

 

 

 

Method 2: IJKMediaFramework

 

Step 1: Download IJKMediaFramework

1. You can go to the Baidu official website for detailed compilation steps and detailed tutorials on GitHub !!! --> Proceed to step 3 !!!

2. I have compiled the IJKMediaFramework that can be used by both the real machine and the simulator.

Link: https://pan.baidu.com/s/1dFGM9uL password: gxi6 if the connection fails, Please mail: lieryangios@126.com or below message !!!

 

Step 2: Decompress the downloaded ijk.zip file.

1. IJKMediaFramework. framework

2. libcrypto.

3. librtmp.

4. libssl.

Drag a total of four to your project

 

Step 3: write code

 

ViewController. h

 

# Import <UIKit/UIKit. h> @ interface ViewController: UIViewController // video stream path @ property (nonatomic, copy) NSString * rtspPath; @ end

 

 

ViewController. m

 

# Import "ViewController. h "# import <IJKMediaFramework/IJKMediaFramework. h> // macro definition # define EYScreenBounds [UIScreen mainScreen]. bounds @ interface ViewController () @ property (nonatomic, strong) IJKFFMoviePlayerController * ijkPlayer; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // initialize the playback controller self. ijkPlayer = [[IJKFFMoviePlayerController alloc] initWithContentURLString: self. rts PPath witexceptions: nil]; // set the print level. The test shows no effect [IJKFFMoviePlayerController setLogLevel: k_IJK_LOG_DEBUG]; // set the view Size of the controller self. ijkPlayer. view. frame = EYScreenBounds; // Add the view of the Controller to the view of the controller [self. view addSubview: self. ijkPlayer. view];}-(void) viewWillAppear :( BOOL) animated {[super viewWillAppear: animated]; if (! Self. ijkPlayer. isPlaying) {// play [self. ijkPlayer prepareToPlay] ;}}- (void) viewWillDisappear :( BOOL) animated {[super viewWillDisappear: animated]; if (self. ijkPlayer. isPlaying) {// close [self. ijkPlayer shutdown] ;}}@ end

 

Note: method 1 and method 2 can only use one, because the two of them have conflicts and no solution is found !!! (I personally think there should be a conflict between. a In method 2 and. tbd IN THE SYSTEM)

 

 

More --> blog navigation every Monday !!!

 

 

 

Any questions about iOS development! Please leave a message below !!! Or mail lieryangios@126.com although I may not be able to answer, but I will ask iOS development master !!! Answer your questions !!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.