2011-07-05 157 views
1

我是新來編寫Objective-C。我從Xcode 4.2開始。我發現很難找到學習的例子。MPMoviePlayer示例不起作用

最近,我開始寫我的應用程序,需要播放mp4視頻。 然後我發現MPMovieplayercontroller可以提供幫助。

這些代碼(從不同的例子的結論):

-(void)play // a function that trigger by pressing a button 
{ 
    [self.view addSubview:self.player.view]; 
    [self.player play]; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.view.backgroundColor = [UIColor greenColor]; 
    screen.backgroundColor = [UIColor redColor]; 

    NSString *videoFilePath = [[NSBundle mainBundle] pathForResource:@"ted" ofType:@"mp4"]; 

    if (videoFilePath == NULL) 
    { 
     return; 
    } 

    NSURL *videoURL =[NSURL fileURLWithPath:videoFilePath]; 
    self.player.view.frame = CGRectMake(300,300, 400,400); 
    self.player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL]; 
} 

這是行不通的。沒有顯示任何內容。我確定我的按鈕給出了迴應並調用了正確的功能(玩)。

我還在運行時使用配置文件檢查了應用程序。它說泄漏被發現。 現在我不知道我能做什麼。

我也是新來的stackoverflow。如果我在一個不正確的方式問,請讓我know.Thank你

+0

實際上我已經得到了解決方案。 我無法弄清楚這段代碼有什麼問題。 我DEL所有的代碼,並按照蘋果文件,一切都解決了。 – Ted

回答

1

我面臨的MPMoviPlayerController爲4.1.x版之前或等效版本工作正常,同樣的問題我已經差不多解決了這個問題,以播放視頻.. 。 以下是密碼,

- (void)viewDidLoad 
    { 
     [super viewDidLoad];  
     NSString *strURL = @"http://iphonetv.orange.mu:1935/live/ndtvgtimes.stream/playlists.m3u8"; 
     NSURLRequest *urlReq = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:strMovieURL]];  
     [myWebView loadRequest:urlReq];  
    } 

    - (BOOL)webView:(UIWebView *)myWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
    { 
     NSURL *url = [request URL]; 
     if ([[[url path] pathExtension] isEqualToString:@"mp4" ] || [[[url path] pathExtension] isEqualToString:@"m4v" ] || [[[url path] pathExtension] isEqualToString:@"m3u8" ]) 
     { 
      //video files are .mp4 or m4v, stream indexes are m3u8 
      //it's a movie, go play! 
      [self playMovieAtURL:url]; 
      return YES; 
     } 
     else 
     { 
      return [super webView:myWebView shouldStartLoadWithRequest:request navigationType:navigationType]; 
     } 
    } 

    -(void)playMovieAtURL:(NSURL*)theURL 
    { 
     theMovie = [[MPMoviePlayerController alloc] initWithContentURL:theURL];  
     theMovie.scalingMode = MPMovieScalingModeAspectFit; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie]; 
     [theMovie play]; 
    } 

    -(void)myMovieFinishedCallback:(NSNotification*)aNotification 
    { 
     MPMoviePlayerController* theMovie1 = [aNotification object]; 
     //theMovie = [aNotification object]; 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie1]; 
     [theMovie stop]; 
     float version = [[[UIDevice currentDevice] systemVersion] floatValue]; 
     if (version >= 3.0) 
     { 
      // iPhone 3.0 code here 
      // theMovie.initialPlaybackTime = -1.0; //Breaks on 2.x compiling 
      [theMovie setInitialPlaybackTime: -1.0]; //Only gives a warning on 2.x :) 
      } 

     [theMovie release]; 
     NSLog(@"Go STOP received"); 
    }