我試圖從服務器下載mp4文件,然後將其保存在一個文件中。它保存在文件中後,我嘗試播放它。當程序運行時,它在文件下載時暫停,然後當我創建MPMoviePlayerController對象時屏幕變黑。 我做了以下。 1.追蹤代碼並查看NSDAta對象以確保正在加載正確的數據量。 2.添加一個if([mFile fileExistsAtPath:filename] == true)以確保文件存在。 3.檢查所有返回值爲零。 我現在不在意了!iphone試圖從一個文件播放視頻,但只能得到一個黑色的屏幕
//設置文件名以將其保存並播放它 NSFileManager * mFile = [NSFileManager defaultManager]; NSString * filename = [NSHomeDirectory()stringByAppendingPathComponent:@「ted10.dat」];
// load video
NSURL *movieUrl=[[NSURL alloc] initWithString:@"http://www.besttechsolutions.biz/projects/golfflix/ted.mp4"];
NSData *data = [NSData dataWithContentsOfURL:movieUrl];
[data writeToURL:movieUrl atomically:YES];
[mFile createFileAtPath:filename contents: data attributes:nil];
// makse sure file exist
if ([mFile fileExistsAtPath:filename]==true)
{
// play it
NSURL *fileUrl=[ NSURL fileURLWithPath:filename];
mp=[[MPMoviePlayerController alloc] initWithContentURL: fileUrl];
[mp.view setFrame: self.view.bounds];
[self.view addSubview: mp.view];
if ([mp respondsToSelector:@selector(loadState)])
{
// Set movie player layout
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setFullscreen:YES];
// May help to reduce latency
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
}//localhost/Dino/golflix2/Classes/videolist.h
else
{
}
[mp play];
}
嗨,謝謝你的幫助,我花了好幾天的時間試着讓它工作。 –