iOS 視頻播放代碼

jopen 12年前發布 | 48K 次閱讀 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]; }</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!