2012-05-03 79 views
9

的iOS 5.1,這裏是代碼:的MPMoviePlayerController給了我黑色的空視圖,沒有視頻播放

MPMoviePlayerController *player = 
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]]; 
    [player prepareToPlay]; 
    [player.view setFrame: self.view.bounds]; // player's frame must match parent's 
    [self.view addSubview: player.view]; 
    // ... 
    [player play]; 
其實

,這僅僅是一樣的蘋果基準說,但是當我點擊按鈕使用此功能,只有一個黑色的Rectangle呆在那裏,沒有視頻播放,沒有聲音發生,沒有暗戀,所以我想知道如何使這項工作

+0

我的代碼工作與否? – Dinesh

回答

1

請嘗試下面的代碼從您的項目播放視頻資源:

NSBundle *bundle = [NSBundle mainBundle]; 
    NSString *moviePath = [bundle pathForResource:@"map" ofType:@"mp4"]; 
    NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain]; 

    MPMoviePlayerController *player = 
    [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
    [player prepareToPlay]; 
    [player.view setFrame: self.view.bounds]; // player's frame must match parent's 
    [self.view addSubview: player.view]; 
    // ... 
    [player play]; 

謝謝..!

+0

由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'*** - [NSURL initFileURLWithPath:]:nil字符串參數'並感謝您的幫助,但我曾試過這種代碼,這是控制檯信息 –

+0

將您的視頻添加到「複製束資源」。 –

1

建立在@ Dinesh的回答,您的URL創建不正確。值爲

[NSURL URLWithString:@"map.mp4"] 

將爲零,因爲@「map.mp4」不是有效的URL。要從應用程序的包中創建有效的網址,請執行Dinesh的說法。要創建一個你不喜歡

[NSURL URLWithString:@"http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"] 

的URL的東西應該包含所有遠程URL它的部分:協議,主機,路徑,文件。

乾杯,菲利普

+0

謝謝,但實際上我想使用本地mp4文件,而不是流,[NSBundle mainBundle]似乎工作。 –

+1

我很高興它爲你工作。請考慮授予Dinesh的答案。這對你來說也是最好的,因爲不授予它會傷害你的聲譽。 – fsaint

17

對不起,很晚回覆。解決方案有點奇怪。但它幫助我繼續前進,同時面臨同樣的問題。

MPMovieSourceTypeStreaming是你錯過的東西。

MPMoviePlayerController *player = 
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]]; 
[player prepareToPlay]; 
[player.view setFrame: self.view.bounds]; // player's frame must match parent's 
player.movieSourceType = MPMovieSourceTypeStreaming; 
[self.view addSubview: player.view]; 
// ... 
[player play]; 
+0

這不是原來的海報問題,但它絕對是我的! – shulmey

+0

so @shulmey,有幫助嗎? –

+4

我的問題是我沒有設置movieSourceType屬性。所以如果它幫助任何人。確保你設置了這個屬性。默認情況下,它必須設置爲MPMovieSourceTypeUnknown。當我把它放到MPMovieSourceTypeFile。它爲我工作。 –

0

你的球員應該是你的視圖控制器的屬性,像這樣:

.H:

@property(nonatomic,weak)MPMoviePlayerController *moviePlayer; 

.M:

MPMoviePlayerController *player = 
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]]; 
    [player prepareToPlay]; 
    [player.view setFrame: self.view.bounds]; // player's frame must match parent's 
    //set the player to your property 
    self.moviePlayer=player; 

    [self.view addSubview: self.moviePlayer.view]; 
    // ... 
    [self.moviePlayer play]; 
0

請使用MPMoviePlayerViewController因爲的MP4文件。當你使用MOV然後工作完美!

MPMoviePlayerViewController *moviePlayerViewController; 

-(void)PlayVideoController:(NSString*)videoUrl1 { 
    NSURL *fileURL = [NSURL URLWithString:videoUrl1]; 
    moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL]; 

    // Register for the playback finished notification. 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer]; 

    //Present 
    [self presentMoviePlayerViewControllerAnimated:moviePlayerViewController]; 

    // Play the movie! 
    moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile; 
    [moviePlayerViewController.moviePlayer play]; 
} 

-(void)myMovieFinishedCallback:(NSNotification*)aNotification { 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer]; 
    [moviePlayerViewController release], moviePlayerViewController = nil; 
} 
4

你可能會過早地發送您的

[player play] 

消息。

使用下列通知觀測

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

爲了偵聽LoadState的改變來播放。要檢查是否存在更新這樣做:

- (void)playMovie:(NSNotification *)notification { 
    MPMoviePlayerController *player = notification.object; 
    if (player.loadState & MPMovieLoadStatePlayable) 
    { 
     NSLog(@"Movie is Ready to Play"); 
     [player play]; 
    } 
} 

電影應該開始播放一旦它準備好了。

1

在主窗口添加您的電影播放器​​視圖,如:

MPMoviePlayerController *player = 
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]]; 
    [player prepareToPlay]; 
    [player.view setFrame: self.view.bounds]; 

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
[appDelegate.window addSubview: player.view]; 

[player play]; 

希望如此,很有效!

0

您可能希望跨空格檢查視頻網址。以下代碼適用於我。

- (IBAction)btnPlayVideo:(id)sender { 

    self.strVideoUrl = [self.strVideoUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSURL *fileURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", self.strVideoUrl]]; 
    NSLog(@"Url to play = %@", self.strVideoUrl); 

     self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 

     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(moviePlaybackComplete:) 
                name:MPMoviePlayerPlaybackDidFinishNotification 
                object:self.moviePlayerController]; 
     [self.moviePlayerController.view setFrame:self.view.bounds]; 
     [self.view addSubview:self.moviePlayerController.view]; 
     self.moviePlayerController.shouldAutoplay = YES; 
     self.moviePlayerController.fullscreen = YES; 
     self.moviePlayerController.controlStyle=NO; 
     [self.moviePlayerController prepareToPlay]; 
     self.moviePlayerController.movieSourceType = MPMovieSourceTypeStreaming; 

     [self.moviePlayerController play]; 

} 

- (void)moviePlaybackComplete:(NSNotification *)notification 
{ 
    self.moviePlayerController = [notification object]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:MPMoviePlayerPlaybackDidFinishNotification 
                object:self.moviePlayerController]; 

    [self.moviePlayerController.view removeFromSuperview]; 
    self.closeVideAdProp.hidden = NO; 
    btnCloseAd.hidden=FALSE; 
} 
相關問題