实现播放视频的时候自动横屏必须重写MPMoviePlayerViewController,具体代码如下:
1.重写MPMoviePlayerViewController
- //
- // DirectionMPMoviePlayerViewController.h
- // Direction
- //
- // Created by apple on 12-4-10.
- // Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
- //
- #import <MediaPlayer/MediaPlayer.h>
- @interface DirectionMPMoviePlayerViewController : MPMoviePlayerViewController
- @end
- //
- // DirectionMPMoviePlayerViewController.m
- // Direction
- //
- // Created by apple on 12-4-10.
- // Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
- //
- #import "DirectionMPMoviePlayerViewController.h"
- @implementation DirectionMPMoviePlayerViewController
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return UIDeviceOrientationIsLandscape(interfaceOrientation);
- }
- @end
2.初始化播放器,播放
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- [self customTitleView];
- NSString *mystr = @"http://114.112.50.220:8080/res/20120331/2FFCE63A-C997-4D8C-4C4F-4127D78A958E.m3u8";
- NSURL *myURL = [[NSURL alloc] initWithString:mystr];
- [self playMovieAtURL:myURL];
- }
- -(void)playMovieAtURL:(NSURL*)theURL
- {
- playerView = [[DirectionMPMoviePlayerViewController alloc] initWithContentURL:theURL];
- playerView.view.frame = self.view.frame;//全屏播放(全屏播放不可缺)
- playerView.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;//全屏播放(全屏播放不可缺)
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(myMovieFinishedCallback:)
- name:MPMoviePlayerPlaybackDidFinishNotification
- object:playerView];
- [playerView.moviePlayer play];
- [self presentMoviePlayerViewControllerAnimated:playerView];
- }
- // When the movie is done, release the controller.
- -(void)myMovieFinishedCallback:(NSNotification*)aNotification
- {
- DirectionMPMoviePlayerViewController* theMovie = [aNotification object];
- [[NSNotificationCenter defaultCenter] removeObserver:self
- name:MPMoviePlayerPlaybackDidFinishNotification
- object:theMovie];
- [theMovie release];
- }
原文链接:http://southking.iteye.com/blog/1481684