Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从本地主机播放视频_Iphone_Video - Fatal编程技术网

Iphone 从本地主机播放视频

Iphone 从本地主机播放视频,iphone,video,Iphone,Video,我从localhost的服务器端下载了一段视频,路径如下: /Users/adrian/Library/Application Support/iPhone Simulator/4.3.2/Applications/60584763-39F6-4124-805D-BEFDE453206D/Documents/videos/bar.mpeg 事实上,视频就存在于这条路径上 但当我试着播放它时,屏幕变黑,没有播放视频 我是这样做的: moviePlayer = [[CustomMoviePlay

我从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];

当我把同一个视频放在资源文件夹中时,一切都很好。所以我假设问题出在路径上。那么问题出在哪里呢?

你必须将该视频添加到你的xcode项目中

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

例:


希望它能起作用。

您必须将该视频添加到您的xcode项目中

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

例:

希望它能起作用。

解决方案:

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];         
}
解决方案:

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];         
}

我怎样才能将它从外部文件夹添加到我的项目中?我不认为你可以这样使用该文件,你必须将它添加到你的xcode项目中。但我对图像做了同样的事情,但它不适用于视频:)我怎样才能将它从外部文件夹添加到我的项目中?我不认为你可以这样使用该文件,你必须将它添加到你的xcode项目中。但我对图像做了同样的操作,但它不适用于视频:)