iOS - ijkplayer

#import 

@property (nonatomic, retain) id  ijkplayer;



- (void)initijkplayer:(NSString *)urlStr {
    //    [IJKFFMoviePlayerController setLogReport:YES];
    //    [IJKFFMoviePlayerController setLogLevel:k_IJK_LOG_DEBUG];
    //    IJKFFOptions *options = [IJKFFOptions optionsByDefault];
    //    [options setPlayerOptionIntValue:1 forKey:@"videotoolbox"];
    //
    //    self.ijkplayer = [[IJKFFMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:urlStr] withOptions:options];
    //    self.ijkplayer.view.frame =CGRectMake(0, 100, self.view.frame.size.width, 200);
    //    self.ijkplayer.view.backgroundColor = [UIColor redColor];
    //    self.ijkplayer.scalingMode = IJKMPMovieScalingModeAspectFit;
    //    self.ijkplayer.shouldAutoplay = YES;
    //    [self.view addSubview:self.ijkplayer.view];
    //    [self.ijkplayer prepareToPlay];
    
    
    //调整参数
    IJKFFOptions *options = [IJKFFOptions optionsByDefault];
    
    
    [options setPlayerOptionIntValue:30  forKey:@"max-fps"];
    [options setPlayerOptionIntValue:30 forKey:@"r"];
    //跳帧开关
    [options setPlayerOptionIntValue:1  forKey:@"framedrop"];
    [options setPlayerOptionIntValue:0  forKey:@"start-on-prepared"];
    [options setPlayerOptionIntValue:0  forKey:@"http-detect-range-support"];
    [options setPlayerOptionIntValue:48  forKey:@"skip_loop_filter"];
    [options setPlayerOptionIntValue:0  forKey:@"packet-buffering"];
    [options setPlayerOptionIntValue:2000000 forKey:@"analyzeduration"];
    [options setPlayerOptionIntValue:25  forKey:@"min-frames"];
    [options setPlayerOptionIntValue:1  forKey:@"start-on-prepared"];
    [options setCodecOptionIntValue:8 forKey:@"skip_frame"];
    [options setFormatOptionValue:@"nobuffer" forKey:@"fflags"];
    [options setFormatOptionValue:@"8192" forKey:@"probsize"];
    //自动转屏开关
    [options setFormatOptionIntValue:0 forKey:@"auto_convert"];
    //重连次数
    [options setFormatOptionIntValue:1 forKey:@"reconnect"];
    //开启硬解码
    [options setPlayerOptionIntValue:1  forKey:@"videotoolbox"];
    // 清空DNS,有时因为在APP里面要播放多种类型的视频(如:MP4,直播,直播平台保存的视频,和其他http视频), 有时会造成因为DNS的问题而报10000问题的
//    [options setPlayerOptionIntValue:1 forKey:@"dns_cache_clear"];
    //ijk播放器
    self.ijkplayer = [[IJKFFMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:urlStr] withOptions:options];
    
    //播放区域
    self.playView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.playView.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:self.playView];
    
    UIView *playingView = [self.ijkplayer view];
    playingView.frame = self.playView.bounds;
    //    playingView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.playView insertSubview:playingView atIndex:1];
    
    [self.ijkplayer setScalingMode:IJKMPMovieScalingModeFill];
    
    [self installMovieNotificationObservers];
    
    if(![self.ijkplayer isPlaying]){
        [self.ijkplayer prepareToPlay];
        
    }
    
}

- (void)installMovieNotificationObservers{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loadStateDidChange:)
                                                 name:IJKMPMoviePlayerLoadStateDidChangeNotification
                                               object:self.ijkplayer];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackFinish:)
                                                 name:IJKMPMoviePlayerPlaybackDidFinishNotification
                                               object:self.ijkplayer];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(mediaIsPreparedToPlayDidChange:)
                                                 name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
                                               object:self.ijkplayer];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackStateDidChange:)
                                                 name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
                                               object:self.ijkplayer];
}
 
- (void)removeMovieNotificationObservers{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:IJKMPMoviePlayerLoadStateDidChangeNotification
                                                  object:self.ijkplayer];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:IJKMPMoviePlayerPlaybackDidFinishNotification
                                                  object:self.ijkplayer];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
                                                  object:self.ijkplayer];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
                                                  object:self.ijkplayer];
}







//network load state changes
- (void)loadStateDidChange:(NSNotification *)notification{
    IJKMPMovieLoadState loadState = self.ijkplayer.loadState;
    NSLog(@"LoadStateDidChange : %d",(int)loadState);
}
 
//when movie playback ends or a user exits playback.
- (void)moviePlayBackFinish:(NSNotification *)notification{
    int reason = [[[notification userInfo] valueForKey:IJKMPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    NSLog(@"playBackFinish : %d",reason);
}
 
//
- (void)mediaIsPreparedToPlayDidChange:(NSNotification *)notification{
    NSLog(@"mediaIsPrepareToPlayDidChange");
}
 
// when the playback state changes, either programatically or by the user
- (void)moviePlayBackStateDidChange:(NSNotification *)notification{
    switch (_ijkplayer.playbackState) {
        case IJKMPMoviePlaybackStateStopped:
            NSLog(@"playBackState %d: stoped", (int)self.ijkplayer.playbackState);
            break;
        case IJKMPMoviePlaybackStatePlaying:
            NSLog(@"playBackState %d: playing", (int)self.ijkplayer.playbackState);
            break;
        case IJKMPMoviePlaybackStatePaused:
            NSLog(@"playBackState %d: paused", (int)self.ijkplayer.playbackState);
            break;
        case IJKMPMoviePlaybackStateInterrupted:
            NSLog(@"playBackState %d: interrupted", (int)self.ijkplayer.playbackState);
            break;
        case IJKMPMoviePlaybackStateSeekingForward:
            break;
        case IJKMPMoviePlaybackStateSeekingBackward:
            break;
        default:
            break;
    }
}

你可能感兴趣的:(iOS - ijkplayer)