目标c - 尝试使用 MPMoviePlayer 控制器播放视频,只能获取音频



我想在我的iPhone应用程序中播放视频。我使用了以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *movieUrl = [NSURL fileURLWithPath:
                       [[NSBundle mainBundle] pathForResource:@"Myvideo" 
                                                       ofType:@"mp4"]];
    //create a new instance of MPMoviePlayerController
    MPMoviePlayerController* myMovie=[[MPMoviePlayerController alloc] 
                                      initWithContentURL:movieUrl];
    //disable scaling of our movie
    myMovie.scalingMode = MPMovieScalingModeNone;
    //don't show any controls
   // myMovie.movieControlMode = MPMovieControlModeHidden;
    //you can specify at which time the movie should 
    //start playing (default is 0.0)
    myMovie.initialPlaybackTime = 2.0;
    //register a callback method which will be called
    //after the movie finished
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(movieFinished:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:myMovie]; 
    //start the movie (asynchronous method)
    [myMovie play];
    // Do any additional setup after loading the view from its nib.
}

只有声音适用于此代码。有人可以帮我吗?

您需要为影片分配一个帧并将其添加到视图中。看这里。

因此,您可以添加:

[myMoview.view setFrame: self.view.bounds];  // player's frame must match parent's
[self.view addSubview:myMovie.view];

Apple Technical Q&A 1240:

MPMoviePlayer控制器播放电影音频,但不播放视频

问:我可以在 iOS 3.1.3 上使用 MPMoviePlayerController 成功播放电影。当我在iPad和装有iOS 4的iPhone上运行相同的代码时,我可以听到电影音频,但视频不再显示。这是怎么回事?

答:从 iPhone iOS 3.2 开始,调用 -play: 方法仍会开始播放影片,但不能确保影片可见。为了显示影片,必须从MPMoviePlayerController对象获取新的视图属性,并将该视图添加到视图层次结构中。下面是一个代码片段:

清单 1 如何将 MPMoviePlayerController 视图属性添加到视图层次结构中以播放电影。

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[[player view] setFrame:[myView bounds]]; // size to fit parent view exactly
[myView addSubview:[player view]];
[player play];

有关详细信息,请参阅有关使用媒体播放器框架的重要移植提示和 MPMoviePlayer 控制器类参考。

最新更新