MPMoviePlayerController不播放本地视频

时间:2014-02-04 10:15:48

标签: ios video mpmovieplayercontroller

我的视频没有开始在简单的项目中播放 所以我尝试了一些例子来加入项目。 首先,我添加了两个框架MediaPlayer和MobileCoreService以及导入的.h文件

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

然后添加了属性

@property (copy,   nonatomic) NSURL *movieURL;
@property (strong, nonatomic) MPMoviePlayerController *player;

最后一步是添加到viewDidLoad下一个代码

_movieURL = [NSURL URLWithString:@"Movie.m4v"];
_player = [[MPMoviePlayerController alloc] initWithContentURL:_movieURL];
_player.view.frame = CGRectMake(0 , 0, 400, 300);
_player.controlStyle=MPMovieControlStyleNone;
_player.scalingMode = MPMovieScalingModeAspectFill;
[self.view addSubview:_player.view];
[_player play];

应用程序根文件夹中的Movie.m4v文件。

此外,我正在尝试使用操作按钮启动它,但代码正在使用

[_player play];  

但我得到一个黑色矩形,如果我理解正确,我的addSubview部分正在工作 搜索另一个例子,但几乎找到相同的实现。 带有src代码和文件的http://photokarma.bl.ee/ios/VideoTest.zip存档

3 个答案:

答案 0 :(得分:0)

请在您的代码中解析此行。

_movieURL = [NSURL URLWithString:@"Movie.m4v"];

如果这是一个http网址,那么写完就像http://www.yourserver.com/../../Movie.m4v 或者您在iOS应用程序中从本地访问此视频,然后提供完整的路径,如

NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];

然后在找到电影网址后,你可以init播放你的播放器

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
player.view.frame = CGRectMake(184, 200, 400, 300);
[self.view addSubview:player.view];
[player play];

根据您的评论do you have any idea why i get urlpath nil?

检查:

电影文件存在于您的源代码副本中。还要检查Xcode项目是否有对该文件的引用(如果它是红色,则表示该文件实际上不存在)

在Xcode的目标中,查看“Build Phases”并显示“Copy Bundle Resources”构建阶段。该文件应该存在。如果不是,请按+按钮并选择它。

根据您给出的代码我解决了并发现了这个错误,我也讨论过。 勾选两个

  • 添加目标您的项目名称
  • 将项目复制到目标群组

所以你需要删除视频文件并再次添加它们并勾选。

enter image description here

答案 1 :(得分:0)

尝试此代码..

NSString *path = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v" inDirectory:nil];
NSURL *movieURL = [NSURL fileURLWithPath:path];
player= [[ MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
player.view.frame = CGRectMake(184, 200, 400, 300);
[self.view addSubview:player.view];
[player play];

源代码:http://ishare-edu.blogspot.in/2012/10/how-to-play-audio-and-video-in-iphone.html

希望它有效......

答案 2 :(得分:0)

尝试简单步骤:

第1步:导入MediaPlayer框架#import <MediaPlayer/MediaPlayer.h>

第2步:.hUIViewController<MPMediaPlayback>

中设置委托

第3步:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *fileLocation= [bundle pathForResource:@"file_video" ofType:@"m4v"];
    NSURL *vedioURL =[NSURL fileURLWithPath:fileLocation];
    MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
    [self presentMoviePlayerViewControllerAnimated:videoPlayerView];
    [videoPlayerView.moviePlayer play];
}

-(void)endSeeking
{
    [self.navigationController popViewControllerAnimated:NO];
}

-(void)stop
{
    [self.navigationController popViewControllerAnimated:NO];
}
相关问题