iOS 视频功能

来源:互联网 发布:淘宝开店怎么卖东西 编辑:程序博客网 时间:2024/06/09 22:37

主控制器

 #import "YYViewController.h" 10 #import "MBProgressHUD+MJ.h" 11 #import "YYviodesModel.h" 12 #import "UIImageView+WebCache.h" 13 #import "YYCell.h" 14 #import <MediaPlayer/MediaPlayer.h> 15  16 @interface YYViewController () 17 @property(nonatomic,strong)NSArray *videos; 18  19 @end 20  21 @implementation YYViewController 22  23 - (void)viewDidLoad 24 { 25     [super viewDidLoad]; 26     //去掉下划线 27     self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; 28      29     [MBProgressHUD showMessage:@"正在努力加载中"]; 30      31     //创建路径 32  33     NSString  *urlStr=@"http://192.168.1.53:8080/MJServer/video"; 34     NSURL *url=[NSURL URLWithString:urlStr]; 35      36     //创建请求 37     NSMutableURLRequest  *request=[NSMutableURLRequest requestWithURL:url];//默认为get请求 38     //设置最大的网络等待时间 39     request.timeoutInterval=20.0; 40      41     //获取主队列 42     NSOperationQueue *queue=[NSOperationQueue mainQueue]; 43     //发起请求 44    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 45        //隐藏HUD 46         [MBProgressHUD hideHUD]; 47        if (data) {//如果请求成功,拿到服务器返回的数据 48            //解析拿到的数据(JSON方式) 49            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 50 //           NSArray *array=dict[@"video"]; 51            NSArray *array=dict[@"videos"]; 52             53            NSMutableArray *videos=[NSMutableArray array]; 54            for (NSDictionary *dict in array) { 55                YYviodesModel *model=[YYviodesModel viodesModelWithDict:dict]; 56                [videos addObject:model]; 57            } 58            self.videos=videos; 59             60            //刷新表格 61            [self.tableView reloadData]; 62             63        }else//如果请求失败,没有拿到数据 64        { 65            [MBProgressHUD showError:@"网络繁忙,等稍后再试!"]; 66        } 67         68    }]; 69 } 70  71 #pragma mark-数据源方法 72 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 73 { 74     return 1; 75 } 76 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 77 { 78     return self.videos.count; 79 } 80 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 81 { 82     static NSString *ID=@"ID"; 83     YYCell *cell=[tableView dequeueReusableCellWithIdentifier:ID]; 84     if (cell==nil) { 85         cell=[[YYCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; 86     } 87      88     //获取数据模型 89     YYviodesModel *model=self.videos[indexPath.row]; 90     cell.textLabel.text=model.name; 91     NSString *length=[NSString stringWithFormat:@"时长%d分钟",model.length]; 92     cell.detailTextLabel.text=length; 93      94     // video.image == resources/images/minion_01.png 95     NSString *imageUrl = [NSString stringWithFormat:@"http://192.168.1.53:8080/MJServer/%@", model.image]; 96  97     //这里使用了第三方框架 98     [cell.imageView  setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]]; 99     100     return cell;101 }102 103 //设置cell的行高104 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath105 {106     return 70;107 }108 109 //播放视频110 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath111 {112     //取出数据模型113     YYviodesModel *model=self.videos[indexPath.row];114     115     //创建视屏播放器116  //   MPMoviePlayerController 可以随意控制播放器的尺寸117     //MPMoviePlayerViewController只能全屏播放118     119         NSString *url = [NSString stringWithFormat:@"http://192.168.1.53:8080/MJServer/%@", model.url];120     NSURL *videoUrl=[NSURL URLWithString:url];121     MPMoviePlayerViewController *movieVc=[[MPMoviePlayerViewController alloc]initWithContentURL:videoUrl];122     //弹出播放器123     [self presentMoviePlayerViewControllerAnimated:movieVc];124     125 }126 @end
要导入的框架
在把头文件导入#import<MediaPlayer/MediaPlayer>
实现屏幕方向的控制
1 #pragma mark-实现屏幕方向的控制 2 /** 3  *  控制当前控制器支持那些方向 4  * 5  *  返回值是 UIInterfaceOrientationMask* 6  */ 7 -(NSUInteger)supportedInterfaceOrientations 8 {/** 9   *UIInterfaceOrientationMaskPortrait:竖屏(正常)10   *UIInterfaceOrientationMaskPortraitUpsideDown:竖屏(上下颠倒)11   *UIInterfaceOrientationMaskLandscapeLeft:横屏向左12   *UIInterfaceOrientationMaskLandscapeRight:横屏向右13   *UIInterfaceOrientationMaskLandscape:横屏(左右都支持)14   *UIInterfaceOrientationMaskAll:所有都支持15   */16     return UIInterfaceOrientationMaskAll;17 }
 //视屏列表界面只支持竖屏方向18 #pragma mark-实现屏幕方向的控制19 /**20  *  控制当前控制器支持那些方向21  *22  *  返回值是 UIInterfaceOrientationMask*23  */24 -(NSUInteger)supportedInterfaceOrientations25 {/**26   *UIInterfaceOrientationMaskPortrait:竖屏(正常)27   *UIInterfaceOrientationMaskPortraitUpsideDown:竖屏(上下颠倒)28   *UIInterfaceOrientationMaskLandscapeLeft:横屏向左29   *UIInterfaceOrientationMaskLandscapeRight:横屏向右30   *UIInterfaceOrientationMaskLandscape:横屏(左右都支持)31   *UIInterfaceOrientationMaskAll:所有都支持32   */33     return UIInterfaceOrientationMaskPortrait;34 }
18 //播放界面只支持横屏19 #pragma mark-实现屏幕方向的控制20 21 -(NSUInteger)supportedInterfaceOrientations22 {23     return UIInterfaceOrientationMaskLandscape;24 }25 26 @end


0 0