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」を実装することで、別の解決策を探すのに1週間を費やしましたが、成功しませんでした!

最後に試したのは、ビデオがフルスクリーン モードで再生されている場合にリスナーを追加することです。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 に答える
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. ViewController.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 に答える