2012-09-08 75 views
0

我已經做了一段代碼來播放視頻文件。但是,當我建立它我得到以下錯誤:終止叫做拋出異常

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[NSURL initFileURLWithPath:]: nil string parameter' First throw call stack: (0x1ed8022 0x11c0cd6 0x1e80a48 0x1e809b9 0xc6553b 0xc654c5 0x2be8 0x1ed9e99 0x32614e 0x3260e6 0x3ccade 0x3ccfa7 0x3cc266 0x34b3c0 0x34b5e6 0x331dc4 0x325634 0x17baef5 0x1eac195 0x1e10ff2 0x1e0f8da 0x1e0ed84 0x1e0ec9b 0x17b97d8 0x17b988a 0x323626 0x29b2 0x2925) terminate called throwing an exception

我已經嘗試了很多修復它,但沒有任何工作!你沒有解決方案嗎?是的,我已經實現了mediaplayer框架!

我已經編寫了follwing代碼:

-(IBAction)playvideo { 

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
             pathForResource:@"film" ofType:@"mp4"]]; 

    MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] 
                initWithContentURL:url]; 

    [self presentMoviePlayerViewControllerAnimated:playercontroller]; 

    playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile; 

    [playercontroller.moviePlayer play]; 

    [playercontroller release]; 

    playercontroller = nil; 

} 

回答

2

的問題是在這條線描述:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[NSURL initFileURLWithPath:]: nil string parameter'

這是告訴你,你傳遞一個nil到不參數允許一個nil

步驟,以確保這就是問題所在:

-(IBAction)playvideo { 
    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"film" ofType:@"mp4"] 

    if (!videoPath) { 
     NSLog(@"Video path is nil. My bundle must be set up incorrectly"); 
     return; // return early. 
    } 

    NSURL *url = [NSURL fileURLWithPath:videoPath]; 

    MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] 
                initWithContentURL:url]; 

    // The rest of your implementation here. 

} 

這至少應該幫助您本地化問題。

另外 - 您應該使用基於URL的方法獲取資源,而不是基於字符串的文件路徑。但每次只有一件事。

+0

我測試過了,我得到了NSLog!現在怎麼辦? –

+0

正如它所說 - 你沒有在你想要它的捆綁包中的文件。你是否在資源中添加文件?它在一個子目錄中嗎?它有完全相同的名字嗎? – Abizern

+0

我已經測試了一切,我認爲一切都正確 –