2013-07-04 110 views
4

我一直在爲此而努力了一段時間... 我試圖播放視頻我下載並保存使用AVPlayer,但沒有顯示出來。 什麼最讓我吃驚的是,如果我使用蘋果的示例網址測試HTTP實時流 該視頻確實起到。 (http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8) 然而,當我更改URL到本地文件路徑的視頻不能播放。我用iPhone錄製了視頻並驗證了格式(.mov)是可播放的。下面是代碼塊:AVPlayer不播放本地視頻

AVPlayer *mPlayer = [[AVPlayer alloc] init]; 
//mPlayer = [mPlayer initWithURL:[NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]]; 
mPlayer = [mPlayer initWithURL:filePath]; 
playerLayer = [AVPlayerLayer playerLayerWithPlayer:mPlayer]; 
[[[self view] layer] insertSublayer:playerLayer atIndex:3]; 
[playerLayer setFrame:self.view.bounds]; 
[mPlayer play]; 
NSLog(@"Playing video at: %@", filePath); 

示例輸出: Playing video at: /var/mobile/Applications/B298EE7D-D95D-4CCC-8466-2C0270E2071E/Documents/394665262.174180.mov

編輯: 通過使用解決[NSURL fileURLWithPath:local_url_string]

+2

如何創建'filePath'? –

+5

謝謝Martin!我通過使用'fileURLWithPath'解決了這個問題 – mauzepeda

+3

你不應該發送多個'init' /'init ...'消息給同一個對象。您應該只在第一行創建AVPlayer,例如'[[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:...]]''。 –

回答

2

僅有小塊代碼:

NSString *path = [[NSBundle mainBundle] pathForResource:@"splash" ofType:@"mp4"]; 
NSURL *videoURL = [NSURL fileURLWithPath:path]; 
AVPlayer* player = [AVPlayer playerWithURL:videoURL]; 
+1

我的視頻在本地服務器上,我想在我的設備上播放它。 – Rex

+0

@Rex只是改變NSURL * videoURL = [NSURL URLWithString:@ 「http://cdn3.viblast.com/streams/dash/vod-bunny/XXX.mp4」]; – WINSergey

+0

是的,我也在使用這個。但是不能在設備上播放我的視頻 – Rex