我需要從服務器下載,保存和播放視頻文件。我的下載和保存技術似乎正在工作,但我無法播放視頻。視頻在服務器上的路徑是正確的,這個技術也適用於下載,保存和查看PNG。此外,MPMoviePlayerController似乎找到該文件,它只是無法播放它。我在視頻應該是黑色區域。播放下載的(未流式傳輸的)視頻
任何幫助,將不勝感激。以下是我的代碼(目標:iOS 6.1,xCode 5)。
//Downloading & Saving
-(void)downloadFileAtPath:(NSString *)path
{
NSURL *url = [NSURL URLWithString:path];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
[connection start];
}
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
//NSLog(@"%s", __PRETTY_FUNCTION__);
[[NSFileManager defaultManager] createFileAtPath:pathForCurrentFile contents:nil attributes:nil];
currentFile = [NSFileHandle fileHandleForUpdatingAtPath:pathForCurrentFile];
if (currentFile)
{
[currentFile seekToEndOfFile];
}
}
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
//NSLog(@"%s", __PRETTY_FUNCTION__);
if(currentFile != nil){
if (currentFile) {
[currentFile seekToEndOfFile];
}
[currentFile writeData:data];
}
}
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
NSLog(@"%s, %@", __PRETTY_FUNCTION__, error);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[currentFile closeFile];
}
下面是回放
//Playback
NSString *loopFilePath = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:vidFileName];
NSURL *vidURL = [NSURL fileURLWithPath:vidFilePath];
self.vidController = [[MPMoviePlayerController alloc] initWithContentURL:vidURL];
self.vidController.view.frame = CGRectMake(0, 0, 640, 480);
self.vidController.controlStyle = MPMovieControlStyleDefault;
[self.view addSubview:self.vidController.view];
[self.vidController prepareToPlay];
[self.vidController play];
對於FSM的愛,「技術」。 – random
它是什麼類型的文件? – diatrevolo