2014-10-30 25 views
0

發揮.tmp文件用的MPMoviePlayerController我從服務器下載視頻文件的MP4與NSURLSessionDownloadTask但在委託didFinishDownloadingToURL:(NSURL *)location,地點是這樣如何在Objective-C

file:///private/var/mobile/Applications/BEA4F9F8-D685-468F-B96C-AE7890ACFC5E/tmp/CFNetworkDownload_LQuGoo.tmp

當.tmp文件我嘗試初始化的MPMoviePlayerController控制檯顯示此錯誤

_itemFailedToPlayToEnd: { kind = 1;new = 2; old = 0;}和屏幕顯示黑色

我研究,我可以保存.tmp文件和p從我的目錄放置.mp4,但我不想這樣做,我想知道是否有一種方法直接與MPMoviePlayerController播放。我的代碼

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; 
       config.HTTPMaximumConnectionsPerHost = 1; 
NSURLSession *downloadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; 
NSURLSessionDownloadTask * downloadTask =[downloadSession downloadTaskWithURL:self.userRecord.URL completionHandler:nil]; 
[downloadTask resume]; 


-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { 

    self.userRecord.locationData = location; 

    [(NSObject *)self.delegate performSelectorOnMainThread:@selector(fileDownloaderDidFinish:) withObject:self waitUntilDone:NO]; 
     return; 

} 


-(void)fileDownloaderDidFinish:(FileDownloader *)fileDownloader { 


     [self setVideoPlayer:[[MPMoviePlayerController alloc] initWithContentURL:fileDownloader.userRecord.locationData]] 
     ; 
     [self.videoPlayer setControlStyle:MPMovieControlStyleNone]; 
     [self.videoPlayer.view setFrame:CGRectMake(0, 0, 320, 320)]; 
     [self.videoPlayer prepareToPlay]; 
     [self.vwContainerVideo addSubview:self.videoPlayer.view]; 
     [self.vwContainerVideo bringSubviewToFront:self.videoPlayer.view]; 
     [self.vwContainerVideo bringSubviewToFront:self.btPlay]; 
     [self.vwContainerVideo bringSubviewToFront:self.imPlay]; 
     [self.vwContainerThumbnail setHidden:YES]; 
     [AnimationHelper fadeIn:self.vwContainerVideo withDuration:0.5 andWait:0.5]; 
     [self.vwContainerVideo setHidden:NO]; 
     [self.videoPlayer play]; 

} 

回答

0

我的決定是保存文件的.tmp的目錄,並從目錄的URL初始化MPMoviePlayerController

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; 
       config.HTTPMaximumConnectionsPerHost = 1; 
NSURLSession *downloadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; 
NSURLSessionDownloadTask * downloadTask =[downloadSession downloadTaskWithURL:self.userRecord.URL completionHandler:nil]; 
[downloadTask resume]; 


-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { 
    self.userRecord.data = [NSData dataWithContentsOfURL:location]; 
    [self.userRecord.data writeToFile:[rootDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@.%@",self.userRecord.directory,self.userRecord.nameFile, self.userRecord.ext]] options:NSDataWritingWithoutOverwriting error:&error]; 
    self.userRecord.locationData = [NSURL fileURLWithPath:[rootDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@.%@",self.userRecord.directory,self.userRecord.nameFile, self.userRecord.ext]]]; 

    [(NSObject *)self.delegate performSelectorOnMainThread:@selector(fileDownloaderDidFinish:) withObject:self waitUntilDone:NO]; 
     return; 

} 


-(void)fileDownloaderDidFinish:(FileDownloader *)fileDownloader { 


     [self setVideoPlayer:[[MPMoviePlayerController alloc] initWithContentURL:fileDownloader.userRecord.locationData]] 
     ; 
     [self.videoPlayer setControlStyle:MPMovieControlStyleNone]; 
     [self.videoPlayer.view setFrame:CGRectMake(0, 0, 320, 320)]; 
     [self.videoPlayer prepareToPlay]; 
     [self.vwContainerVideo addSubview:self.videoPlayer.view]; 
     [self.vwContainerVideo bringSubviewToFront:self.videoPlayer.view]; 
     [self.vwContainerVideo bringSubviewToFront:self.btPlay]; 
     [self.vwContainerVideo bringSubviewToFront:self.imPlay]; 
     [self.vwContainerThumbnail setHidden:YES]; 
     [AnimationHelper fadeIn:self.vwContainerVideo withDuration:0.5 andWait:0.5]; 
     [self.vwContainerVideo setHidden:NO]; 
     [self.videoPlayer play]; 

}