iOS視頻播放代碼iPhone軟體開發教學課程

/**
 @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的專欄

You May Also Like