Portrait video to landscape

你说的曾经没有我的故事 提交于 2019-12-08 13:44:42

问题


I am aware questions like this one may already be out there but for the sake of others like me I will go ahead and ask

I have a app that is set to only allow portrait orientation but this setting affects my videos as I would like only the videos to be able to play in landscape also. Is there a method I can add unto my .m file to make this work? Here is my code;

 #import "BIDVideosViewController.h"

 @interface BIDVideosViewController ()


 @end

 @implementation BIDVideosViewController

 @synthesize moviePlayer ;


 @synthesize tableList;


 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 if (self) {
    // Custom initialization
 }
 return self;
 }


 - (void)viewDidLoad
 {
 [super viewDidLoad];
 UITableView *table = [[UITableView alloc]initWithFrame:self.view.bounds];
 [table setDelegate:self];
 [table setDataSource:self];
 [self.view addSubview:table];
 tableList = [[NSMutableArray alloc] initWithObjects:@"Gangan",@"SwimGood",@"German Ice", nil];

 }

 - (void)didReceiveMemoryWarning
 {
 [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
 }

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
 return [tableList count];
 }

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 static NSString *DisclosureButtonIdentifier = @"DisclosurebutotonIdentifier";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DisclosureButtonIdentifier];
 if (cell == nil)
 {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DisclosureButtonIdentifier];
 }

 NSInteger row = [indexPath row];
 NSString *rowString = [tableList objectAtIndex:row];
 cell.textLabel.text = rowString;

 return cell;
 }




 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
{
    NSBundle *str = [tableList objectAtIndex:indexPath.row];
    if ([str isEqual:@"Gangan"])
    {
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *thePath = [bundle pathForResource:@"Gangan" ofType:@"mp4"];
        NSURL *theurl = [NSURL fileURLWithPath:thePath];
        moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theurl];

        [moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
        [self.view addSubview:moviePlayer.view];
        [moviePlayer setFullscreen:YES];

        [moviePlayer play];
    }
    else if ([str isEqual:@"SwimGood"])
    {
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *thePath = [bundle pathForResource:@"SwimGood" ofType:@"mp4"];
        NSURL *theurl = [NSURL fileURLWithPath:thePath];
        moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theurl];
        [moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
        [self.view addSubview:moviePlayer.view];
        [moviePlayer setFullscreen:YES];

        [moviePlayer play];

    }
    else if ([str isEqual:@"German Ice"])
    {
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *thePath = [bundle pathForResource:@"German Ice" ofType:@"mp4"];
        NSURL *theurl = [NSURL fileURLWithPath:thePath];
        moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theurl];
        [moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
        [self.view addSubview:moviePlayer.view];
        [moviePlayer setFullscreen:YES];

        [moviePlayer play];
    }

   }
   }
   @end

回答1:


In your view controller .m file, you should have

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

There you can return YES for supported orientations, which will enable you to rotate video.

Also not that if you use UINavigationController, this aproach wont work, unless all views managed by UINavigationController implement - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation the same way.

Also in your Targets -> "You Project" -> Summary, you can set supported orientations.

Edit: go here an look for UIInterfaceOrientation. There you have constants you need.

I would write it like this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation == UIInterfaceOrientationPortrait)
       return YES;  
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
       return YES; 
    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
       return YES;
}


来源:https://stackoverflow.com/questions/13065455/portrait-video-to-landscape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!