2011-11-11 54 views
0

我試圖從服務器下載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]; 

} 

回答

0

這是你的固定代碼:

- (IBAction) doMovie: (id) sender 
{ 
    // set up file name to save it under and play it 
    NSFileManager *mFile= [NSFileManager defaultManager]; 
    NSString *filename=[ NSHomeDirectory() stringByAppendingPathComponent:@"ted10.mp4"]; 

    // load video 
    NSURL *movieUrl=[[NSURL alloc] initWithString:@"http://www.besttechsolutions.biz/projects/golfflix/ted.mp4"]; 
    NSError *error = NULL; 
    NSData *data = [NSData dataWithContentsOfURL:movieUrl]; 
    if([data writeToFile:filename options: NSDataWritingAtomic error: &error] != YES) 
    { 
     NSLog(@"error writing data to file %@", filename); 
    } else { 
     // make sure file exist 
     if ([mFile fileExistsAtPath:filename]==true) 
     { 
      // play it 
      NSURL *fileUrl=[ NSURL fileURLWithPath:filename]; 
      MPMoviePlayerController * mp=[[MPMoviePlayerController alloc] initWithContentURL: fileUrl]; 
      [mp.view setFrame: self.view.bounds]; 
      [self.view addSubview: mp.view]; 

      // Set movie player layout 
      [mp setControlStyle:MPMovieControlStyleFullscreen]; 
      [mp setFullscreen:YES]; 

      // May help to reduce latency 
      [mp prepareToPlay]; 

      [mp play]; 
     } 
    } 
} 

1)

最重要的是,你最初嘗試電影回遠程URL。我認爲你真正想做的是寫入本地文件(或緩存)。

2)

文件擴展名(.MP4)給出的提示,電影播放器​​正在播放什麼樣的文件。 「.dat」太泛泛。

3)

口語寫作本地文件的,不要把它在主目錄。找到緩存目錄或其他更合適的地方。

+0

嗨,謝謝你的幫助,我花了好幾天的時間試着讓它工作。 –

相關問題