2010-06-22 38 views
3

当运行在设备3.1.2,为什么它也通过if(NSClassFromString(@“MPMoviePlayerViewController”)!= nil) 并做iOS4的代码,然后它会崩溃,如何解决这个问题?Media Player基于iOS4和部署iOS3的问题

   if(NSClassFromString(@"MPMoviePlayerViewController") != nil) { 
        // iOS 4 code 
        NSLog(@"MPMoviePlayerViewController"); 
        MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:AppDelegate.PushLink]]; 
        if (mp) { 
         // save the movie player object 
         self.theMovie4 = mp; 
         [mp release]; 
         //Present      
         [self presentMoviePlayerViewControllerAnimated:self.theMovie4]; 

         // Play the movie! 
         self.theMovie4.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming; 
         [self.theMovie4.moviePlayer play]; 
        } 
       } 
       else { 
        //iOS 3 Code 
        AppDelegate = nil; 
        AppDelegate = [[UIApplication sharedApplication] delegate]; 
        [AppDelegate ForceHideNavigationBar]; 
        theMovie3 = nil; 

        [[NSNotificationCenter defaultCenter] addObserver:self 
                  selector:@selector(moviePreloadDidFinish:) 
                   name:MPMoviePlayerContentPreloadDidFinishNotification 
                   object:theMovie3]; 

        [[NSNotificationCenter defaultCenter] addObserver:self 
                  selector:@selector(moviePlayBackDidFinish:) 
                   name:MPMoviePlayerPlaybackDidFinishNotification 
                   object:theMovie3]; 

        // Register to receive a notification when the movie scaling mode has changed. 
        [[NSNotificationCenter defaultCenter] addObserver:self 
                  selector:@selector(movieScalingModeDidChange:) 
                   name:MPMoviePlayerScalingModeDidChangeNotification 
                   object:theMovie3]; 

        theMovie3 = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:AppDelegate.PushLink]]; 

        [theMovie3 play]; 

       } 

回答

15

我正在做一个非常类似的事情,我的应用程序使用4.0作为基础SDK,但我也瞄准iOS3设备。我必须检查操作系统版本以正确提供视频播放的正确代码。例如:

- (void) playMovieAtURL: (NSURL*) theURL { 
    NSLog(@"playMovieAtURL"); 

if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2) { 
      NSLog(@"> 3.2"); 
      MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:theURL]; 

      if (mp) 
      { 
       // save the movie player object 
       //self.moviePlayerViewController = mp; 
       //[mp release]; 
       [self presentMoviePlayerViewControllerAnimated:mp]; 
       mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile; 
       [mp.moviePlayer play]; 
       [mp release]; 
      } 

     } 
    else if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2) { 
      NSLog(@"< 3.2"); 

      MPMoviePlayerController* theMovie = [[MPMoviePlayerController alloc] initWithContentURL: theURL]; 

      theMovie.scalingMode = MPMovieScalingModeAspectFill; 

      // Register for the playback finished notification 
      [[NSNotificationCenter defaultCenter] 
      addObserver: self 
      selector: @selector(myMovieFinishedCallback:) 
      name: MPMoviePlayerPlaybackDidFinishNotification 
      object: theMovie]; 

      // Movie playback is asynchronous, so this method returns immediately. 
      [theMovie play]; 



     } 

    } 

希望这有助于!

+0

谢谢stitz – RAGOpoR 2010-06-28 00:52:17

+1

这是一个巨大的帮助。谢谢你! – 2010-08-13 04:23:35

+0

非常感谢!它拯救了我的一天! – 2010-09-04 06:44:22

0
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) 
     name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 

    NSString *movpath = [[NSBundle mainBundle] pathForResource:@"splash" ofType:@"mp4"]; 
    MPMoviePlayerViewController* mpviewController = [[MPMoviePlayerViewController alloc] 
         initWithContentURL:[NSURL fileURLWithPath:movpath]]; 
    [window addSubview:mpviewController.view]; 
     [window makeKeyAndVisible]; 
    MPMoviePlayerController *mp = [mpviewController moviePlayer]; 
    [mp prepareToPlay]; 
    [[mpviewController moviePlayer] play]; 

此代码将在iOS4的

工作,这一次的电影完成了,因为你必须创建通知的方法,但不退出。此外,您还需要在那里发布影片,以便您可以捕获内存泄漏。