9

iOS 5.1,代码如下:

 MPMoviePlayerController *player =
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]];
    [player prepareToPlay];
    [player.view setFrame: self.view.bounds];  // player's frame must match parent's
    [self.view addSubview: player.view];
    // ...
    [player play];

事实上,这和苹果参考说的一样,但是当我点击按钮使用这个功能时,只有一个黑色的矩形停留在那里,没有视频播放,没有声音发生,也没有暗恋,所以我想知道如何使这项工作

4

8 回答 8

17

很抱歉很晚才回复。解决方案有点奇怪。但它帮助我在面对同样的问题时继续前进。

MPMovieSourceTypeStreaming是你错过的东西。

MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds];  // player's frame must match parent's
player.movieSourceType    = MPMovieSourceTypeStreaming;
[self.view addSubview: player.view];
// ...
[player play];
于 2012-11-29T12:31:31.540 回答
4

您可能正在发送您的

[player play] 

消息太快了。

使用以下通知观察

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:player];

侦听 loadState 更改为可播放。要检查该更新,请执行以下操作:

- (void)playMovie:(NSNotification *)notification {
    MPMoviePlayerController *player = notification.object;
    if (player.loadState & MPMovieLoadStatePlayable)
    {
        NSLog(@"Movie is Ready to Play");
        [player play];
    }
}

电影准备好后应该开始播放。

于 2013-06-03T08:38:14.387 回答
1

基于@Dinesh 的回答,您的 URL 创建不正确。的价值

[NSURL URLWithString:@"map.mp4"]

将为零,因为 @"map.mp4" 不是有效的 URL。要从应用程序包中创建一个有效的 url,请按照 Dinesh 的说明进行操作。要创建一个远程 URL,您可以执行类似的操作

[NSURL URLWithString:@"http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"]

URL 应该包含它的所有部分:协议、主机、路径、文件。

干杯,费利佩

于 2012-05-03T07:53:02.943 回答
1

试试下面的代码从你的项目资源中播放视频:

  NSBundle *bundle = [NSBundle mainBundle];
  NSString *moviePath = [bundle pathForResource:@"map" ofType:@"mp4"];
  NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];

  MPMoviePlayerController *player =
  [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
  [player prepareToPlay];
  [player.view setFrame: self.view.bounds];  // player's frame must match parent's
  [self.view addSubview: player.view];
    // ...
  [player play];

谢谢..!

于 2012-05-03T07:46:38.517 回答
1

在主窗口上添加您的电影播放器​​控制器视图,例如:

 MPMoviePlayerController *player =
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]];
    [player prepareToPlay];
    [player.view setFrame: self.view.bounds];

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview: player.view];

[player play];

希望它会起作用!

于 2013-10-21T11:51:25.413 回答
0

请使用 MPMoviePlayerViewController 因为 MP4 文件。当你使用 MOV 然后工作完美!

MPMoviePlayerViewController *moviePlayerViewController;

-(void)PlayVideoController:(NSString*)videoUrl1 { 
    NSURL *fileURL = [NSURL URLWithString:videoUrl1]; 
    moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];

    // Register for the playback finished notification.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];

    //Present
    [self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];

    // Play the movie!
    moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    [moviePlayerViewController.moviePlayer play];
}

-(void)myMovieFinishedCallback:(NSNotification*)aNotification {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
    [moviePlayerViewController release], moviePlayerViewController = nil;
}
于 2013-05-31T11:23:31.053 回答
0

您的播放器应该是您的视图控制器的属性,如下所示:

。H:

@property(nonatomic,weak)MPMoviePlayerController *moviePlayer;

米:

    MPMoviePlayerController *player =
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]];
    [player prepareToPlay];
    [player.view setFrame: self.view.bounds];  // player's frame must match parent's
    //set the player to your property
    self.moviePlayer=player;

    [self.view addSubview: self.moviePlayer.view];
    // ...
    [self.moviePlayer play];
于 2013-01-03T16:17:46.253 回答
0

您可能需要交叉检查视频网址中的空格。以下代码对我有用。

- (IBAction)btnPlayVideo:(id)sender {

    self.strVideoUrl = [self.strVideoUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL    *fileURL    =   [NSURL URLWithString:[NSString stringWithFormat:@"%@", self.strVideoUrl]];
    NSLog(@"Url to play = %@", self.strVideoUrl);

        self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(moviePlaybackComplete:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:self.moviePlayerController];
        [self.moviePlayerController.view setFrame:self.view.bounds];
        [self.view addSubview:self.moviePlayerController.view];
        self.moviePlayerController.shouldAutoplay = YES;
        self.moviePlayerController.fullscreen = YES;
        self.moviePlayerController.controlStyle=NO;
        [self.moviePlayerController prepareToPlay];
        self.moviePlayerController.movieSourceType = MPMovieSourceTypeStreaming;

        [self.moviePlayerController play];

}

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

    [self.moviePlayerController.view removeFromSuperview];
    self.closeVideAdProp.hidden = NO;
    btnCloseAd.hidden=FALSE;
}
于 2015-12-18T11:11:36.427 回答