简体   繁体   English

基于iOS4并部署iOS3的Media Player存在问题

[英]A problem with Media Player base on iOS4 and deploy iOS3

when Run on Device 3.1.2, why it also pass if(NSClassFromString(@"MPMoviePlayerViewController") != nil) and do code of iOS4 then it will crash , how to fix this issues? 当在Device 3.1.2上运行时,为什么它也传递if(NSClassFromString(@“MPMoviePlayerViewController”)!= nil)并且执行iOS4的代码然后它会崩溃,如何解决这个问题?

               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];

                }

I am doing a very similar thing on my app which is using 4.0 as the base SDK and yet I am targeting iOS3 devices too. 我在我的应用程序上做了一个非常类似的事情,它使用4.0作为基础SDK,但我也瞄准iOS3设备。 I had to check the OS version to properly provide the right code for the video playback. 我必须检查操作系统版本,以便为视频播放正确提供正确的代码。 For example: 例如:

- (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];



        }

    }

Hope this helps!! 希望这可以帮助!!

[[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];

This code will work in iOS4 此代码适用于iOS4

It however does quit once the movie completes because you must create the method for the notification. 但是,一旦电影完成,它就会退出,因为您必须为通知创建方法。 Also you need to release the movie there so that you can catch the memory leak. 此外,您需要在那里发布电影,以便您可以捕获内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM