0

这是我为在我的 Xcode 项目中播放视频而编写的代码。当我运行该项目时,它会在我身上崩溃。我得到这个原因:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“ * -[NSURL initFileURLWithPath:]: nil string parameter”

我找到了一个在模拟器上运行时可以使用的教程。我将我的视频添加到项目中并相应地更新了代码。我的视频是 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 视图中。我将视频放在服务器上。这可能是一个更好的解决方案,更少的加载时间。

4

2 回答 2

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 为空,请检查您的文件和路径。

注意:做内存管理自己的东西:)

于 2012-09-24T06:08:38.800 回答
0

你得到 nil 因为NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ladder rack 1 4" ofType:@"m4v"];包含空格。所以你需要改变它:

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

参考这个链接

于 2012-09-26T19:10:19.247 回答