2011-10-21 3 views
3

私は、このコードMPMoviePlayerViewControllerが動作しますが、MPMoviePlayerControllerは動作しません。なぜですか?

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"d" ofType:@"mp4"]; 
NSURL *url = [NSURL fileURLWithPath:filepath]; 

//part 1 
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: url]; 
[player.view setFrame: self.view.bounds]; 
//[player prepareToPlay]; 
//[player setShouldAutoplay:YES]; 
//[player setControlStyle:2]; 
[self.view addSubview: player.view]; 
[player play]; 

//part2 
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; 
[[mp moviePlayer] prepareToPlay]; 
[[mp moviePlayer] setShouldAutoplay:YES]; 
[[mp moviePlayer] setControlStyle:2]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mp]; 
[self presentMoviePlayerViewControllerAnimated:mp]; 

その2作品を持っている...彼らは同じURLを使用していないPART1と私はほとんど私が使用しているcopyedとADCサイト

からスニペットを貼り付けましたios5

+0

パート2を含むためのおかげで一部だけを見たことがあり、あなたのあなたの.hファイルを持っている必要がありますので、

1のコードの周りにそれはちょうど動作しないので私を狂って運転してきた。パート2のコードは素晴らしいです! – scum

答えて

-1

ビデオがロードされない限り、プレーヤービューをメインビューに追加することはできません。

ですから、この行置き換える必要があります。このいずれかで

[self.view addSubview: player.view]; 

[[NSNotificationCenter defaultCenter] 
    addObserver:self 
    selector:@selector(movieLoadStateDidChange:) 
    name:MPMoviePlayerLoadStateDidChangeNotification 
    object:player]; 

をそして、このメソッドを追加します。

-(void)movieLoadStateDidChange: (NSNotification*)notification{ 
    if (player.loadState & MPMovieLoadStatePlayable == MPMovieLoadStatePlayable) { 
     [[NSNotificationCenter defaultCenter] 
      removeObserver:self 
      name:MPMoviePlayerLoadStateDidChangeNotification 
      object:player] ; 
     [self.view addSubview:player.view]; 
    } 
} 
+0

MPMovieLoadStatePlayable == MPMovieLoadStatePlayable ??これは何ですか? – applefreak

0

私はiOSの5で同様の問題がありましたMPPlayerControllerと私はAppleのサンプルプロジェクトをチェックしましたが、違いはフレームを設定するだけなのでフレームを手動で設定しました完全にrked。

[[[self moviePlayer] view] setFrame:CGRectMake(0, 0, 320, 480)]; 
1

この質問が投稿されてから長い時間でしたが、答えはあなたがMPMoviePlayerController変数の参照を保持しなければならないということです。

関数にpart1のコードがある場合は、.hファイルにMPMoviePlayerController *playerと宣言します。あなたがそれをしないと(あなたはARCで開発中です)、あなたのプレイヤー変数は、その関数を終了するとすぐに割り当て解除されます。 player.viewself.viewに追加しているので、プレイヤーのビューは保持されますが、プレーヤーのコントローラーは保持されず、割り当てが解除されます。

MPMoviePlayerController *player; 

この場合には、あなたの関数のようなものでなければならない:

-(void) playMovie 
{ 
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
             pathForResource:@"mymovie" 
             ofType:@"mov"]];  

    player = [[MPMoviePlayerController alloc] initWithContentURL: url]; 
    [player.view setFrame: self.view.bounds]; 
    [self.view addSubview: player.view]; 
    [player play];   
} 
+0

これは明らかではありませんでした - ありがとう。 –

関連する問題