简体   繁体   中英

Playing video chosen in ViewController in IOS

I have a Master-Detail Application. In there, I've been able to download a XML file and use it as an array.

But I want to choose a tab and have it to play a video.

All of this works except in the following code block.

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

When I try to alloc a MPMoviePlayer, it crashes the program. Any insight would be greatly appreciated.

Code :

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

Try changing this line:

[[player view] setFrame:[self.view bounds]];

To this:

[[player view] setFrame:[self.view frame]];

And see this for the differences between bounds and frame.

Here's the code i use for playing video in a view after a button tap

#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM