2011-11-16 101 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];   
}