ios 视频播放/直播demo

方法一: 
- ( void )viewDidLoad 
     [super viewDidLoad]; 
     // Do any additional setup after loading the view.   
     //利用自带MPMoviePlayerController来实现视频播放,首先要在 项目中导入MediaPlayer.Framework框架包. 
     //在试图控制器中导入#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]; 
       
     // 播放视频按钮 
     UIButton *playButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
     playButton.frame = CGRectMake(200, 30, 100, 30); 
     [playButton addTarget:self action:@selector(playClick:) forControlEvents:UIControlEventTouchUpInside]; 
     [playButton setTitle:@ "播放视频" forState:UIControlStateNormal]; 
       
     playButton.backgroundColor = [UIColor greenColor]; 
     playButton.layer.cornerRadius = 5; 
     playButton.layer.masksToBounds = YES; 
     [self.view addSubview:playButton]; 
   
- ( void )playClick:(UIButton *)btn 
     //视频文件路径,此视频已经存入项目包中.属于本地播放 
     NSString *path = [[NSBundle mainBundle] pathForResource:@ "test" ofType:@ "mp4" ]; 
     //视频URL 
     NSURL *url = [NSURL fileURLWithPath:path]; 
     //视频播放对象 
     MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
     movie.controlStyle = MPMovieControlStyleFullscreen; 
     [movie.view setFrame:self.view.bounds]; 
     movie.initialPlaybackTime = -1; 
     [self.view addSubview:movie.view]; 
     //注册一个播放结束的通知, 当播放结束时, 监听到并且做一些处理 
     //播放器自带有播放通知的功能, 在此仅仅只需要注册观察者监听通知的即可 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification 
       object:movie]; 
     [movie play]; 
   
- ( void )myMovieFinishedCallback:(NSNotification *)notify 
     //视频播放对象 
     MPMoviePlayerController *theMovie = [notify object]; 
     //销毁播放通知 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:(theMovie)]; 
     //释放视频对象 
     [theMovie.view release]; 
   
方法二: 
- ( void )viewDidLoad 
     //首先要在 项目中导入MediaPlayer.Framework框架包. 
     //在试图控制器中导入#import <MediaPlayer/MediaPlayer.h> 
     [super viewDidLoad]; 
     // Do any additional setup after loading the view. 
     NSURL *videoURL; 
     NSString *path = [[NSBundle mainBundle] pathForResource:@ "test" ofType:@ "mp4" ]; 
     //判断是网络地址还是本地播放地址 
     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]; 
     //视频播放组件的容器,加这个容器是为了兼容iOS6,如果不加容器在iOS7下面没有任何问题,如果在iOS6下面视频的播放画面会自动铺满self.view; 
     UIView *moviePlayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)]; 
     [self.view addSubview:moviePlayView]; 
     [moviePlayView addSubview:[_moviePlayerController.moviePlayer view]]; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值