2012-11-15 27 views
21

播放MP4視頻文件,所以我想打,我已經拖進我的XCode項目,因此應該能夠從我的mainBundle玩,右一個簡單的介紹動畫的視頻文件?無法從mainBundle

有了這個代碼:

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

我收到此錯誤信息: *終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因是: '* - [NSURL initFileURLWithPath:]:零字符串參數'

任何幫助將是巨大的!

+0

首先,我的問題是,introvideo.mp4文件是不存在主束。其次,我需要將我的播放器對象設置爲全局 - 這使得視頻播放效果很好。感謝所有的建議,夥計們! – PinkFloydRocks

回答

57

這意味着你的代碼不能找到你introvideo.mp4文件。確保您已成功將該文件添加到您的軟件包。您可以檢入您的項目設置:複製包資源。 enter image description here

+0

感謝您的回答 - 我在哪裏可以找到Copy Bundle Resource?這是一個複選框嗎?這在過去對我來說從來都不是問題,所以現在它突然出現了,這有點怪怪的。 – PinkFloydRocks

+0

謝謝 - 手動添加視頻到捆綁確實解決了崩潰! – PinkFloydRocks

+0

不客氣。 – sunkehappy

2
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]; 
+0

好的,所以資源沒有被複制到捆綁包中,並且在手動添加文件後崩潰消失了。但使用您的代碼播放該文件不會顯示該視頻。它顯示全部黑色MPMoviePlayerController沒有控制器 - 任何想法爲什麼? – PinkFloydRocks

+0

我可否知道您使用的是哪個版本的iOS? – rehan

3

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

  1. 該文件確實添加到「複製資源」構建階段;
  2. 您沒有在文件名中犯錯 - 設備上的路徑區分大小寫。
3

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

你提到 you getting this error message: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter

它只是表明問題出在資源路徑上,意味着在資源包中沒有這樣的文件存在。這就是爲什麼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]; 

我希望它可能值得你充滿。

感謝

4

您的代碼是不正確的。請相應地更改以下內容。仍將您的視頻複製到您的捆綁資源中。

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"); 
1

我已將整個「聲音」目錄放入項目中,並將其添加到「複製束資源」部分。它工作正常,但後來開始崩潰。

此修復程序是將目錄名添加到文件名。例如,這沒有工作:

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

...但這種工作:

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

的Xcode 9

這裏是一個更新的圖像。

轉到構建階段>複製包資源,看看你的文件是存在的。如果不是,您可以通過按「+」按鈕來添加它。

所有的

enter image description here

相關問題