简体   繁体   English

MPMoviePlayerViewController无法播放视频或显示控件

[英]MPMoviePlayerViewController not playing video or displaying controls

Here's my code that should be playing a video embedded in a subview, but it only displays a still image with no controls. 这是我的代码,应该播放嵌入子视图中的视频,但是它只显示没有控件的静止图像。

- (void)displayVideo:(NSURL *)videoURL
{
    if (self.mediaPlayer) {
        [self.mediaPlayer.view removeFromSuperview];
        self.mediaPlayer = nil;
    }

    self.mediaPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
    [self.mediaPlayer.moviePlayer prepareToPlay];
    self.mediaPlayer.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
    self.mediaPlayer.view.frame = CGRectMake(0, 0, self.mediaView.bounds.size.width, self.mediaView.bounds.size.height);
    [self.mediaView addSubview:self.mediaPlayer.view];
    [self.mediaPlayer.moviePlayer play];
}

I also tried to load the media player directly where mediaPlayer is MPMoviePlayerController instead of MPMoviePlayerViewController, but I get even less with only a black view. 我还尝试直接在mediaPlayer是MPMoviePlayerController而不是MPMoviePlayerViewController的情况下直接加载媒体播放器,但是仅黑屏显示的效果就更少了。

    self.mediaPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
    [self.mediaPlayer prepareToPlay];
    self.mediaPlayer.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
    self.mediaPlayer.view.frame = CGRectMake(0, 0, self.mediaView.bounds.size.width, self.mediaView.bounds.size.height);
    [self.mediaView addSubview:self.mediaPlayer.view];
    [self.mediaPlayer play];

Thanks for any help. 谢谢你的帮助。

The first code is completely wrong. 第一个代码是完全错误的。 The only way to use MPMoviePlayerViewController is as a presented view controller ( presentViewController:... ); 使用MPMoviePlayerViewController的唯一方法是作为一个呈现的视图控制器( presentViewController:... ); you must not grab its view and try to shove it into your own interface. 您一定不要抓住它的视图,并尝试将其推入自己的界面。

The second one stands a much better chance. 第二个机会更好。 So here are some things to think about: 因此,这里有一些事情要考虑:

  • Is videoURL valid? videoURL有效吗? How do you know? 你怎么知道的? No, seriously. 不,认真 And think about the format, too, since not every video format is playable under iOS. 还要考虑格式,因为并非每种视频格式都可以在iOS下播放。

  • Is self.mediaPlayer retaining the movie player controller? self.mediaPlayer是否保留电影播放器​​控制器? Again, look carefully; 再次仔细看; that's crucial. 这很关键。 It must have a strong or retain policy. 它必须有strong政策或retain政策。

  • Do you have any other media player controller views in your interface? 界面中是否还有其他媒体播放器控制器视图? I notice that in the second code you forgot to remove the previous one. 我注意到在第二个代码中,您忘记删除前一个代码。 This is crucial! 这很关键! There can be only one such view. 这样的观点只能有一种。

(By the way, there is no need to ask for MPMovieControlStyleEmbedded ; it is the default in this configuration.) (顺便说一句,无需要求MPMovieControlStyleEmbedded ;这是此配置中的默认设置。)

Finally, it might help to compare with working code. 最后,与工作代码进行比较可能会有所帮助。 The code in my book does work: 我的书中的代码确实有效:

http://www.apeth.com/iOSBook/ch28.html#_mpmovieplayercontroller http://www.apeth.com/iOSBook/ch28.html#_mpmovieplayercontroller

NSURL* m = [[NSBundle mainBundle] URLForResource:@"ElMirage"
                                   withExtension:@"mp4"];
MPMoviePlayerController* mp =
    [[MPMoviePlayerController alloc] initWithContentURL:m];
self.mpc = mp; // retain policy
self.mpc.shouldAutoplay = NO;
[self.mpc prepareToPlay];
self.mpc.view.frame = CGRectMake(10, 10, 300, 250);
self.mpc.backgroundView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.mpc.view];

And you can prove that by downloading this example: 您可以通过下载以下示例来证明这一点:

https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/ch28p786moviePlayer https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/ch28p786moviePlayer

First check your videoURL valid? 首先检查您的videoURL是否有效? The video technologies in iOS support the playback of movie files with the .mov, .mp4, .m4v, and .3gp filename extensions and using the following compression standards: iOS中的视频技术支持使用.mov,.mp4,.m4v和.3gp文件扩展名并使用以下压缩标准来播放电影文件:

1) H.264 video, up to 1.5 Mbps, 640 by 480 pixels, 30 frames per second, Low-Complexity version of the H.264 Baseline Profile with AAC-LC audio up to 160 Kbps, 48 kHz, stereo audio in .m4v, .mp4, and .mov file formats 1)H.264视频,最高1.5 Mbps,640 x 480像素,每秒30帧,低复杂度版本的H.264 Baseline Profile,具有AAC-LC音频,最高160 Kbps,48 kHz,立体声音频。 m4v,.mp4和.mov文件格式

2) H.264 video, up to 768 Kbps, 320 by 240 pixels, 30 frames per second, Baseline Profile up to Level 1.3 with AAC-LC audio up to 160 Kbps, 48 kHz, stereo audio in .m4v, .mp4, and .mov file formats 2)H.264视频,最高768 Kbps,320 x 240像素,每秒30帧,基准配置文件最高1.3级,AAC-LC音频最高160 Kbps,48 kHz,.m4v,.mp4立体声音频,和.mov文件格式

3) MPEG-4 video, up to 2.5 Mbps, 640 by 480 pixels, 30 frames per second, Simple Profile with AAC-LC audio up to 160 Kbps, 48 kHz, stereo audio in .m4v, .mp4, and .mov file formats 3)MPEG-4视频,最高2.5 Mbps,640 x 480像素,每秒30帧,具有AAC-LC音频(最高160 Kbps,48 kHz)的简单配置文件,.m4v,.mp4和.mov文件的立体声音频格式

 -(void) viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];

     player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:strSelectedVideoUrl]];
     player.scalingMode = MPMovieScalingModeAspectFit;
     player.movieSourceType = MPMovieSourceTypeFile;
       player.view.frame = CGRectMake(0, 45, 320, 400);
       player.shouldAutoplay = YES;
     [player prepareToPlay];
     [self.view addSubview:player.view];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];

     [player play];
 }
 - (void) movieFinishedCallback:(NSNotification*) aNotification {
     MPMoviePlayerController *player1 = [aNotification object];
     [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player1];
     [player stop];
     [player1.view removeFromSuperview];
     //[player1 release];
     player1 = nil;
     [self.navigationController popViewControllerAnimated:YES];
 }

Try this, it's working perfectly for me 试试这个,对我来说很完美

NSURL *movieURL = [NSURL URLWithString:@"http://........"];

// Initialize a movie player object with the specified URL

 self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

self.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;


    [self.moviePlayer.view setFrame:self.view.bounds];

    [self.view addSubview:self.moviePlayer.view];

    [self.moviePlayer play];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM