开发者

Trying to play video using MPMoviePlayerController, only get audio

开发者 https://www.devze.com 2023-03-07 18:41 出处:网络
I want to play a video in my iPhone app. I used this code: - (void)viewDidLoad { [super viewDidLoad]; NSURL *movieUrl = [NSURL fileURLWithPath:

I want to play a video in my iPhone app. I used this code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *movieUrl = [NSURL fileURLWithPath:
                       [[NSBundle mainBundle] pathForResource:@"Myvideo" 
                                                       ofType:@"mp4"]];

    //create a new instance of MPMoviePlayerController
    MPMoviePlayerController* myMovie=[[MPMoviePlayerController allo开发者_JS百科c] 
                                      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.
}

Only the sound works with this code. Can someone help me please?


You need to allocate a frame to your movie and add it to the view. See here.

So you can add:

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


Apple Technical Q&A 1240:

MPMoviePlayerController plays movie audio but not video

Q: I'm able to successfully play movies using MPMoviePlayerController on iOS 3.1.3. When I run this same code on the iPad and on the iPhone with iOS 4 I can hear the movie audio, but the video is no longer displayed. What's going on?

A: Starting with iPhone iOS 3.2, calling the -play: method still starts playback of the movie but it does not ensure that the movie is visible. In order to display a movie, you must get the new view property from your MPMoviePlayerController object and add that view to your view hierarchy. Here's a code snippet:

Listing 1 How to add the MPMoviePlayerController view property to your view hierarchy to play a movie.

#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];

See Important Porting Tip for Using the Media Player Framework and the MPMoviePlayerController Class Reference for more information.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号