/* * Copyright (C) 2013-2015 Bilibili * Copyright (C) 2013-2015 Zhang Rui * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #import "IJKMoviePlayerViewController.h" #import "IJKMediaControl.h" #import "IJKCommon.h" #import "IJKDemoHistory.h" @implementation IJKVideoViewController - (void)dealloc { } + (void)presentFromViewController:(UIViewController *)viewController withTitle:(NSString *)title URL:(NSURL *)url completion:(void (^)())completion { IJKDemoHistoryItem *historyItem = [[IJKDemoHistoryItem alloc] init]; historyItem.title = title; historyItem.url = url; [[IJKDemoHistory instance] add:historyItem]; [viewController presentViewController:[[IJKVideoViewController alloc] initWithURL:url] animated:YES completion:completion]; } - (instancetype)initWithManifest: (NSString*)manifest_string { self = [self initWithNibName:@"IJKMoviePlayerViewController" bundle:nil]; if (self) { self.url = [NSURL URLWithString:@"ijklas:"]; self.manifest = manifest_string; } return self; } - (instancetype)initWithURL:(NSURL *)url { self = [self initWithNibName:@"IJKMoviePlayerViewController" bundle:nil]; if (self) { self.url = url; } return self; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } #define EXPECTED_IJKPLAYER_VERSION (1 << 16) & 0xFF) | - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. // [[UIApplication sharedApplication] setStatusBarHidden:YES]; // [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO]; #ifdef DEBUG [IJKFFMoviePlayerController setLogReport:YES]; [IJKFFMoviePlayerController setLogLevel:k_IJK_LOG_DEBUG]; #else [IJKFFMoviePlayerController setLogReport:NO]; [IJKFFMoviePlayerController setLogLevel:k_IJK_LOG_INFO]; #endif [IJKFFMoviePlayerController checkIfFFmpegVersionMatch:YES]; // [IJKFFMoviePlayerController checkIfPlayerVersionMatch:YES major:1 minor:0 micro:0]; IJKFFOptions *options = [IJKFFOptions optionsByDefault]; if (self.manifest != nil){ [options setPlayerOptionValue:@"ijklas" forKey:@"iformat"]; [options setPlayerOptionIntValue:0 forKey:@"find_stream_info"]; [options setFormatOptionValue:self.manifest forKey:@"manifest_string"]; } self.player = [[IJKFFMoviePlayerController alloc] initWithContentURL:self.url withOptions:options]; self.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; self.player.view.frame = self.view.bounds; self.player.scalingMode = IJKMPMovieScalingModeAspectFit; self.player.shouldAutoplay = YES; self.view.autoresizesSubviews = YES; [self.view addSubview:self.player.view]; [self.view addSubview:self.mediaControl]; self.mediaControl.delegatePlayer = self.player; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self installMovieNotificationObservers]; [self.player prepareToPlay]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [self.player shutdown]; [self removeMovieNotificationObservers]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ return UIInterfaceOrientationIsLandscape(toInterfaceOrientation); } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark IBAction - (IBAction)onClickMediaControl:(id)sender { [self.mediaControl showAndFade]; } - (IBAction)onClickOverlay:(id)sender { [self.mediaControl hide]; } - (IBAction)onClickDone:(id)sender { [self.presentingViewController dismissViewControllerAnimated:YES completion:nil]; } - (IBAction)onClickHUD:(UIBarButtonItem *)sender { if ([self.player isKindOfClass:[IJKFFMoviePlayerController class]]) { IJKFFMoviePlayerController *player = self.player; player.shouldShowHudView = !player.shouldShowHudView; sender.title = (player.shouldShowHudView ? @"HUD On" : @"HUD Off"); } } - (IBAction)onClickPlay:(id)sender { [self.player play]; [self.mediaControl refreshMediaControl]; } - (IBAction)onClickPause:(id)sender { [self.player pause]; [self.mediaControl refreshMediaControl]; } - (IBAction)didSliderTouchDown { [self.mediaControl beginDragMediaSlider]; } - (IBAction)didSliderTouchCancel { [self.mediaControl endDragMediaSlider]; } - (IBAction)didSliderTouchUpOutside { [self.mediaControl endDragMediaSlider]; } - (IBAction)didSliderTouchUpInside { self.player.currentPlaybackTime = self.mediaControl.mediaProgressSlider.value; [self.mediaControl endDragMediaSlider]; } - (IBAction)didSliderValueChanged { [self.mediaControl continueDragMediaSlider]; } - (void)loadStateDidChange:(NSNotification*)notification { // MPMovieLoadStateUnknown = 0, // MPMovieLoadStatePlayable = 1 << 0, // MPMovieLoadStatePlaythroughOK = 1 << 1, // Playback will be automatically started in this state when shouldAutoplay is YES // MPMovieLoadStateStalled = 1 << 2, // Playback will be automatically paused in this state, if started IJKMPMovieLoadState loadState = _player.loadState; if ((loadState & IJKMPMovieLoadStatePlaythroughOK) != 0) { NSLog(@"loadStateDidChange: IJKMPMovieLoadStatePlaythroughOK: %d\n", (int)loadState); } else if ((loadState & IJKMPMovieLoadStateStalled) != 0) { NSLog(@"loadStateDidChange: IJKMPMovieLoadStateStalled: %d\n", (int)loadState); } else { NSLog(@"loadStateDidChange: ???: %d\n", (int)loadState); } } - (void)moviePlayBackDidFinish:(NSNotification*)notification { // MPMovieFinishReasonPlaybackEnded, // MPMovieFinishReasonPlaybackError, // MPMovieFinishReasonUserExited int reason = [[[notification userInfo] valueForKey:IJKMPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue]; switch (reason) { case IJKMPMovieFinishReasonPlaybackEnded: NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonPlaybackEnded: %d\n", reason); break; case IJKMPMovieFinishReasonUserExited: NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonUserExited: %d\n", reason); break; case IJKMPMovieFinishReasonPlaybackError: NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonPlaybackError: %d\n", reason); break; default: NSLog(@"playbackPlayBackDidFinish: ???: %d\n", reason); break; } } - (void)mediaIsPreparedToPlayDidChange:(NSNotification*)notification { NSLog(@"mediaIsPreparedToPlayDidChange\n"); } - (void)moviePlayBackStateDidChange:(NSNotification*)notification { // MPMoviePlaybackStateStopped, // MPMoviePlaybackStatePlaying, // MPMoviePlaybackStatePaused, // MPMoviePlaybackStateInterrupted, // MPMoviePlaybackStateSeekingForward, // MPMoviePlaybackStateSeekingBackward switch (_player.playbackState) { case IJKMPMoviePlaybackStateStopped: { NSLog(@"IJKMPMoviePlayBackStateDidChange %d: stoped", (int)_player.playbackState); break; } case IJKMPMoviePlaybackStatePlaying: { NSLog(@"IJKMPMoviePlayBackStateDidChange %d: playing", (int)_player.playbackState); break; } case IJKMPMoviePlaybackStatePaused: { NSLog(@"IJKMPMoviePlayBackStateDidChange %d: paused", (int)_player.playbackState); break; } case IJKMPMoviePlaybackStateInterrupted: { NSLog(@"IJKMPMoviePlayBackStateDidChange %d: interrupted", (int)_player.playbackState); break; } case IJKMPMoviePlaybackStateSeekingForward: case IJKMPMoviePlaybackStateSeekingBackward: { NSLog(@"IJKMPMoviePlayBackStateDidChange %d: seeking", (int)_player.playbackState); break; } default: { NSLog(@"IJKMPMoviePlayBackStateDidChange %d: unknown", (int)_player.playbackState); break; } } } #pragma mark Install Movie Notifications /* Register observers for the various movie object notifications. */ -(void)installMovieNotificationObservers { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadStateDidChange:) name:IJKMPMoviePlayerLoadStateDidChangeNotification object:_player]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:IJKMPMoviePlayerPlaybackDidFinishNotification object:_player]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mediaIsPreparedToPlayDidChange:) name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification object:_player]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackStateDidChange:) name:IJKMPMoviePlayerPlaybackStateDidChangeNotification object:_player]; } #pragma mark Remove Movie Notification Handlers /* Remove the movie notification observers from the movie object. */ -(void)removeMovieNotificationObservers { [[NSNotificationCenter defaultCenter]removeObserver:self name:IJKMPMoviePlayerLoadStateDidChangeNotification object:_player]; [[NSNotificationCenter defaultCenter]removeObserver:self name:IJKMPMoviePlayerPlaybackDidFinishNotification object:_player]; [[NSNotificationCenter defaultCenter]removeObserver:self name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification object:_player]; [[NSNotificationCenter defaultCenter]removeObserver:self name:IJKMPMoviePlayerPlaybackStateDidChangeNotification object:_player]; } @end