在 IOS 中播放 ViewController 中选择的视频
Posted
技术标签:
【中文标题】在 IOS 中播放 ViewController 中选择的视频【英文标题】:Playing video chosen in ViewController in IOS 【发布时间】:2014-05-25 03:58:23 【问题描述】:我有一个主从应用程序。在那里,我已经能够下载一个 XML 文件并将其用作一个数组。
但我想选择一个标签并让它播放视频。
除了以下代码块之外,所有这些都有效。
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
当我尝试分配 MPMoviePlayer 时,它会使程序崩溃。任何见解将不胜感激。
代码:
MasterViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface MasterViewController : UITableViewController
@property (strong, nonatomic) NSDictionary *presidents;
@property (strong, nonatomic) NSArray *number;
@property (strong, nonatomic) NSArray *name;
@property (strong, nonatomic) NSArray *years;
@property (strong, nonatomic) NSArray *party;
@property (strong, nonatomic) NSArray *image;
@end
MasterViewController.m
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface MasterViewController ()
NSMutableArray *_objects;
@end
@implementation MasterViewController
@synthesize number, name, years, party, image;
- (void)awakeFromNib
[super awakeFromNib];
- (void)viewDidLoad
self.navigationItem.title=@"Presidents";
NSString *urlStr = [[NSString alloc]
initWithFormat:@"http://pickupboyd.com/wordpress/wp-content/uploads/2012/04/Presidents12.plist"];
NSURL *url = [NSURL URLWithString:urlStr];
_presidents = [[NSDictionary alloc] initWithContentsOfURL:url];
name = [_presidents objectForKey:@"Names"];
years = [_presidents objectForKey:@"Years"];
party = [_presidents objectForKey:@"Party"];
image = [_presidents objectForKey:@"Images"];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (void)insertNewObject:(id)sender
if (!_objects)
_objects = [[NSMutableArray alloc] init];
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//This is to the get count of the array in dictionary "Presidents" called Number
number = [_presidents objectForKey:@"Number"];
return number.count;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *nameOfPresident = [name objectAtIndex:indexPath.row];
cell.textLabel.text = nameOfPresident;
NSString *datesInOffice = [years objectAtIndex:indexPath.row];
cell.detailTextLabel.text = datesInOffice;
//set the image view-------------------------------------
NSString *presidentImages = [image objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:presidentImages];
return cell;
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
// Return NO if you do not want the specified item to be editable.
return YES;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
if (editingStyle == UITableViewCellEditingStyleDelete)
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
else if (editingStyle == UITableViewCellEditingStyleInsert)
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
if ([[segue identifier] isEqualToString:@"showDetail"])
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSURL *movieURL = [party objectAtIndex:indexPath.row];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[[player view] setFrame:[self.view bounds]];
[self.view addSubview:[player view]];
[player play];
MPMoviePlayerViewController *playVideo = [[MPMoviePlayerViewController alloc]initWithContentURL:movieURL];
//NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] presentMoviePlayerViewControllerAnimated:playVideo];
@end
DetailViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface DetailViewController : UIViewController
@property (strong, nonatomic) id detailItem;
@end
DetailViewController.m
#import "DetailViewController.h"
@interface DetailViewController ()
- (void)configureView;
@end
@implementation DetailViewController
#pragma mark - Managing the detail item
- (void)configureView
// Update the user interface for the detail item.
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
@end
【问题讨论】:
您能否提供有关崩溃的任何信息? 请尝试仅添加代码的相关部分,而不是仅粘贴整个文件。还要始终包含您收到的错误。 抱歉,感谢您的意见。我对延误表示歉意。错误是:在主线程 1:信号 SIGABRT。谢谢! 【参考方案1】:尝试更改此行:
[[player view] setFrame:[self.view bounds]];
到这里:
[[player view] setFrame:[self.view frame]];
请参阅this 了解边界和框架之间的差异。
【讨论】:
谢谢你。我确实尝试过,但仍然收到主要错误。我将继续为此努力。这是我第一次发帖,非常感谢您的意见。【参考方案2】:这是我在点击按钮后在视图中播放视频的代码
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController : UIViewController
NSURL * videoURL;
UITableView * view;
@property (nonatomic, strong) MPMoviePlayerController*player;
-(IBAction)playMovie;
@end
.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(IBAction)playMovie
NSString *url = [[NSBundle mainBundle]
pathForResource:@"testy3 (Converted)" ofType:@"mov"];
self.player = [[MPMoviePlayerController alloc]
initWithContentURL: [NSURL fileURLWithPath:url]];
// Play Partial Screen
self.player.view.frame = CGRectMake(0, 0, 320, 400);
[self.view addSubview:self.player.view];
// Play Movie
[self.player play];
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
@end
【讨论】:
我今天会试试这个,让你知道。看来我可能没有分配 MPMoviePlayer?再次感谢! 那么,为了清楚起见,分配一个 UIView 会比尝试使用 segue 方法在 Detail ViewController 中执行此操作更好吗?看起来好像您基本上在需要时分配 UIView 并且不真正使用情节提要 segue?谢谢,只是想让我明白所有这些。以上是关于在 IOS 中播放 ViewController 中选择的视频的主要内容,如果未能解决你的问题,请参考以下文章
iOS: ios视频播放(MPMediaPlayerController,AVPlayer,AVPlayerViewcontrollerffmpeg-AVPlayer)