2013-04-30 180 views
0

我在我的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]; 

我發現#2這個例子中,卻無法得到它的工作。鏈接到我的視頻是正確的(是的,它在圖像文件夾中)。我的屏幕上顯示一個500×500像素的黑色矩形(課程框架),但沒有播放視頻。

有些幫助會很大。 W.

回答

2

定義myPlayer對象全球

在你的代碼

,該myPlayer的生活與變量的作用域結束。如果你在一個方法內創建。玩家以該方法的範圍結束。

@property(nonatomic, strong) MPMoviePlayerController *myPlayer; 

然後初始化從任何你想要的,

_myPlayer = [[MPMoviePlayerController alloc] init]; 
+0

真棒!努力可以在本地做到這一點。謝謝! – 2013-04-30 19:27:49

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

    } 

}