从localhost播放视频

时间:2011-11-16 10:25:42

标签: iphone video

我已经从这个路径的localhost上从服务器端下载了一个视频:

/Users/adrian/Library/Application Support/iPhone Simulator/4.3.2/Applications/60584763-39F6-4124-805D-BEFDE453206D/Documents/videos/bar.mpeg

事实上,视频存在于这条道路上。

但是当我尝试播放时,屏幕变黑,没有播放视频。

我是这样做的:

 moviePlayer = [[CustomMoviePlayerViewController alloc] initWithPath:[NSString stringWithFormat:@"/Users/adrian/Library/Application Support/iPhone Simulator/4.3.2/Applications/60584763-39F6-4124-805D-BEFDE453206D/Documents/videos/bar.mpeg"]];
    [moviePlayer readyPlayer];
    [self presentModalViewController:moviePlayer animated:YES];

当我在资源文件夹中放入相同的视频时,一切都很完美。所以我认为这个问题与路径有关。那么问题是什么呢?

2 个答案:

答案 0 :(得分:1)

解决方案:

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setHTTPMethod:@"GET"];
NSError *error;
NSURLResponse *response;

NSString *documentFolderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];  
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *videosFolderPath = [documentFolderPath stringByAppendingPathComponent:@"videos"];

BOOL isDir;
if (([fileManager fileExistsAtPath:videosFolderPath isDirectory:&isDir] && isDir) ==FALSE)
{
    [[NSFileManager defaultManager] createDirectoryAtPath:videosFolderPath attributes:nil];
}
NSData *urlData;

[request setURL:[NSURL URLWithString:itemString]];
urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *filePath = [[videosFolderPath stringByAppendingPathComponent:@"bar3.mp4"] retain];

BOOL written = [urlData writeToFile:filePath atomically:NO];

if(written)
{
    NSLog(@"Saved to file: %@", filePath);
    moviePlayer = [[CustomMoviePlayerViewController alloc] initWithPath:filePath];
    [moviePlayer readyPlayer];
    [self presentModalViewController:moviePlayer animated:YES];
    [moviePlayer release];         
}

答案 1 :(得分:0)

您必须在xcode项目中添加该视频

只需使用NSBundle获取它的路径并使用视频。

例如:

NSString *path=[[NSBundle mainBundle] pathForResource:@"yourmoviename" ofType:@"mpeg"];

moviePlayer = [[CustomMoviePlayerViewController alloc] initWithPath:path];
[moviePlayer readyPlayer];
[self presentModalViewController:moviePlayer animated:YES];

希望它能奏效。