ios视频播放代码

//要实现视频播放需要导入类库(MediaPlayer.framework)
下面介绍两种方法实现视频播放:
其中第一种方法需要导入头文件:  #import "MediaPlayer/MPMoviePlayerController.h"
<pre name="code" class="objc">其中第二种方法需要导入头文件:  #import <MediaPlayer/MediaPlayer.h>
新建一个文件
YJFPlayMovieViewController
继承UIViewController
YJFPlayMovieViewController.m// VideosTestYJF// Copyright (c) 2014年 Personl. All rights reserved.//#import "YJFPlayMovieViewController.h"#import "MediaPlayer/MPMoviePlayerController.h" //方法1需要导入的头文件#import <MediaPlayer/MediaPlayer.h> //方法2需要导入的头文件@interface YJFPlayMovieViewController ()@property (nonatomic, retain) MPMoviePlayerViewController *moviePlayViewControllr1;@property (nonatomic, retain) MPMoviePlayerController *moviePlayControllr2;@end@implementation YJFPlayMovieViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self;}- (void)viewDidLoad{ [super viewDidLoad]; //方法一,播放的时候可以在固定的位置 // 播放视频按钮1 UIButton *playButton1 = [UIButton buttonWithType:UIButtonTypeSystem]; playButton1.frame = CGRectMake(65, 30, 180, 30); [playButton1 addTarget:self action:@selector(playMovie1:) forControlEvents:UIControlEventTouchUpInside]; [playButton1 setTitle:@"方法一播放视频" forState:UIControlStateNormal]; playButton1.backgroundColor = [UIColor cyanColor]; playButton1.layer.cornerRadius = 5; playButton1.layer.masksToBounds = YES; [self.view addSubview:playButton1]; // 播放视频按钮2 UIButton *playButton = [UIButton buttonWithType:UIButtonTypeSystem]; playButton.frame = CGRectMake(65, 80, 180, 30); [playButton addTarget:self action:@selector(playMovie2:) forControlEvents:UIControlEventTouchUpInside]; [playButton setTitle:@"方法二播放视频" forState:UIControlStateNormal]; playButton.backgroundColor = [UIColor cyanColor]; playButton.layer.cornerRadius = 5; playButton.layer.masksToBounds = YES; [self.view addSubview:playButton]; }//方法一点击播放按钮触发事件-(void)playMovie1:(UIButton *)button{ [self.moviePlayControllr2.view removeFromSuperview]; NSURL *videoURL = nil; // NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"]; if ([path hasPrefix:@"http://"]) { videoURL = [NSURL URLWithString:path]; } else { videoURL = [NSURL fileURLWithPath:path]; } //创建MPMoviePlayerViewController对象 self.moviePlayViewControllr1 = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL]; //设置视频播放的位置 [self.moviePlayViewControllr1.view setFrame:CGRectMake(0, 150, 320, 300)]; //视频播放类型 self.moviePlayViewControllr1.moviePlayer.movieSourceType = MPMovieSourceTypeFile; [self.moviePlayViewControllr1.moviePlayer setScalingMode:MPMovieScalingModeNone]; [self.moviePlayViewControllr1.moviePlayer setRepeatMode:MPMovieRepeatModeNone]; [self.moviePlayViewControllr1.moviePlayer setControlStyle:MPMovieControlModeVolumeOnly]; [self.moviePlayViewControllr1.moviePlayer setFullscreen:NO animated:YES]; [self.moviePlayViewControllr1.moviePlayer play]; //视频播放组件的容器,加这个容器是为了兼容iOS6,如果不加容器在iOS7下面没有任何问题,如果在iOS6下面视频的播放画面会自动铺满self.view; UIView *moviePlayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)]; [self.view addSubview:moviePlayView]; [moviePlayView addSubview:[_moviePlayViewControllr1.moviePlayer view]]; [_moviePlayViewControllr1 release];}//方法二点击播放按钮触发事件-(void)playMovie2:(UIButton *)button{ [self.moviePlayViewControllr1.view removeFromSuperview]; //视频文件路径,此视频已经存入项目包中.属于本地播放 NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"]; //视频URL NSURL *url = [NSURL fileURLWithPath:path]; //视频播放对象 self.moviePlayControllr2 = [[MPMoviePlayerController alloc] initWithContentURL:url]; self.moviePlayControllr2.controlStyle = MPMovieControlStyleFullscreen; [self.moviePlayControllr2.view setFrame:self.view.bounds]; self.moviePlayControllr2.initialPlaybackTime = -1; [self.view addSubview:self.moviePlayControllr2.view]; //注册一个播放结束的通知, 当播放结束时, 监听到并且做一些处理 //播放器自带有播放通知的功能, 在此仅仅只需要注册观察者监听通知的即可 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayControllr2]; [self.moviePlayControllr2 play]; [_moviePlayControllr2 release];}- (void)movieFinishedCallback:(NSNotification *)notify{ //视频播放对象 MPMoviePlayerController *theMovie = [notify object]; //销毁播放通知 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:(theMovie)]; //释放视频对象 [theMovie.view release];}- (void)dealloc{ self.moviePlayViewControllr1 = nil; self.moviePlayControllr2 = nil; [super dealloc];}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值