[기본] MPMoviePlayerViewController를 통한 MP4 재생

2394 단어
먼저 시스템 라이브러리 MediaPlayer를 가져와야 합니다.framework에 헤더 파일을 포함하려면 다음과 같이 하십시오.
#import <MediaPlayer/MediaPlayer.h>

코드는 다음과 같습니다.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad {  
    [super viewDidLoad];  
    NSString *url = [[NSBundle mainBundle] pathForResource:@"TaylorSwift-LoveStory" ofType:@"mp4"];  

    MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:)  
                                                 name:MPMoviePlayerPlaybackDidFinishNotification  
                                               object:[playerViewController moviePlayer]];  
    //-- add to view--- 
    [self.view addSubview:playerViewController.view];  

    //---play movie--- 
    MPMoviePlayerController *player = [playerViewController moviePlayer];  
    player.controlStyle = MPMovieControlStyleNone;
    [player play];  

}  

- (void) movieFinishedCallback:(NSNotification*) aNotification {  
    MPMoviePlayerController *player = [aNotification object];  
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];  
    [player stop];  
    [self.view removeFromSuperView];  
    [player autorelease];  
}  

좋은 웹페이지 즐겨찾기