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
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
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.