1

我在 30 秒内成功停止了视频。但我无法关闭 MP MovieViewController,我想停止缓冲活动。我用过这段代码......

视频播放代码:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Pungi" ofType:@"mp4"]];

self.movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.movie .controlStyle = MPMovieControlStyleEmbedded;
[  self.movie  play];
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:YES];
self.movie.initialPlaybackTime = 0.5;
[NSTimer scheduledTimerWithTimeInterval:15.0
                                 target:self
                               selector:@selector(stopVideo)
                               userInfo:nil
                                repeats:NO];

停止视频:

[self.movie stop];
[self.movie.view removeFromSuperview];
[self.movie release];
4

5 回答 5

5

把你MPMoviePlayerController放在一个单独的类中并加载它:

MoviePlayerViewController.h

#import <MediaPlayer/MediaPlayer.h> 

@interface MoviePlayerViewController : UIViewController

@end

MoviePlayerViewController.m

#import "MoviePlayerViewController.h"

MPMoviePlayerViewController *movieController;

@interface MoviePlayerViewController ()

@end

@implementation MoviePlayerViewController



- (void)willEnterFullscreen:(NSNotification*)notification {
    NSLog(@"willEnterFullscreen");
}

- (void)enteredFullscreen:(NSNotification*)notification {
    NSLog(@"enteredFullscreen");
}

- (void)willExitFullscreen:(NSNotification*)notification {
    NSLog(@"willExitFullscreen");
}

- (void)exitedFullscreen:(NSNotification*)notification {
    NSLog(@"exitedFullscreen");

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)playbackFinished:(NSNotification*)notification {
    NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    switch ([reason intValue]) {
        case MPMovieFinishReasonPlaybackEnded:
            [self dismissModalViewControllerAnimated:YES];
            break;
        case MPMovieFinishReasonPlaybackError:
            break;
        case MPMovieFinishReasonUserExited:
            [self dismissModalViewControllerAnimated:YES];
            break;
        default:
            break;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];



    NSString *videoName = @"Videoname";

    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:videoName @"movietype"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];


    [movieController.view setFrame:CGRectMake(0, -20, 320, 480)];
    [self.view addSubview:movieController.view];

    [movieController.moviePlayer play];

}

视图控制器.m

    MoviePlayerViewController *player = [[MoviePlayerViewController alloc] initWithNibName:nil bundle:nil];
 [self presentModalViewController:player animated:YES];

它能做什么:

MoviePlayerViewController是一个自定义类,它加载MPMoviePlayerController一个视频。在viewDidLoad方法中(或任何你想要的地方)你加载MoviePlayerViewController. ( [self presentModalViewController:animated]).... 这样做的好处是,您的主类不会因moviecrap/定义而重载,并且您可以MoviePlayerViewController在完成时轻松关闭它,使用通知来检查它是否已停止。如果它已停止:

[self dismissModalViewControllerAnimated:YES];

希望这可以帮助!

于 2012-10-06T11:48:03.443 回答
2

如下面的代码所示,您应该在实际停止之前暂停并将 initialPlaybackTime 设置为 -1。这是 MPMoviePlayerController 提供的棘手的事情之一。

    [_moviePlayerController pause];

    if ([_moviePlayerController isKindOfClass:[MPMoviePlayerController class]]) {
        ((MPMoviePlayerController*)_moviePlayerController).initialPlaybackTime = -1;
    }

    [_moviePlayerController stop];
    if ([_moviePlayerController isKindOfClass:[MPMoviePlayerController class]]) {
        ((MPMoviePlayerController*)_moviePlayerController).initialPlaybackTime = -1;
    }
    [_moviePlayerController.view removeFromSuperview];
于 2012-10-06T09:12:56.867 回答
1

您需要在 viewDidLoad 中添加通知观察者:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinishNotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

然后添加方法:

 - (void)moviePlayerPlaybackDidFinishNotification:(NSNotification*)notification 
 {
    [self dismissMoviePlayerViewControllerAnimated];
 }
于 2014-10-16T06:56:46.503 回答
0

尝试这个.......

self.movie.initialPlaybackTime = -1; 
[self.movie stop]; 
[self.movie release]; 
于 2012-10-06T08:27:10.230 回答
0

在您的标头中导入一些标头:

 #import <MediaPlayer/MediaPlayer.h>
 #import <MediaPlayer/MPMoviePlayerViewController.h>

您可能还需要平衡您的“presentMoviePlayer”呼叫与某处的解雇:

[self dismissMoviePlayerViewControllerAnimated];

如果您提前完成了资源,您可以通过使用 NotificationManager 来监视 MPMoviePlayerPlaybackDidFinishNotification 来更快地释放它。

并且

- (void)dealloc {
   [movie release], 
   movie = nil;
   [super dealloc];
}
于 2012-10-06T08:28:00.573 回答