[英]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。 檢查一下
您需要在應用程序的資源包中檢查該視頻是否可用。
正如您提到的那樣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];
我希望對你來說可能是值得的。
謝謝
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.