2016-07-29 286 views
6

enter image description here我正在使用AVPlayer播放視頻。我無法播放.mp4視頻。我正在看這個黑屏。這是我的代碼。無法在AVPlayer中播放mp4視頻

`NSString *filePath = [self.videoArray objectAtIndex:index]; 
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:filePath ofType: @"mp4]; 
url = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 
playerViewController = [[AVPlayerViewController alloc] init]; 
playerViewController.videoGravity = AVLayerVideoGravityResize; 
playerViewController.view.frame = self.view.bounds; 
playerViewController.showsPlaybackControls = NO; 
video=[AVPlayer playerWithURL:url]; 
playerViewController.player = _video; 
playerViewController.view.userInteractionEnabled = false; 
[video play]; 
[self.view addSubview:playerViewController.view];` 
+0

使用'NSLog'或調試點檢查filepath或soundFilePath是否有適當的值?還提供更多詳細信息,如錯誤日誌。 –

+0

'NSInvalidArgumentException',原因:'*** - [NSURL initFileURLWithPath:]:nil字符串參數' – Bharathi

+1

因此你的URL是錯誤的。 'self.video_Array'在這個數組中有哪些URL,它是一個HTTP鏈接還是一個本地URL。 –

回答

5

試試這個代碼我認爲它會爲你工作,因爲它似乎你的網址不是捆綁它從網絡。

// your filepath which is may be "http" type 
NSString *filePath = [self.video_Array objectAtIndex:index]; 
// http to NSURL using string filepath 
NSURL *url= [[NSURL alloc] initWithString:filePath]; 
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init]; 
playerViewController.videoGravity = AVLayerVideoGravityResize; 
playerViewController.view.frame = self.view.bounds; 
playerViewController.showsPlaybackControls = NO; 
video=[AVPlayer playerWithURL:url]; 
playerViewController.player = _video; 
playerViewController.view.userInteractionEnabled = false; 
[video play]; 
[self.view addSubview:playerViewController.view]; 
+0

謝謝。我添加了兩個同名的視頻文件。這是問題所在。 – Bharathi