1、AVPlayer
(1) Pros and cons
Advantages: You can customize the UI and control it
Disadvantages: Simple playback, no control UI (progress, pause, play, etc. buttons), and if you want to display the playback interface, you need to use AVPlayerLayer to add layers to the layer you need to display.
(2) Realize remote video playback
Implement playback function (only sound)
1. Import the framework
#import <AVFoundation/>
2. Create an AVPlayer object through a remote URL
NSURL *remoteURL = [NSURL URLWithString:@"/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"]; _player = [AVPlayer playerWithURL:remoteURL];
3. Start playing
[ play];
There is a problem: only sound can be played, no images can be seen
Solution: You need to use the AVPlayerLayer object to create a layer based on the player and add it to the view
Show video
4. Create AVPlayerLayer based on player object
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:];
5. Set the layer's frame
= CGRectMake(0, 0, , * 9 / 16);
6. Add the layer to the layer of the View that needs to be displayed
[ addSublayer:layer];
2、MPMoviePlayerController
(1) Pros and cons
Advantages: The built-in playback control UI (progress bar, previous, next, pause), does not need to be added manually
Disadvantages: Cannot customize the UI
Only this controller view can be added to other views for display
This controller is not a view controller and cannot be popped up directly
The player's playback status is told to the outside world through notification
(2) Video playback implementation steps
1. Import the framework
#import <MediaPlayer/>
2. Create a controller based on the URL MPMoviePlayerController
NSURL *remoteURL = [NSURL URLWithString:@"/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"]; _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:remoteURL];
3. Set the play view frame and add it to the view that needs to be displayed.
// Set the frame of the playback view = CGRectMake(0, 0, , * 9 / 16); // Set the play view control style = MPMovieControlStyleFullscreen; // Add play view to the view to be displayed [ addSubview:];
4. Play
[ play];
3、MPMoviePlayerViewController
(1) Pros and cons
Advantages: The built-in playback control UI does not need to be added manually
This controller is a view controller, which can be popped up and pressed on the stack.
You can also manually resize the view and add it to other views
Disadvantages: Cannot customize the UI
(2) Video playback implementation steps
1. Import the framework
#import <MediaPlayer/>
2. Create a controller based on the URL MPMoviePlayerViewController
NSURL *remoteURL = [NSURL URLWithString:@"/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"]; _playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:remoteURL];
3. The controller is popped up directly modally (or: set the play view frame and add it to the view that needs to be displayed)
[self presentViewController: animated:YES completion:^{ [ play]; }];
4. Play
[ play];
4. For the second and third implementation solutions, after iOS9.0, AVPlayerViewController is used uniformly
(1) Pros and cons
Advantages: The built-in playback control UI does not need to be added manually
This controller is a view controller, which can pop up and press the stack.
You can also manually resize the view and add it to other views
Disadvantages: Cannot customize the UI
(2) Video playback implementation steps
1. Import the framework
#import <AVFoundation/> #import <AVKit/>
2. Create AVPlayer based on URL
NSURL *remoteUrl = [NSURL URLWithString:@"/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"]; AVPlayer *player = [AVPlayer playerWithURL:remoteUrl];
3. Create an AVPlayerViewController controller based on AVPlayer
_playerVc = [[AVPlayerViewController alloc] init]; _playerVc.player = player;
4. Set the play view frame and add it to the view that needs to be displayed.
= CGRectMake(0, 0, , * 9 / 16); [ addSubview:];
5. Play
[ play];
Merge of Step 4 and Step 5:
[self presentViewController: animated:YES completion:nil];
Summarize
The above is all about video playback in iOS. I hope the content of this article will be helpful to everyone to learn or develop IOS.