2011-11-16 103 views
1

我已经在这条道路从下载服务器端的视频在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]; 

当我把资源文件夹都相同的视频作品perfect.So我认为这个问题是与path.But有什么问题呢?

回答

0

你要添加的视频在您的Xcode项目

,只需使用一个NSBundle来得到它的路径,并使用视频。

例如:

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

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

希望它会工作。

+0

,我怎么可以添加从外部文件夹到我的项目? – adrian

+0

顺便说一句,上面的代码不是新的....我说这已经起作用了! – adrian

+0

我不认为你可以用这种方式使用该文件,你必须将它添加到你的Xcode项目。 –

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