美文网首页
AVPlayer+URLSession+JSON解析

AVPlayer+URLSession+JSON解析

作者: 952625a28d0d | 来源:发表于2016-06-27 21:31 被阅读205次
    • 项目中用到了SDWebImage和MJExtension两个常用库
    [cell.imageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"123"]];```
    
    

    self.videos = [XMGVideo objectArrayWithKeyValuesArray:arrayM];```

    #import "ViewController.h"
    #import "UIImageView+WebCache.h"
    #import "XMGVideo.h"
    #import "MJExtension.h"
    
    // 视频播放
    #import <AVKit/AVKit.h>
    #import <AVFoundation/AVFoundation.h>
    
    @interface ViewController (){
        AVPlayerViewController      *_playerController;
        AVPlayer                    *_player;
        AVAudioSession              *_session;
        NSString                    *_urlString;
    }
    
    /*TableView的数据源*/
    @property (nonatomic, strong) NSArray *videos;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [XMGVideo setupReplacedKeyFromPropertyName:^NSDictionary *{
           
            return @{
                     @"ID":@"id"
                     };
        }];
        
        //1.确定请求路径
        NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/video?type=JSON"];
        
        // 2:开始网络请求
        NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:[NSURLRequest requestWithURL:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            // 3.解析数据
            NSDictionary *dictM = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            
            // 存入本地或者数据库
            [dictM writeToFile:@"/Users/xiaomage/Desktop/video.plist" atomically:YES];
            
            // 4:取出视频数组
            NSArray *arrayM = dictM[@"videos"];
            
            //字典转模型
            //        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:arrayM.count];
            //
            //        for (NSDictionary *dict in arrayM) {
            //            [arr addObject:[XMGVideo videoWithDict:dict]];
            //        }
            
            //字典数组转模型数组
            self.videos = [XMGVideo objectArrayWithKeyValuesArray:arrayM];
            
            NSLog(@"----%@",self.videos);
            
            //5.主线程刷新TableView
           dispatch_async(dispatch_get_main_queue(), ^{
               [self.tableView reloadData];
           });
        }];
        
        // 6.任务开始
        [task resume];
        
    }
    
    #pragma mark  TableViewDataSource
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.videos.count;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *ID = @"video";
        
        //1.创建cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        XMGVideo *video = self.videos[indexPath.row];
        
        cell.textLabel.text = video.name;
        NSString *detailStr = [NSString stringWithFormat:@"%zd",video.length];
        
        cell.detailTextLabel.text = detailStr;
        NSString *baseUrl = @"http://120.25.226.186:32812/";
        NSURL *imageUrl =[NSURL URLWithString:[baseUrl stringByAppendingPathComponent:video.image]];
        
        [cell.imageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"123"]];
        
        NSLog(@"ID---%@",video.ID);
        //3.返回cell
        return cell;
    }
    
    #pragma mark---TableViewDelegate
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //1.拿到数据
    //     NSDictionary *videoDict = self.videos[indexPath.row];
        
            XMGVideo *video = self.videos[indexPath.row];
        NSString *baseUrl = @"http://120.25.226.186:32812/";
        NSURL *mpUrl = [NSURL URLWithString:[baseUrl stringByAppendingPathComponent:video.url]];
        
        // 初始化播放器并播放
        _session = [AVAudioSession sharedInstance];
        [_session setCategory:AVAudioSessionCategoryPlayback error:nil];
        
        _player = [[AVPlayer alloc] initWithURL:mpUrl];
        // 初始化播放器控制器
        _playerController = [[AVPlayerViewController alloc] init];
        // 赋值播放器
        _playerController.player = _player;
        // 翻转感应 自动适配屏幕大小
        _playerController.videoGravity = AVLayerVideoGravityResizeAspect;
        _playerController.allowsPictureInPicturePlayback = true;    //画中画,iPad可用
        _playerController.showsPlaybackControls = true;
        _playerController.view.translatesAutoresizingMaskIntoConstraints = true;
        // 自动播放
        [_playerController.player play];
        
        [self presentViewController:_playerController animated:YES completion:nil];
    }
    
    @end```
    
    ![AVPlayer+JSON+NSURLSession.gif](https://img.haomeiwen.com/i189984/6cb14be68f8447d8.gif?imageMogr2/auto-orient/strip)

    相关文章

      网友评论

          本文标题:AVPlayer+URLSession+JSON解析

          本文链接:https://www.haomeiwen.com/subject/duazdttx.html