Simple application of iOS Avplayer

Source: Internet
Author: User

1. Introduction of the system framework

2. Create a URL for your video

3. Create a playback item

4. Initialize the player

5. Set the playback page

Introducing System files #import <AVFoundation/AVFoundation.h> #import <AVKit/AVKit.h> @interface viewcontroller ()/** * Controls for video playback */@property (weak, nonatomic) Iboutlet uislider *progressslider;/** * Declare control properties that play video [can play video or play Audio] */@property (Nonatomic,strong) Avplayer *player;/** * Total duration of playback */@property (nonatomic,assign) cgfloat sumplayoperation; @end @implementation    viewcontroller-(void) viewdidload {[Super viewdidload];    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 item to play Avplayeritem *item = [[Avplayeritem alloc] initwithurl:url];    Initialize the Player object self.player = [[Avplayer alloc] initwithplayeritem:item];    Set 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;    Sets the ratio between the playback window and the current view display content layer.videogravity = Avlayervideogravityresizeaspect;    Add playback view to Self.view [Self.view.layer Addsublayer:layer];    Set the default value for playback progress self.progressSlider.value = 0;    Sets 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.va    Lue/self.player.currentitem.duration.timescale; Cmtimemake (A, B) a represents the current time, and the number of frames per second [Self.player seektotime:cmtimemakewithseconds (self.progressslider.value* Self.sumplayoperation, Self.player.currentItem.duration.timescale) completionhandler:^ (BOOL finished) {[Self.playe    R Play];    }]; }

Simple application of iOS Avplayer

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.