Ios video playback code Demo

Source: Internet
Author: User

Ios video playback code Demo

Method 1:-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. // use the built-in MPMoviePlayerController to play the video. First, import MediaPlayer to the project. framework Package. // import # import "MediaPlayer/MPMoviePlayerController. h "UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0,320,568)]; imageView. image = [UIImage imageNamed: @ "00013"]; [self. view addSubview: imageView]; [imageView release]; // play the video button UIButton * playButton = [UIButton buttonWithType: UIButtonTypeSystem]; playButton. frame = CGRectMake (200, 30,100, 30); [playButton addTarget: self action: @ selector (playClick :) forControlEvents: UIControlEventTouchUpInside]; [playButton setTitle: @ "play video" forState: UIControlStateNormal]; playButton. backgroundColor = [UIColor greenColor]; playButton. layer. cornerRadius = 5; playButton. layer. masksToBounds = YES; [self. view addSubview: playButton];}-(void) playClick :( UIButton *) btn {// video file path, which has been saved to the project package. NSString * path = [[NSBundle mainBundle] pathForResource: @ "test" ofType: @ "mp4"]; // video url nsurl * url = [NSURL fileURLWithPath: path]; // The video playback object MPMoviePlayerController * movie = [[MPMoviePlayerController alloc] initWithContentURL: url]; movie. controlStyle = MPMovieControlStyleFullscreen; [movie. view setFrame: self. view. bounds]; movie. initialPlaybackTime =-1; [self. view addSubview: movie. view]; // register a notification indicating the end of playing. When the playing ends, the player listens to and performs some processing. // the player has a playback notification function, you only need to register the observer listening notification here. [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (myMovieFinishedCallback :) name: MPMoviePlayerPlaybackDidFinishNotification object: movie]; [movie play]} -(void) myMovieFinishedCallback :( NSNotification *) y {// The video playback object MPMoviePlayerController * theMovie = [destroy y object]; // destroy the playback notification [[nsicationicationcenter defacenter] removeObserver: self name: MPMoviePlayerPlaybackDidFinishNotification object :( theMovie)]; // release the video object [theMovie. view release];} Method 2:-(void) viewDidLoad {// import MediaPlayer in the project. framework Package. // import # import in the attempt Controller
 
  
[Super viewDidLoad]; // Do any additional setup after loading the view. NSURL * videoURL; NSString * path = [[NSBundle mainBundle] pathForResource: @ "test" ofType: @ "mp4"]; // determine whether it is a network address or a local playback address if ([path hasPrefix: @ "http: //"]) {videoURL = [NSURL URLWithString: path];} else {videoURL = [NSURL fileURLWithPath: path];} MPMoviePlayerViewController * _ moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL: videoURL]; [_ moviePlayerController. view setFrame: CGRectMake (0,100,320,200)]; _ moviePlayerController. moviePlayer. movieSourceType = MPMovieSourceTypeFile; [_ moviePlayerController. moviePlayer setScalingMode: MPMovieScalingModeNone]; [_ moviePlayerController. moviePlayer setRepeatMode: MPMovieRepeatModeNone]; [_ moviePlayerController. moviePlayer setControlStyle: MPMovieControlModeVolumeOnly]; [_ moviePlayerController. moviePlayer setFullscreen: NO animated: YES]; [_ moviePlayerController. moviePlayer play]; // the container of the video playback component. This container is added to ensure compatibility with iOS6. If no container is added under iOS7, there is no problem, if the video playing screen under iOS6 is automatically filled with self. view; UIView * moviePlayView = [[UIView alloc] initWithFrame: CGRectMake (0, 0,320,568)]; [self. view addSubview: moviePlayView]; [moviePlayView addSubview: [_ moviePlayerController. moviePlayer view];}
 


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.