IOS 播放視頻 MPMoviePlayerController


在unity游戲的開頭播放視頻 , 根據需求 , 最后決定用 MPMoviePlayerController 來實現播放, 實現如下: by Tin

 

需要在AppController.mm的 OpenEAGL_UnityCallback  修改下view的大小

    UIView *mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    // mainView.backgroundColor = [UIColor grayColor];
    
    [MyViewController Instance].view = mainView;
    
    
    [UnityGetGLViewController().view addSubview: [MyViewController Instance].view];

 

需要在游戲中接收unity的命令

// ========================   播放開頭動畫  start ========================
// by:xihao
// 2014-05-16

void PlayMovieInIOS( char * path )
{    
    [[MyViewController Instance] PlayVideo:[NSString stringWithUTF8String:path]];
    
}


void exPlayVideo( char * url )
{
    [[MyViewController Instance] PlayVideo:[NSString stringWithUTF8String:url]];
}


void exReleaseVideo()
{
    [[MyViewController Instance] ReleaseVideo];
}


MovieViewController *  mv ;

-(void) PlayVideo:(NSString *)  path
{
    if ( mv != nil) {
        [mv breakMovie] ;
        [mv release];
        mv= nil ;
    }
    
    mv = [[ MovieViewController alloc] init];
    [self.view addSubview:mv.view];
    [mv playMovie:path];
}


-(void) ReleaseVideo
{
    if ( mv != nil) {
        [mv breakMovie] ;
        [mv release];
        mv= nil ;
    }
    
    UnitySendMessage("_IOSDoor","ReleaseVideoOver", "");
}


// ========================   播放開頭動畫  end ========================

 

接下來是播放視頻

MPMoviePlayerController *movie ;

/**
 @method 播放電影
 */
-(void)playMovie:(NSString *)fileName{
    
    
    NSURL *url = [NSURL fileURLWithPath: fileName ];
    //視頻播放對象 
    movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
    movie.controlStyle = MPMovieControlStyleNone;
    [movie.view setFrame:self.view.bounds];
    movie.initialPlaybackTime = -1;
    [self.view addSubview:movie.view];
    // 注冊一個播放結束的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(myMovieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:movie];
 
    [movie play];
}

#pragma mark -------------------視頻播放結束委托--------------------

-(void)  breakMovie
{
    if (movie == nil) {
        return ;
    }
    
    //銷毀播放通知
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:movie];
    [movie.view removeFromSuperview];
    // 釋放視頻對象
    [movie release];
    movie = nil ;
}


/*
 @method 當視頻播放完畢釋放對象
 */
-(void)myMovieFinishedCallback:(NSNotification*)notify
{
    
    NSNumber *reason =
    [notify.userInfo
     valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    if (reason != nil){
        NSInteger reasonAsInteger = [reason integerValue];
        switch (reasonAsInteger){
            case MPMovieFinishReasonPlaybackEnded:{
                /* The movie ended normally */
                break; }
            case MPMovieFinishReasonPlaybackError:{
                /* An error happened and the movie ended */
                break;
            }
            case MPMovieFinishReasonUserExited:{
                /* The user exited the player */
                break;
            }
        }
        NSLog(@"Finish Reason = %ld", (long)reasonAsInteger);
    }
    
    /* 取消視頻自動銷毀  由break mv 執行
    //視頻播放對象
    MPMoviePlayerController* theMovie = [notify object];
    //銷毀播放通知
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:theMovie];
    [theMovie.view removeFromSuperview];
    // 釋放視頻對象
    [theMovie release];
    movie = nil ;
    NSLog(@"---------PlayVideoOver 11");
     */
    
    UnitySendMessage("_IOSDoor","PlayVideoOver", "");
    NSLog(@"---------PlayVideoOver 22");
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM