0

我是xcode,界面生成器和iphone模擬器的新手。我試圖通過單擊按鈕(這非常簡單)在xcode中播放電影/視頻。我正在使用xcode 3.2.4和iphone模擬器4.1。視頻沒有在iPhone模擬器中顯示(但我可以聽到來自視頻的音頻)

當我啓動iPhone模擬器並點擊按鈕啓動視頻時,音頻播放,但視頻被隱藏。就好像視頻在標籤欄後面(這是標籤欄應用程序的一部分)。我不知道如何讓視頻在前面播放。

下面的代碼:

-(IBAction)launchVideo2:(id)sender{ 

NSString *movieFile; 
MPMoviePlayerController *moviePlayer; 

movieFile = [[NSBundle mainBundle] 
    pathForResource:@"IGDIs_Video_Picture_Naming_iPhone" ofType:@"mp4"]; 
moviePlayer = [[MPMoviePlayerController alloc] 
     initWithContentURL: [NSURL fileURLWithPath: movieFile]]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
      selector:@selector(playMediaFinished:) 
      name:MPMoviePlayerPlaybackDidFinishNotification 
       object:moviePlayer]; 
moviePlayer.scalingMode = MPMovieScalingModeAspectFill; 
[moviePlayer play]; 
} 

-(void)playMediaFinished:(NSNotification*)theNotification 
{ 
MPMoviePlayerController *moviePlayer=[theNotification object]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
      name:MPMoviePlayerPlaybackDidFinishNotification 
       object:moviePlayer]; 

    [moviePlayer release]; 
} 

任何建議將是巨大的!

回答

2
+0

我不是最大的閱讀/理解文檔還沒有...你有一個使用MPMoviePlayerViewController的代碼的例子嗎? – Justin 2010-11-04 02:35:37

+1

試一下stackoverflow問題http://stackoverflow.com/questions/4077096/how-can-i-play-a-movie-locally-with-the-mpmovieplayerviewcontroller – 2010-11-04 02:43:54

+0

我從你上面提供的例子中精確地複製了代碼並添加了建議的代碼: – Justin 2010-11-07 00:35:07

0

它看起來並不像你所添加的電影,以便你的主要觀點。您需要添加如下內容:

... 
    [[moviePlayer view] setFrame:[self view] bounds]]; 
    [[self view] addSubview:[moviePlayer view]]; 
    [moviePlayer play]; 
} 

在播放之前。我假設launchVideo2方法位於擁有主視圖的UIViewController中。您可能需要更改[self view]以適合您的代碼,以對應您希望電影播放的視圖。

0

根據我的經驗,視頻不會顯示在SIM卡中。但是,音頻會。 (這是一個改進,因爲以前都不會玩。)

嘗試在設備上運行應用程序。如果視頻不在那裏顯示,請在這裏發帖,我們將解決問題。

+0

我在設備上試過了,它沒有工作... – Justin 2010-11-04 02:25:11

+0

看起來像你缺少[self presentMoviePlayerViewControllerAnimated:moviePlayer]; (見亞倫桑德斯評論) – joshpaul 2010-11-04 03:48:51

0

你有可能不相關的記憶問題有:

  • 在你的第一個方法,你不鬆開播放器,這是一個局部變量。
  • 在你的第二種方法中,你釋放了玩家,你不擁有。

正確的內存管理(即最佳實踐),你可以用伊娃做到這一點。