目标C iPhone使用媒体播放器播放视频不起作用



在iPhone应用程序上,我正在尝试使用媒体播放器播放视频,但它不起作用。我使用主捆绑包来存储视频。我没有错误,但是当我单击以启动视频时,我有一个空白屏幕。我的道路似乎是正确的。我不明白为什么它不起作用。感谢您的帮助。这是我的代码:

#import "VideoViewController.h"
@interface VideoViewController ()
@end
@implementation VideoViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"myvideo" ofType:@"mp4"];
    NSURL *url = [NSURL URLWithString: moviePath];
    NSLog(@"%@", url);
    self.moviePlayer =  [[MPMoviePlayerController alloc] initWithContentURL:url];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.moviePlayer];
    self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
    self.moviePlayer.shouldAutoplay = YES;
    [self.view addSubview:self.moviePlayer.view];
    [self.moviePlayer setFullscreen:YES animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:player];
    if ([player
         respondsToSelector:@selector(setFullscreen:animated:)])
    {
        [player.view removeFromSuperview];
    }
}
@end
好的,

我找到了解决方案!希望这对其他人也有帮助;)

#import "VideoViewController.h"
@interface VideoViewController ()
@end
@implementation VideoViewController
{
    BOOL didPlay;
}
- (void)playMovie
{
    didPlay = YES;
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"myvideo" ofType:@"mp4"];
    NSURL *mediaURL = [NSURL fileURLWithPath:moviePath];
    // play
    MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:mediaURL];
    player.moviePlayer.allowsAirPlay = YES;
    player.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
    [self presentMoviePlayerViewControllerAnimated:player];
    id observer1;
    id observer2;
    observer1 = [[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerPlaybackDidFinishNotification object:player.moviePlayer queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification)
     {
         [[NSNotificationCenter defaultCenter] removeObserver:observer1];
         [[NSNotificationCenter defaultCenter] removeObserver:observer2];
     }];
    observer2 = [[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerLoadStateDidChangeNotification object:player.moviePlayer queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification)
     {
         if ((player.moviePlayer.loadState & MPMovieLoadStatePlayable) != 0)
             [player.moviePlayer performSelector:@selector(play) withObject:nil afterDelay:1.0f];
     }];
}
- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (!didPlay) [self playMovie];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    didPlay = NO;
    self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
    [self.view addSubview:self.moviePlayer.view];
}

- (IBAction)goBack:(id)sender {
    [self performSegueWithIdentifier: @"back" sender: self];
    NSLog(@"ok");
}
@end

最新更新