簡體   English   中英

無法播放mainBundle的MP4視頻文件

[英]Unable to play MP4 video file from mainBundle

所以我正在嘗試播放一個簡單的介紹動畫視頻文件,我已將其拖入XCode中的項目中,因此應該可以從我的mainBundle播放,對吧?

使用此代碼:

 NSURL *urlString = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"introvideo" ofType:@"mp4"]]; 
MPMoviePlayerController *player  = [[MPMoviePlayerController alloc] initWithContentURL:urlString];
[player play];

我收到此錯誤消息: *由於未捕獲的異常'NSInvalidArgumentException'終止應用程序,原因:'* - [NSURL initFileURLWithPath:]:nil string parameter'

任何幫助都會很棒!

這意味着您的代碼無法找到您的introvideo.mp4文件。 確保您已成功將該文件添加到捆綁包中。 您可以簽入項目的設置:復制捆綁資源。 在此輸入圖像描述

您的代碼不正確。 請相應更改以下內容。 仍然將您的視頻復制到您的捆綁資源中。

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:@"MacBook Pro with Retina Display" ofType:@"m4v"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
                                                 initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[playercontroller release];
playercontroller = nil;
NSLog(@"Video is Playing");

似乎pathForResource:ofType:返回nil。 檢查一下

  1. 這個文件確實被添加到“復制資源”構建階段;
  2. 你沒有在文件名中出錯 - 設備上的路徑區分大小寫。

您需要在應用程序的資源包中檢查該視頻是否可用。

正如您提到的那樣you getting this error message: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter

它只是表明問題在resourcePath中,意味着Resource Bundle中沒有這樣的文件。這就是為什么pathForResource返回nil路徑。

您需要再次放置該視頻文件並確保該文件存在於資源包中。

然后你應該去代碼作為Rehan也發布了。

  NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"video" ofType:@"mov" inDirectory:@""]];

  MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
  [[player view] setFrame:[self.view bounds]]; // Frame must match parent view
  [self.view addSubview:[player view]];
  [player play];
  [player release];

我希望對你來說可能是值得的。

謝謝

Xcode 9

這是一張更新的圖片。

轉到Build Phases> Copy Bundle Resources並查看您的文件是否存在。 如果不是,您可以按“+”按鈕添加它。

在此輸入圖像描述

NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"video" ofType:@"mov" inDirectory:@""]];

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[player view] setFrame:[self.view bounds]]; // Frame must match parent view
[self.view addSubview:[player view]];
[player play];
[player release];

我已將整個“Sounds”目錄放入項目中,並將其添加到“Copy bundle resources”部分。 它工作正常,但隨后開始崩潰。

修復是將目錄名稱添加到文件名。 例如,這並不工作:

SKAction.playSoundFileNamed("crash.caf", waitForCompletion: false)

......但確實有效:

SKAction.playSoundFileNamed("Sounds/crash.caf", waitForCompletion: false)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM