0

我正在使用图像匹配器 sdkMPMoviePlayerController来在匹配后播放视频。

NSLog视频中是过程但不是播放。

这里 .m 文件`

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize moviePlayer;
- (void)viewDidLoad
{
    [super viewDidLoad];
   _cvView = [[ImageMatcher alloc] initWithAppKey:@"apikey" useDefaultCamera:TRUE];
    _cvView.matcherDelegate = self;
    [_cvView start];
    [_cvView setEnableMedianFilter:YES];
    [_cvView setImagePoolMinimumRating:0];
    [_cvView setMatchMode:matcher_mode_All];
    bool value = [_cvView addImage:[UIImage imageNamed:@"pic2.jpeg"] withUniqeID:[NSNumber numberWithInt:20]];   
}
-(void) viewDidAppear:(BOOL)animated
{
   [self presentViewController:_cvView animated:YES completion:nil];
}
-(void) imageRecognitionResult:(int)uId
{
    if (uId == 20) {
        [self startPlayingVideo2:nil];
        NSLog(@"ID = %d",uId);
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
-(void) startPlayingVideo1:(id)paramSender{
    NSLog(@"function startPlayingVideo make");
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *urlAsString = [mainBundle pathForResource:@"test1" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:urlAsString];
    if(self.moviePlayer != nil){
        NSLog(@"if 1 make");
        [self stopPlayingVideo:nil];
    }
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    if(self.moviePlayer != nil){
        NSLog(@"if 2 make");
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(videoHasFinishedPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
        moviePlayer.view.frame = CGRectMake(0, 0, 1024, 760);
        [self.view addSubview:self.moviePlayer.view];
        [self.moviePlayer setFullscreen:YES animated:YES];
        [self.moviePlayer play];
    }else{
        NSLog(@"fail");
    }
}
-(void) startPlayingVideo2:(id)paramSender{
    NSLog(@"function startPlayingVideo make");
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *urlAsString = [mainBundle pathForResource:@"sample_mpeg4" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:urlAsString];
    if(self.moviePlayer != nil){
        NSLog(@"if 1 make");
        [self stopPlayingVideo:nil];
    }
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    if(self.moviePlayer != nil){
        NSLog(@"if 2 make");
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(videoHasFinishedPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
        moviePlayer.view.frame = CGRectMake(0, 0, 1024, 760);
        [self.view addSubview:self.moviePlayer.view];
        [self.moviePlayer setFullscreen:YES animated:YES];
        [self.moviePlayer play];
    }else{
        NSLog(@"fail");
    }
}
-(void) stopPlayingVideo:(id)paramSender{
    if (self.moviePlayer != nil) {
        NSLog(@"stopPlayingVideo on");
        [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
        [self.moviePlayer stop];
        if([self.moviePlayer.view.superview isEqual:self.view]){
            [self.moviePlayer.view removeFromSuperview];
        }
    }
}
-(void) videoHasFinishedPlaying:(NSNotification *)paramNotification{
    NSNumber *reason = [paramNotification.userInfo valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    if (reason != nil) {
        NSInteger reasonAsInteger = [reason integerValue];
        switch (reasonAsInteger){
            case MPMovieFinishReasonPlaybackEnded:{
                NSLog(@"MPMovieFinishReasonPlaybackEnded");
                break;
            }
            case MPMovieFinishReasonPlaybackError:{
                NSLog(@"MPMovieFinishReasonPlaybackError");
                break;
            }
            case MPMovieFinishReasonUserExited:{
                NSLog(@"MPMovieFinishReasonUserExited");
                break;
            }
        }
        NSLog(@"finish reason = %ld", (long)reasonAsInteger);
         [self stopPlayingVideo:nil];
    }
}
@end
`
4

1 回答 1

2

添加prepareToPlay您的代码(准备播放电影播放器​​。(必需))'

[self.moviePlayer prepareToPlay];
[self.moviePlayer play];

要为播放准备新的电影播放器​​,请调用 MPMediaPlayback 协议参考中描述的 prepareToPlay 方法。

于 2013-04-19T12:09:33.117 回答