2012-11-06 196 views
2

我正在使用iOS 6中包含的mediaplayer框架嘗試從應用內播放電影。我導入,然後:iOS中播放視頻的媒體播放器框架6

-(IBAction)playMovie:(id)sender 
{ 
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"buyTutorial" ofType:@"mov"]; 
    NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
    [self.view addSubview:moviePlayerController.view]; 
    moviePlayerController.fullscreen = YES; 
    [moviePlayerController play]; 
} 

當調用此函數時,視圖會變爲空白屏幕和無限加載。我已經嘗試過許多其他版本的這個實現,結果各不相同,都失敗了。在特定情況下的日誌是:

2012-11-05 21:19:27.900 [MPAVController] Autoplay: Disabling autoplay for pause 
2012-11-05 21:19:27.902 [MPAVController] Autoplay: Disabling autoplay 
2012-11-05 21:19:27.977 [MPAVController] Autoplay: Disabling autoplay for pause 
2012-11-05 21:19:27.978 [MPAVController] Autoplay: Disabling autoplay 
2012-11-05 21:19:27.984 [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 
2012-11-05 21:19:28.156 [MPAVController] Autoplay: Enabling autoplay 

想了解原因?這是我第一次嘗試播放視頻,現在它已成爲一場噩夢。

+0

嘗試調用'[moviePlayerController prepareToPlay];' –

+0

添加了[moviePlayerController prepareToPlay],但是這並沒有改變結果。 – AddisDev

+0

請參考http://stackoverflow.com/questions/11549143/playing-a-downloaded-video-from-the-documents-directory-with-cocoa-touch –

回答

9

在.h文件中添加以下

@property (nonatomic, strong) MPMoviePlayerController *controller; 

試試這個

-(IBAction)playMovie:(id)sender 
    { 
     NSString *filepath = [[NSBundle mainBundle] pathForResource:@"buyTutorial" ofType:@"mov"]; 
     NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
     MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
     [self.view addSubview:moviePlayerController.view]; 
     moviePlayerController.fullscreen = YES; 
    [moviePlayerController prepareToPlay]; 
     [moviePlayerController play]; 
[self setController:moviePlayerController]; 
    } 
+0

無法構建您的代碼,因爲類不包含任何方法setController – AddisDev

+0

編輯了上面的And。 PLZ檢查 –

+0

接受這個答案請。它幫助了我,而且是正確的。 – NDY

0
Am facing the same issue, you can else try playing it on the web view. 

    NSURL *url = [NSURL fileURLWithPath:self.filePath]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [self.docWebView loadRequest:request]; 
0

你的代碼的問題是變量 「moviePlayerController」 只是局部範圍內,經過這麼右你調用[moviePlayerController play];並退出本地變量釋放的函數(因爲在這裏有一個asyn操作,在這裏用play方法從隊列中播放)。它不會讓內容再次引用URL。所以你看到一個黑屏和一個不定式的「Loading ...」

你需要聲明一個類的實例變量,並將內容從局部變量複製到類的屬性,就像上面的@iAppDeveloper中的示例代碼。它應該工作!