代码之家  ›  专栏  ›  技术社区  ›  4thSpace wkw

MPMoviePlayerController在iOS4中不工作

  •  1
  • 4thSpace wkw  · 技术社区  · 14 年前

    我用的是 iOS 4 + MPMoviePlayerController

    MPMoviePlayerController 去玩。在模拟器(带iOS4的iPhone设备)中,我听到声音,但没有视频。在设备上(iphone3gs和iOS4),我什么都没有得到。这是我的密码:

        MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
        moviePlayer.movieControlMode = MPMovieControlModeDefault;
    
        if ([moviePlayer respondsToSelector:@selector(view)]) {
            moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
            [moviePlayer.view setFrame:self.view.bounds];
            [self.view addSubview:moviePlayer.view];
        }
    
        [moviePlayer play];
    

    知道我做错了什么吗?

    2 回复  |  直到 7 年前
        1
  •  9
  •   Art Gillespie    14 年前

    你的代码的问题是没有调用 setFullscreen:animated 在你的 MPMoviePlayerController 实例。

    - (void)willEnterFullscreen:(NSNotification*)notification {
        NSLog(@"willEnterFullscreen");
    }
    
    - (void)enteredFullscreen:(NSNotification*)notification {
        NSLog(@"enteredFullscreen");
    }
    
    - (void)willExitFullscreen:(NSNotification*)notification {
        NSLog(@"willExitFullscreen");
    }
    
    - (void)exitedFullscreen:(NSNotification*)notification {
        NSLog(@"exitedFullscreen");
        [self.movieController.view removeFromSuperview];
        self.movieController = nil;
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    - (void)playbackFinished:(NSNotification*)notification {
        NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
        switch ([reason intValue]) {
            case MPMovieFinishReasonPlaybackEnded:
                NSLog(@"playbackFinished. Reason: Playback Ended");         
                break;
            case MPMovieFinishReasonPlaybackError:
                NSLog(@"playbackFinished. Reason: Playback Error");
                break;
                case MPMovieFinishReasonUserExited:
                NSLog(@"playbackFinished. Reason: User Exited");
                break;
            default:
                break;
        }
        [self.movieController setFullscreen:NO animated:YES];
    }
    
    - (void)showMovie {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
        NSURL* movieURL =  [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"tron" ofType:@"mov"]];
        self.movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
        if ([self.movieController respondsToSelector:@selector(view)]) {
            self.movieController.view.frame = self.view.frame;
            [self.view addSubview:movieController.view];
            [self.movieController setFullscreen:YES animated:YES];
        }
        [self.movieController play];
    }
    
    // This method is set as the action for an on-screen button
    - (void)movieTime:(id)sender {
        [self showMovie];
    }
    
        2
  •  -4
  •   Jon Shier    14 年前

    为了 MPMoviePlayerController view 是属性,不是方法,所以不能使用 respondsToSelector: