Simple Application of AVPlayer in iOS Development
- 2021-11-01 05:00:52
- OfStack
Preface
In iOS development, there are usually two ways to play video, one is to use MPMoviePlayerController (MediaPlayer. Framework needs to be imported), and the other is to use AVPlayer. The difference between these two classes is that MPMoviePlayerController is simpler to use and less powerful than AVPlayer, while AVPlayer is a little more troublesome but more powerful. The following article mainly introduces the simple application of AVPlayer, and friends who need it will come and have a look.
Simple Application of AVPlayer
1. Introducing the system framework
2. Create an url for video
3. Create a playback item
4. Initialize the player
5. Set up the playback page
The example code is as follows:
// Introducing system files
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
@interface ViewController ()
/**
* Controls that control video playback
*/
@property (weak, nonatomic) IBOutlet UISlider *progressSlider;
/**
* Declare control properties for playing video [ You can play both video and audio ]
*/
@property (nonatomic,strong)AVPlayer *player;
/**
* Total duration of playback
*/
@property (nonatomic,assign)CGFloat sumPlayOperation;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Set the playback url
NSString *playString = @"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4";
NSURL *url = [NSURL URLWithString:playString];
// Set the items to play
AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
// Initialization player Object
self.player = [[AVPlayer alloc] initWithPlayerItem:item];
// Set up the playback page
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
// Set the size of the playback page
layer.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300);
layer.backgroundColor = [UIColor cyanColor].CGColor;
// Set the scale between the playback window and the current view to display the content
layer.videoGravity = AVLayerVideoGravityResizeAspect;
// Add a playback view to the self.view
[self.view.layer addSublayer:layer];
// Set the default value of playback progress
self.progressSlider.value = 0;
// Set the default volume value for playback
self.player.volume = 1.0f;
}
#pragma mark - Start button response method
- (IBAction)startPlayer:(id)sender {
[self.player play];
}
#pragma mark - Pause button response method
- (IBAction)stopPlayer:(id)sender {
[self.player pause];
}
#pragma mark - Change progress
- (IBAction)changeProgress:(id)sender {
self.sumPlayOperation = self.player.currentItem.duration.value/self.player.currentItem.duration.timescale;
//CMTimeMake(a,b) a Represents the current time, b Indicates how many frames per second
[self.player seekToTime:CMTimeMakeWithSeconds(self.progressSlider.value*self.sumPlayOperation, self.player.currentItem.duration.timescale) completionHandler:^(BOOL finished) {
[self.player play];
}];
}
Summarize