4

我正在尝试在 UIWebView 上播放 YouTube 视频,如下所示:

// Create the URL
_videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.youtube.com/watch?v=%@", _videoID]];

// Create the request with the URL
NSURLRequest *requestObj = [NSURLRequest requestWithURL:_videoUrl];

// Load the request into the Web View
[_webView loadRequest:requestObj];

youtube页面显示,当我开始播放视频时,它没有旋转。

我花了一周时间寻找不同的解决方案,通过实施“shouldAutorotate”和“supportedInterfaceOrientations”,但没有成功!

我尝试的最后一件事是如果视频以全屏模式播放,则添加一个侦听器,在 AppDelegate.m 中,我将以下代码添加到“didFinishLaunchingWithOptions”:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];

并实施:

- (void) moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification {
self.allowRotation = YES; }

- (void) moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
self.allowRotation = NO; }

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.forceLandscapeRight) {
    return UIInterfaceOrientationMaskLandscapeRight;
}
if (self.allowRotation) {
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

return UIInterfaceOrientationMaskPortrait; }

问题是“moviePlayerWillEnterFullscreenNotification”或“moviePlayerWillExitFullscreenNotification”都没有被调用。

请帮忙!

4

3 回答 3

3

这是我在 Swift 中解决问题的方法。它基于上面的答案,但更简单:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
  // if it's our window, portrait. Other windows are (probably) the fullscreen video player ;)
  if self.window == window {
    return .Portrait
  } else {
    return [.Portrait, .Landscape]
  }
}
于 2015-10-19T16:48:37.453 回答
2

找到了答案:

我不得不使用

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeFinished:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];

代替

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];

并在我的 ViewControler 中实现这些方法

-(BOOL) shouldAutorotate {
 return NO; }

-(NSUInteger)supportedInterfaceOrientations{
 return UIInterfaceOrientationMaskPortrait; }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
 return UIInterfaceOrientationPortrait; }

更多详细信息:用于 youtube 嵌入式视频的 iOS 6.0+ 自动旋转

希望这会有所帮助:)

于 2014-05-29T11:07:36.460 回答
1

我刚刚做了一些研究,我想出了同时支持 iOS7 和 iOS8 的解决方案。要仅在播放 YouTube 视频时允许应用程序旋转,请按照以下步骤操作:

  1. AppDelegate.m

导入#import <MediaPlayer/MediaPlayer.h> 并实现函数“supportedInterfaceOrientationsForWindow”如下:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

if ([[window.rootViewController presentedViewController]
     isKindOfClass:[MPMoviePlayerViewController class]] || [[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"MPInlineVideoFullscreenViewController")] || [[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) {

    return UIInterfaceOrientationMaskAllButUpsideDown;
}else {

    if ([[window.rootViewController presentedViewController]
         isKindOfClass:[UINavigationController class]]) {

        // look for it inside UINavigationController
        UINavigationController *nc = (UINavigationController *)[window.rootViewController presentedViewController];

        // is at the top?
        if ([nc.topViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
            return UIInterfaceOrientationMaskAllButUpsideDown;

            // or it's presented from the top?
        } else if ([[nc.topViewController presentedViewController]
                    isKindOfClass:[MPMoviePlayerViewController class]]) {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    }
}

return UIInterfaceOrientationMaskPortrait;
}
  1. 视图控制器.m

在您的“ViewController.m”上,将以下代码添加到“ViewDidLoad”,如果视频以全屏模式播放,此代码允许您创建一个侦听器:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // SetUp notifications when video gets played and stopped
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeFinished:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];

    // Init The Video WebView
    [self initVideoWebView];
}

此功能允许您使用 YouTube URL 启动 WebView:

- (void) initVideoWebView {

    // Create the URL
    _videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.youtube.com/watch?v=%@", _videoID]];

    // Create the request with the URL
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:_videoUrl];

    // Load the request into the Web View
    [_webView loadRequest:requestObj];
}

最后要做的是在您的 ViewControler 上实现以下功能:

-(BOOL) shouldAutorotate {
 return NO; }

-(NSUInteger)supportedInterfaceOrientations{
 return UIInterfaceOrientationMaskPortrait; }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
 return UIInterfaceOrientationPortrait; }

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait; }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait; }

如果这有帮助,请提高利率:)

于 2014-11-22T11:54:16.770 回答