iOS中 MediaPlayer framework实现视频播放
ios 实现 视频 播放 Framework MediaPlayer
2023-09-14 08:59:40 时间
iOS开发中播放音乐可以使用MPMusicPlayerController类来实现,播放视频可以使用MPMoviePlayerController和MPMoviePlayerViewController类来实现,同时MPMediaPickerController 类可以用于从系统媒体库中选择媒体播放。这几个类都包含与MediaPlayer.framework框架中。
这里主要介绍MediaPlayer.framework
指定根视图:
RootTableViewController *rootTVC = [[RootTableViewController alloc] initWithStyle:UITableViewStylePlain]; UINavigationController *rootNC = [[UINavigationController alloc] initWithRootViewController:rootTVC]; self.window.rootViewController = rootNC;
[self.tableView registerNib:[UINib nibWithNibName:@"TestCell" bundle:nil] forCellReuseIdentifier:@"testCell"]; self.dataSourceArray = [NSMutableArray array]; [self loadDataAndShow];
NSURL *url = [NSURL URLWithString:@"http://c.m.163.com/nc/video/list/V9LG4B3A0/y/1-20.html"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (data != nil) { NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSArray *array = dict[@"V9LG4B3A0"]; for (NSDictionary *aDict in array) { TestModel *model = [[TestModel alloc] init]; [model setValuesForKeysWithDictionary:aDict]; [self.dataSourceArray addObject:model]; } [self.tableView reloadData]; } else { NSLog(@"%@", [connectionError localizedDescription]); } }];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section return self.dataSourceArray.count;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testCell" forIndexPath:indexPath]; TestModel *model = self.dataSourceArray[indexPath.row]; [cell.movieImageView sd_setImageWithURL:[NSURL URLWithString:model.cover]]; UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]; [cell.movieImageView addGestureRecognizer:tapGR]; return cell;
UIView *view = sender.view; UITableViewCell *cell = (UITableViewCell *)view.superview.superview; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; self.selectedIndexPath = indexPath; TestModel *model = self.dataSourceArray[indexPath.row]; self.mpPVC = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:model.mp4_url]]; self.mpPVC.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 370); [self.mpPVC.moviePlayer setScalingMode:MPMovieScalingModeAspectFill]; [self.mpPVC.moviePlayer setControlStyle:MPMovieControlStyleEmbedded]; [cell addSubview:self.mpPVC.view];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath return 370;
TestCell *cell = (TestCell *)[self.tableView cellForRowAtIndexPath:self.selectedIndexPath]; // 当前cell在tableView的坐标 CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:self.selectedIndexPath]; CGRect rectInWindow = [self.tableView convertRect:rectInTableView toView:[self.tableView superview]]; self.selectedRect = CGRectMake(rectInTableView.origin.x, rectInTableView.origin.y, cell.movieImageView.bounds.size.width + 20, cell.movieImageView.bounds.size.height + 20); if ([self.mpPVC.moviePlayer isPreparedToPlay]) { if (rectInWindow.origin.y = -370 || rectInWindow.origin.y = [UIScreen mainScreen].bounds.size.height) { [UIView animateWithDuration:.5 animations:^{ self.mpPVC.view.frame = CGRectMake(self.view.bounds.size.width - 170, self.view.bounds.size.height - 170, 170, 170); [self.view.window addSubview:self.mpPVC.view]; self.mpPVC.moviePlayer.controlStyle = MPMovieControlStyleEmbedded; }]; } else { self.mpPVC.view.frame = self.selectedRect; [self.tableView addSubview:self.mpPVC.view]; self.mpPVC.moviePlayer.controlStyle = MPMovieControlStyleDefault; } }
@property (nonatomic, copy) NSString *cover; @property (nonatomic, copy) NSString *mp4_url; @end //.m - (void)setValue:(id)value forUndefinedKey:(NSString *)key
相关文章
- (NO.00005)iOS实现炸弹人游戏(七):游戏数据的序列化表示
- (NO.00005)iOS实现炸弹人游戏(六):游戏数据的初始化(三)
- iOS中类单例方法的一种实现
- (NO.00001)iOS游戏SpeedBoy Lite成形记(十三)
- iOS开发之使用XMPPFramework实现即时通信(三)
- iOS开发之网络编程--获取文件的MIMEType
- [iOS]iOS AudioSession详解 Category选择 听筒扬声器切换
- iOS禁用webivew的alert
- iOS开发UI篇—ios应用数据存储方式(偏好设置)
- 【IOS-COCOS2D游戏开发之十五】详解CCPROGRESSTIMER 进度条并修改COCOS2D源码实现“理想”游戏进度条!
- iOS远程推送原理及实现过程
- 推送通知iOS客户端编写实现及推送服务器端编写
- iOS开发那些事-故事板实现标签导航
- IOS控件:WebView移动网站导航
- iOS 开发 关闭黑暗模式
- iOS Cocoapods git 创建自己的cocoapod库再学习
- ios swift 多线程 GCD简单使用
- ios(swift) UIButton setTitle不生效
- ios(swift)显示和隐藏(收起)tabBar
- iOS swift UITableView的cell被点击或是用代码选中其中的UIImageView会显示Highlighted效果
- ios开发之-继承的实现运用
- iOS 实现QQ界面
- iOS开发-使用storyboard实现UILabel的自适应高度(iOS8)
- iOS开发必看的博客汇总