在我的应用中,我从服务器获取视频网址列表并下载并以.mp4
格式保存到我应用的文档目录路径。下载过程为ASIHTTPRequest
方法,然后按照自定义视频名称进行保存。
下载视频方法:
-(void)downloadVideoFile: (NSString *)UrlString URLname:(NSString *)videoName
{
NSString* urlTextEscaped = [UrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL * urlStr = [NSURL URLWithString:urlTextEscaped];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:urlStr];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedVideoPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", videoName]];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:savedVideoPath];
if(!fileExists)
{
[request setDownloadDestinationPath:savedVideoPath];
[request setDelegate:self];
[request setTimeOutSeconds:800];
[request setNumberOfTimesToRetryOnTimeout:3];
[request startAsynchronous];
}
}
当我在MPMoviePlayerController
播放器上播放视频时,通过从文档目录中获取已下载的视频,正在播放,但没有音频。
这是MPMoviePlayerController播放器方法:
-(void)playVideoFile:(int)index_SavedPathIndex
{
index_Selected_CollectionVwCell = index_SavedPathIndex;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedVideoPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",[arrayProgramID objectAtIndex:index_SavedPathIndex]]];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:savedVideoPath];
if(fileExists)
{
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:savedVideoPath]];
[player prepareToPlay];
player.view.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:player.view];
player.controlStyle = MPMovieControlStyleNone;
player.scalingMode = MPMovieScalingModeAspectFill;
player.view.userInteractionEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playbackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[player play];
}
}
我直接在文档目录中播放Quick Time Player
中的视频,即使它没有 NO AUDIO 播放。
然后我只是从窗口系统中的文档目录中播放下载的视频,奇怪的是它与音频一起播放。
我还尝试了另一种下载方法但不影响。我也改变了视频格式,但音频错误相同。
如果有人可以,请帮助: 提前致谢。