In iOS development, if you need to play a video, such as on the machine ..., We often use mediaplayer to play videos, so it is easy to use. However, the customer requirements are becoming more and more complex, especially in the field or interactive effect. Therefore, if you include videos in some animations, the camera will not be able to run. Therefore, after IOS 4, we can use the avplayer class to perform more minor operations.
Zookeeper:
- Mediaplayer videos are placed in uiview, avplayer is placed in avplayerlayer, and avplayerlayer is a sub-class of calayer.
- Before using mediaplayer, remember to add mediaplayer. Framework and # import <mediaplayer/mediaplayer. h>
- Before using avplayer, remember to join avfoundation. frameworkk and # import <avfoundation/avfoundation. h>
Please refer to the following examples:
Use mediaplayer to play a video
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"backspace" ofType:@"mov"]; NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath]; moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:sourceMovieURL]; moviePlayer.view.frame=CGRectMake(0, 0, 1024, 768); moviePlayer.controlStyle=MPMovieControlStyleNone; // Play the movie! [self.view addSubview:moviePlayer.view];
Use avplayer to play a video
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"backspace" ofType:@"mov"]; NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath]; AVAsset *movieAsset= [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil]; AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset]; AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem]; AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player]; playerLayer.frame = self.view.layer.bounds; playerLayer.videoGravity = AVLayerVideoGravityResizeAspect; [self.view.layer addSublayer:playerLayer]; [player play];