5

我試圖讓我的應用播放已下載到文檔目錄的視頻文件。我知道這個文件是越來越下載,但我似乎無法得到發揮的文件,這裏是我的代碼:使用Cocoa-Touch從文檔目錄播放下載的視頻

-(IBAction)play{ 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"%@/piggy.m4v"]; 

NSURL *movieURL = [NSURL fileURLWithPath:path]; 


_player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
[self.view addSubview:_player.view]; 

_player.controlStyle = MPMovieControlStyleDefault; 
_player.shouldAutoplay = YES; 


[_player setFullscreen:YES animated:YES]; 


[_player play]; 

} 
+0

你在的NSString有一個錯誤*路徑定義你不需要%@ /。但是如果你從控制檯獲得輸出或錯誤,你還沒有說過什麼。 – Panagiotis 2012-07-18 19:52:35

+0

你確定這個東西存在於文件目錄中,而不是應用程序包中?我可以從包中加載視頻。 – CodaFi 2012-07-18 20:09:19

+0

建議一句話:按照新的存儲指南(底線:它使備份緩慢/沉重),我的應用因爲將視頻保存到/ Documents而被拒絕。 **如果**您的目標iOS 5.0.1或更高版本,則可以將/ Documents中的子目錄標記爲「跳過備份」。否則,你唯一的選擇是保存到/緩存(並設計一種方式來重新下載視頻,當它被刪除) – 2012-07-18 21:03:31

回答

0

問題將是這一行:NSString *path = [documentsDirectory stringByAppendingPathComponent:@"%@/piggy.m4v"];

改變,要NSString *path = [documentsDirectory stringByAppendingPathComponent:@"piggy.m4v"];

+0

@untitled:你得到的錯誤是什麼? – 2012-07-18 19:58:59

+0

我從控制檯得到上述輸出,我剛剛運行應用程序,也有這個錯誤錯誤(空)設置播放模式爲'AVAudioSessionModeDefault' – untitled 2012-07-18 20:04:57

11

這看起來像某種錯誤的,但你必須設置你的路徑那樣:

NSString *vidPath = [[NSBundle mainBundle] pathForResource:@"promo" ofType:@"mp4"]; 
NSURL *url = [NSURL fileURLWithPath:vidPath isDirectory:NO]; //THIS IS THE KEY TO GET THIS RUN :) 
[introPlayer setContentURL:url]; 
+0

謝謝sooo多。你救了我 – cantfindaname88 2012-10-04 04:25:04