iOS movies play MediaPlayer and Avplayer

Source: Internet
Author: User

On iOS, if you encounter the need to play a movie, such as the animation ..., we are accustomed to use MediaPlayer to play the movie, because it is very convenient to use, so it has been used. But the more demanding the customer is, the more serious it is, especially when it comes to the animation or interaction effect. So if you gimps with a film in some of the animations, the machine will not run. So after iOS 4, we can use Avplayer to do a more subtle operation.

Note:

    • MediaPlayer's film is placed in the UIView, and Avplayer is placed in Avplayerlayer, Avplayerlayer is the sub-category of Calayer.
    • Before using MediaPlayer, remember to join 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 example:

Use MediaPlayer to play a movie

[CPP]View Plaincopy
  1. nsstring *filepath = [[NSBundle mainbundle] pathforresource:@"backspace" oftype:@ "mov"  ];
  2. Nsurl *sourcemovieurl = [Nsurl Fileurlwithpath:filepath];
  3. MoviePlayer = [[MPMoviePlayerController alloc] initwithcontenturl:sourcemovieurl];
  4. Movieplayer.view.frame=cgrectmake (0, 0, 1024, 768);
  5. Movieplayer.controlstyle=mpmoviecontrolstylenone;
  6. //Play the movie!   
  7. [Self.view AddSubview:moviePlayer.view];


Use Avplayer to play a movie

[CPP]View Plaincopy
    1. nsstring *filepath = [[NSBundle mainbundle] pathforresource:@"backspace" oftype:@ "mov"  ];
    2. Nsurl *sourcemovieurl = [Nsurl Fileurlwithpath:filepath];
    3. Avasset *movieasset = [Avurlasset urlassetwithurl:sourcemovieurl options:nil];
    4. Avplayeritem *playeritem = [Avplayeritem playeritemwithasset:movieasset];
    5. Avplayer *player = [Avplayer Playerwithplayeritem:playeritem];
    6. Avplayerlayer *playerlayer = [Avplayerlayer Playerlayerwithplayer:player];
    7. Playerlayer.frame = Self.view.layer.bounds;
    8. playerlayer.videogravity = Avlayervideogravityresizeaspect;
    9. [Self.view.layer Addsublayer:playerlayer];
    10. [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.