【问题标题】:Video doesn't show up on screen视频不显示在屏幕上
【发布时间】:2013-04-30 18:51:31
【问题描述】:

我在我的 VideosView.h 中导入了 MPMoviePlayerController。 在我的 VideosView.m 中,我嵌入了以下代码:

    NSString *path2 = [[NSBundle mainBundle] pathForResource:@"myVideo" ofType:@"mp4" inDirectory:@"images"];
    NSLog(@"%@", path2);

    MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
    myPlayer.shouldAutoplay = YES;
    myPlayer.repeatMode = MPMovieRepeatModeOne;
    myPlayer.fullscreen = YES;
    myPlayer.movieSourceType = MPMovieSourceTypeFile;
    myPlayer.scalingMode = MPMovieScalingModeAspectFit;
    myPlayer.contentURL =[NSURL fileURLWithPath:path2];
    myPlayer.view.frame = CGRectMake(0, 0, 500, 500);
    myPlayer.scalingMode = MPMovieScalingModeFill;
    myPlayer.controlStyle = MPMovieControlModeDefault;

    [self addSubview:myPlayer.view];
    [myPlayer play];

我在 Stackoverflow 上找到了这个示例,但无法使其正常工作。我的视频的链接是正确的(是的,它在图像文件夹中)。我的屏幕上出现了一个 500 x 500 像素的黑色矩形(当然是框架),但没有播放视频。

一些帮助会很棒。 W.

【问题讨论】:

    标签: iphone ios objective-c ipad ipod


    【解决方案1】:

    全局定义 myPlayer 对象

    在您的代码中,myPlayer 的生命周期以变量的范围结束。如果您在方法内部创建了。播放器以该方法的范围结束。

    @property(nonatomic, strong) MPMoviePlayerController *myPlayer;
    

    然后从你想要的任何地方初始化,

    _myPlayer = [[MPMoviePlayerController alloc] init];
    

    【讨论】:

    • 太棒了!认为可以在本地执行此操作。谢谢!
    【解决方案2】:
        self.moviePlayerView = [[MPMoviePlayerViewController alloc]initWithContentURL:videoURL];
        movie = [self.moviePlayerView moviePlayer];
    
        movie.controlStyle = MPMovieControlStyleNone;
    
        [movie setControlStyle:MPMovieControlStyleFullscreen];
    
        self.moviePlayerView.moviePlayer.shouldAutoplay=YES;
    
        [movie prepareToPlay];
    
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(playbackStateChanged)
                                                                                      name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    
    
        self.moviePlayerView.view.frame = CGRectMake(0.0f, 0.0f, 304.0f, 221.0f);
    
        [[self.moviePlayerView moviePlayer]play];
    
    
    add bewlow..
    
     - (void) playbackStateChanged
     {
    self.moviePlayerView.moviePlayer.shouldAutoplay=YES;
    
      MPMoviePlaybackState playbackState = [self.moviePlayerView.moviePlayer playbackState];
    
      switch (playbackState)
      {
    
        case MPMoviePlaybackStateStopped :
    
            break;
    
        case MPMoviePlaybackStatePlaying :
    
    
            break;
    
        case MPMoviePlaybackStateInterrupted :
    
    
            break;
    
        }
    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多