iOS:如何在目录中保存视频并在之后播放? [英] iOS: How to save a video in your directory and play it afterwards?
本文介绍了iOS:如何在目录中保存视频并在之后播放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 iOS
项目中工作。我希望应用程序可以通过网络以编程方式下载
。然后我想玩它。我知道如何从视频
资源
播放本地视频,但我的问题是如何下载它,并找到它播放。
我正在使用 MPMoviePlayerController
来运行视频。
I am working in an iOS
project. I want may application to download a video
from the internet programmatically
. Then I want to play it. I know how can I play a local video from the Resources
, but my question is how could I download it , and the find it to be played.
I am using MPMoviePlayerController
to run the video.
提前感谢
推荐答案
我找到了答案
这里保存了视频
NSString *stringURL = @"http://videoURL";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSString *documentsDirectory ;
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"videoName.mp4"];
[urlData writeToFile:filePath atomically:YES];
}
此代码用于播放其目录中的视频
and this code is for playing the video form its directory
NSString *filepath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"videoName.mp4"];
//video URL
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController play];
这篇关于iOS:如何在目录中保存视频并在之后播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文