视频无法在Xcode中播放; NIL字符串参数

时间:2012-09-24 00:52:34

标签: iphone video xcode4.3

这是我为在Xcode项目中播放视频而编写的代码。当我运行该项目时,它崩溃了我。我明白这个原因:因未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' * - [NSURL initFileURLWithPath:]:nil字符串参数'

我在模拟器上运行它时发现了一个可行的教程。 我将我的视频添加到项目中并相应地更新了代码。我的视频采用m4v格式,就像教程中的其他视频一样。当我用我的视频运行应用程序时它仍然崩溃并给我同样的错误。我从快速拍摄视频并将其导出到iTunes。我做错了什么?

.h文件

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>  

@interface BigBuckBunnyViewController : UIViewController {

}

-(IBAction)playMovie:(id)sender;

@end

.m文件

#import "BigBuckBunnyViewController.h"

@implementation BigBuckBunnyViewController

-(IBAction)playMovie:(id)sender
{
UIButton *playButton = (UIButton *) sender; 

NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"ladder rack 1 4"  
ofType:@"m4v"];
NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] 
initWithContentURL:fileURL];

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(moviePlaybackComplete:)

name:MPMoviePlayerPlaybackDidFinishNotification

object:moviePlayerController];

[moviePlayerController.view setFrame:CGRectMake(playButton.frame.origin.x, 

playButton.frame.origin.y, 

playButton.frame.size.width, 

playButton.frame.size.height)];

[self.view addSubview:moviePlayerController.view];
//moviePlayerController.fullscreen = YES;

//moviePlayerController.scalingMode = MPMovieScalingModeFill;

[moviePlayerController play];
}

- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self

name:MPMoviePlayerPlaybackDidFinishNotification

object:moviePlayerController];

[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}

- (void)dealloc {
[super dealloc];
}

@end

好的,所以我能够创建一个html页面并将html页面链接到视图中的.m文件。我创建了一个UIWebview并将其添加到我的xib视图中。我将视频放在服务器上。这可能是一个更好的解决方案,减少了加载时间。

2 个答案:

答案 0 :(得分:0)

这将有效

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface TipsViewController : UIViewController {

  MPMoviePlayerViewController *playerController;
}

-(IBAction)playVideo;

@end

.m文件

 - (IBAction)playVideo{

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

 if(url != nil){

           playerController = [[MPMoviePlayerViewController alloc] 
    initWithContentURL:url];
           [self presentMoviePlayerViewControllerAnimated:playerController];

           playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;

          [playerController.moviePlayer play]; 
 }
 else{

      NSLog(@"URL not found");

    }

 }

如果url为null,请检查您的文件和路径。

注意:内存管理内容是否拥有:)

答案 1 :(得分:0)

您的结果为零,因为NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ladder rack 1 4" ofType:@"m4v"];包含空格。 所以你需要改变它:

NSURL   *fileURL    =   [NSURL fileURLWithPath:filepath] absoluteString];

请参阅此link