2013-06-24 20 views
19

मैं अपने ऐप में एक वीडियो स्ट्रीम करने की कोशिश कर रहा हूं। जिस विधि को मैंने पाया है वह है:MPMoviePlayerViewController | लैंडस्केप मोड की अनुमति दें

NSURL *theMovieURL = [NSURL URLWithString:self.data.trailer]; 
     if (theMovieURL) 
     { 
      self.movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:theMovieURL]; 
      [self presentMoviePlayerViewControllerAnimated:self.movieController]; 
      [self.movieController.moviePlayer play]; 
     } 

मुझे यकीन नहीं है कि यह सबसे पारंपरिक है, लेकिन यह काम करता है।

समस्या यह है कि मैं केवल वीडियो के लिए लैंडस्केप मोड को अनुमति देने का तरीका नहीं समझ सकता। क्या मुझे shouldAutorotate या shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation जैसे कुछ उपयोग करना चाहिए, और कैसे?

एफवाईआई, संपूर्ण ऐप केवल पोर्ट्रेट मोड की अनुमति देता है।

आपकी मदद के लिए धन्यवाद।

उत्तर

34

shouldAutoRotate आईओएस 6 के रूप में बहिष्कृत किया गया है और जब तक आप < 6 के लिए नहीं जा रहे हैं, तब से बचा जाना चाहिए।

इसके बजाय, आप supportedInterfaceOrientations और preferredInterfaceOrientationForPresentation विधियों को ओवरराइड करना चाहते हैं।

इस मामले में, यदि आप मीडिया प्लेयर उपवर्ग नहीं करना चाहते तो आप एक विधि एप्लिकेशन प्रतिनिधि में इतनी तरह रद्द कर सकते थे:

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]]) 
    { 
     return UIInterfaceOrientationMaskAllButUpsideDown; 
    } 
    else 
    { 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 
+12

जांचें कि प्रस्तुत किए गए व्यू कंट्रोलर को बर्खास्त किया जा रहा है (isBeingDismissed प्रॉपर्टी) है, अन्यथा प्रस्तुत करने वाला व्यू कंट्रोलर लैंडस्केपोड में दिखाई देगा – peko

17

@peko सही तरीका कहते हैं। जब उपयोगकर्ता पूर्णस्क्रीन वीडियो से निकलता है, तो इस विधि को MPMoviePlayerViewController कक्षा के साथ फिर से बुलाया जाता है। आपको यह जांचना चाहिए कि इसे खारिज कर दिया गया है या नहीं, यदि आप नहीं करते हैं, जब उपयोगकर्ता वीडियो से बाहर निकलता है, तो मुख्य विंडो लैंडस्केप मोड में रहेगी, और आप MPInlineVideoFullscreenViewController कक्षा भूल गए हैं। जब आप एम्बेड प्लेयर (पूर्णस्क्रीन नहीं) का उपयोग करते हैं तो इसे उस कक्षा के नाम से बुलाया जाता है।

मैंने ऐसा किया। इससे मेरा काम बनता है।

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)windowx 
{ 
    if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]] || 
    [[self.window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"MPInlineVideoFullscreenViewController")]) 
    { 
     if ([self.window.rootViewController presentedViewController].isBeingDismissed) 
     { 
      return UIInterfaceOrientationMaskPortrait; 
     } 
     else 
     { 
      return UIInterfaceOrientationMaskAllButUpsideDown; 
     } 
    } 
    else 
    { 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 
0

फिल्म प्लेयर, पहले प्रस्तुत दृश्य नियंत्रक नहीं हो सकता है तो आप इस

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { 
    UIViewController* playerController = self.window.rootViewController.presentedViewController; 
    while (playerController && ![playerController isKindOfClass:[MPMoviePlayerViewController class]]) { 
     playerController = playerController.presentedViewController; 
    } 
    if (playerController && !playerController.isBeingDismissed) { 
     return UIInterfaceOrientationMaskAllButUpsideDown; 
    } else { 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 

हालांकि MPMoviePlayerViewController की तरह कुछ करना चाहिए पदावनत किया गया है ताकि आप AVPlayerViewController बजाय का उपयोग करें और में भी अपने वर्ग की जाँच करनी चाहिए यह पाश

0

सबसे अच्छा तरीका है अब तक अगर rootViewController प्रकार AVPlayerViewController या MPMoviePlayerViewController का एक बच्चा है तो आप वापसी [.portrait, .landscapeLeft, .landscapeRight] और बाकी .portrait इस AppDelegate समारोह को लागू करने और जांच करने के लिए है।

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { 
    if let _ = UIApplication.shared.keyWindow?.rootViewController?.childViewControllers.first as? AVPlayerViewController { 
     return [.portrait, .landscapeLeft, .landscapeRight] 
    } 
    return .portrait 
} 

क्योंकि सेब एक अन्य UIWindow में इस ViewController प्रस्तुत करता है आप UIApplication की keyWindow पर जांच होनी चाहिए ताकि आप विंडो घोषित किया जाता है AppDelegate में यह इतना काम नहीं करेगा सावधान रहना होगा पर कि चेक करने की कोशिश करता है, तो।

संबंधित मुद्दे