#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "XMGVideo.h"
#import "MJExtension.h"
#import "GDataXMLNode.h"
// 视频播放
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<NSXMLParserDelegate>{
AVPlayerViewController *_playerController;
AVPlayer *_player;
AVAudioSession *_session;
NSString *_urlString;
}
/*TableView的数据源*/
@property (nonatomic, strong) NSMutableArray *videos;
@end
@implementation ViewController
-(NSMutableArray *)videos
{
if (_videos == nil) {
_videos = [NSMutableArray array];
}
return _videos;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[XMGVideo setupReplacedKeyFromPropertyName:^NSDictionary *{
return @{
@"ID":@"id"
};
}];
//1.确定请求路径
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/video?type=XML"];
//2.创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.发送异步请求
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//4.解析数据
//4.1 加载整个XML文档
GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:data options:kNilOptions error:nil];
//4.2 拿到根元素
NSArray *eles = [doc.rootElement elementsForName:@"video"];
for (GDataXMLElement *ele in eles) {
NSLog(@"%@",ele);
XMGVideo *video = [[XMGVideo alloc]init];
video.name = [ele attributeForName:@"name"].stringValue;
video.length = [ele attributeForName:@"length"].stringValue.integerValue;
video.image = [ele attributeForName:@"image"].stringValue;
video.url = [ele attributeForName:@"url"].stringValue;
[self.videos addObject:video];
}
//5.刷新TableView
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}];
[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];
//拿到该行cell对应的数据
// NSDictionary *videoDict = self.videos[indexPath.row];
XMGVideo *video = self.videos[indexPath.row];
//2.设置cell的数据
/*
cell.textLabel.text = videoDict[@"name"];
NSString *detailStr = [NSString stringWithFormat:@"%@",videoDict[@"length"]];
cell.detailTextLabel.text = detailStr;
NSString *baseUrl = @"http://120.25.226.186:32812/";
NSURL *imageUrl =[NSURL URLWithString:[baseUrl stringByAppendingPathComponent:videoDict[@"image"]]];
[cell.imageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"123"]];
*/
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```
网友评论