iOS movies play MediaPlayer and Avplayer

Source: Internet
Author: User

comefrom:http://blog.csdn.net/think12/article/details/8549438

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

nsstring *filepath = [[NSBundle mainbundle] pathforresource:@"backspace" oftype:@"mov"];

    1. Nsurl *sourcemovieurl = [Nsurl Fileurlwithpath:filepath];
    2. MoviePlayer = [[MPMoviePlayerController alloc] initwithcontenturl:sourcemovieurl];
    3. Movieplayer.view.frame=cgrectmake (0, 0, 1024, 768);
    4. Movieplayer.controlstyle=mpmoviecontrolstylenone;
    5. Play the movie!
    6. [Self.view AddSubview:moviePlayer.view];


Use Avplayer to play a movie

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

iOS movies play MediaPlayer and Avplayer

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.