IOS video playback mediaplayer and avplayer

Source: Internet
Author: User

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