A brief analysis of several schemes of video playback in IOS _ios

Source: Internet
Author: User

1, Avplayer

(1) Advantages and disadvantages

Advantages: You can customize the UI for control

Disadvantages: Simple playback, no control UI (progress, pause, play, etc.), and if you want to display the playback interface, you need to use Avplayerlayer to add layers to the layer you want to show

(2) realize remote video playback

Implement playback function (only sound)

1. Import Frame

#import <AVFoundation/AVFoundation.h>

2. Create a Avplayer object from a remote URL

Nsurl *remoteurl = [Nsurl urlwithstring:@ "Http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
 _player = [Avplayer Playerwithurl:remoteurl];

3. Start Playback

[Self.player play];

There is a problem: only sound can be played, the image cannot be seen

Solution: You need to use the Avplayerlayer object to create a layer based on the player to add to the view

Show Video

4. Create avplayerlayer based on player object

 Avplayerlayer *layer = [Avplayerlayer PlayerLayerWithPlayer:self.player];

5. Set the frame of the layer

  Layer.frame = CGRectMake (0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 9/16);

6. Add the layer to the layer of the View that needs to be displayed

  [Self.view.layer Addsublayer:layer];

2, MPMoviePlayerController

(1) Advantages and disadvantages

Advantages: Self-contained playback control UI (progress bar, Prev, Next, paused), no need to manually add

Disadvantage: Cannot customize UI

This controller view can only be added to other views for presentation

This controller is not a view controller and cannot be ejected directly

The playback state of the player is to inform the outside world by way of notification

(2) Video playback implementation steps

1. Import Frame

#import <MediaPlayer/MediaPlayer.h>

2. Create controller MPMoviePlayerController according to URL

Nsurl *remoteurl = [Nsurl urlwithstring:@ "Http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
 _movieplayer = [[MPMoviePlayerController alloc] initwithcontenturl:remoteurl];

3. Set the play view frame to add to the view you want to show

 Set frame
 Self.moviePlayer.view.frame = CGRectMake for playback view (0, 0, Self.view.bounds.size.width, Self.view.bounds.size.height * 9/16);
 Set playback view control style
 Self.moviePlayer.controlStyle = Mpmoviecontrolstylefullscreen;
 Add a playback view to the view you want to display
 [Self.view AddSubview:self.moviePlayer.view];

4. Play

[Self.movieplayer play];

3, Mpmovieplayerviewcontroller

(1) Advantages and disadvantages

Advantages: Self-contained playback control UI, no need to manually add

This controller is a view controller that can eject, can press stacks

You can also manually resize the view and add it to another view

Disadvantage: Cannot customize UI

(2) Video playback implementation steps

1. Import Frame

#import <MediaPlayer/MediaPlayer.h>

2. Create controller Mpmovieplayerviewcontroller according to URL

Nsurl *remoteurl = [Nsurl urlwithstring:@ "Http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
 _PLAYERVC = [[Mpmovieplayerviewcontroller alloc] initwithcontenturl:remoteurl];

3. Direct mode pops up the controller (or: Sets the play view frame and adds it to the view that needs to be displayed)

[Self PresentViewController:self.playerVC animated:yes completion:^{
   [Self.playerVC.moviePlayer play];
 }];

4. Play

[Self.playerVC.moviePlayer play];

4, for the 2nd and 3rd implementation of the scheme, after iOS9.0, unified use of Avplayerviewcontroller

(1) Advantages and disadvantages

Advantages: Self-contained playback control UI, no need to manually add

This controller is a view controller that can eject, can press stacks

You can also manually resize the view and add it to another view

Disadvantage: Cannot customize UI

(2) Video playback implementation steps

1. Import Frame

 #import <AVFoundation/AVFoundation.h>
 #import <AVKit/AVKit.h>

2. Create avplayer based on URL

  Nsurl *remoteurl = [Nsurl urlwithstring:@ "Http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"]; 
  Avplayer *player = [Avplayer Playerwithurl:remoteurl];

3. Create Avplayerviewcontroller controller according to Avplayer

  _PLAYERVC = [[Avplayerviewcontroller alloc] init];
  _playervc.player = player;

4. Set the play view frame to add to the view you want to show

  Self.playerVc.view.frame = CGRectMake (0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 9/16);
  [Self.view AddSubview:self.playerVc.view];

5. Play

  [Self.playerVc.player play];

Merge of steps 4 and 5:

[Self presentViewController:self.playerVc animated:yes completion:nil];

Summarize

That's all about the video playback in iOS, and hopefully the content of this article will help you learn or develop iOS.

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.