你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS視頻播放代碼

iOS視頻播放代碼

編輯:IOS開發綜合

/**
 @method 播放電影
 */ 
-(void)playMovie:(NSString *)fileName{ 
    //視頻文件路徑 
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp4"]; 
    //視頻URL 
    NSURL *url = [NSURL fileURLWithPath:path]; 
    //視頻播放對象 
    MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
    movie.controlStyle = MPMovieControlStyleFullscreen; 
    [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 -------------------視頻播放結束委托-------------------- 
 
/*
 @method 當視頻播放完畢釋放對象 
 */ 
-(void)myMovieFinishedCallback:(NSNotification*)notify 

    //視頻播放對象 
    MPMoviePlayerController* theMovie = [notify object]; 
    //銷毀播放通知 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:MPMoviePlayerPlaybackDidFinishNotification 
                                                  object:theMovie]; 
    [theMovie.view removeFromSuperview]; 
    // 釋放視頻對象 
    [theMovie release]; 

 


摘自 yhawaii的專欄

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved