码迷,mamicode.com
首页 > 其他好文 > 详细

avplayer 播放视频

时间:2015-09-16 00:46:42      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:

最近在做一个小学生教育方面的项目,要在应用中看各种各样的小故事,所以要用到视频,刚把视频方面的东西搞好,在这里稍微的分享一下

首先介绍几个方法吧和属性吧。

- (id)addPeriodicTimeObserverForInterval:(CMTime)interval queue:(dispatch_queue_t)queue usingBlock:(void (^)(CMTime time))block

这个方法可以用于跟新进度条。

- (void)seekToTime:(CMTime)time completionHandler:(void (^)(BOOL finished))completionHandler

这个是设置从哪个位置开始播放

 CMTime changedTime = CMTimeMakeWithSeconds(timeFloat, 1.0);

获取 CMTime

rate 播放的状态  0 表示暂停  1表示播放

volume 声音

下面是主要的代码

 1 +(Class)layerClass
 2 {
 3     return [AVPlayerLayer class];
 4 }
 5 
 6 -(void)setMoviePlayer:(AVPlayer *)moviePlayer
 7 {
 8     AVPlayerLayer *layer = (AVPlayerLayer *)[self layer];
 9     layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
10     layer.player = moviePlayer;
11 }
12 
13 -(AVPlayer *)moviePlayer
14 {
15     AVPlayerLayer *layer = (AVPlayerLayer *)[self layer];
16     return layer.player;
17 }

 

 

 

 1 /**
 2  *  初始化视频播放器
 3  *
 4  *  @param movieUrl url网址
 5  */
 6 -(void)setMovieUrl:(NSString *)movieUrl
 7 {
 8     _movieUrl = movieUrl;
 9     _movieItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:movieUrl]];
10     _moviePlayer = [AVPlayer playerWithPlayerItem:_movieItem];
11     _moviePlayer.volume = 0.5;
12     [_voiceView setProgressView:0.5];
13     _playerLayer.moviePlayer = _moviePlayer;
14     [self addNotifiction];
15     [_playerLayer.moviePlayer pause];
16 }
/**
 *  添加监听
 */
-(void)addNotifiction
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinishedNotifiction:) name:AVPlayerItemDidPlayToEndTimeNotification object:_playerLayer.moviePlayer.currentItem];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackError:) name:AVPlayerItemNewAccessLogEntryNotification object:_playerLayer.moviePlayer.currentItem];
    [_movieItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
}

 

 

 1   //获取播放的进度
 2     AVPlayerItem *mobieItem = _movieItem;
 3     __block LSCacheView * progress = _progressView;
 4     [_moviePlayer addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
 5         float current=CMTimeGetSeconds(time);
 6         float total=CMTimeGetSeconds([mobieItem duration]);
 7         if (current) {
 8             [progress setProgressView:(current/total)];
 9         }
10     }];

 

    if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
        NSTimeInterval timeInterval = [self availableDuration];
        NSTimeInterval cuttTime = CMTimeGetSeconds(_movieItem.currentTime);
        CMTime duration = _movieItem.duration;
        CGFloat totalDuration = CMTimeGetSeconds(duration);
        
        if (cuttTime < timeInterval)
        {
            //有时候网络卡会自动暂停,通过这个方式可以避免这种方式
            if (_playerLayer.moviePlayer.rate == 0 && _playerButton.isSelected == YES)
            {
                [_playerLayer.moviePlayer play];
            }
        }
        else
        {

            
        }
    }

 

 以上这些我认为就是主要的代码了。哦   还有个进入全屏播放的方式,大部分的项目都用到了UINavigationController,所以我用的横屏播放方式是模态跳转的方式

在需要横屏的controller添加以下代码

 1 - (BOOL)shouldAutorotate
 2 {
 3     return NO;
 4 }
 5 
 6 -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
 7 {
 8     return UIInterfaceOrientationLandscapeRight;
 9 }
10 
11 -(NSUInteger)supportedInterfaceOrientations
12 {
13     return UIInterfaceOrientationMaskLandscapeRight;
14 }

 

这便可以跳转进入的时候横屏了

这里可以下载源码 

https://github.com/LuShui/player

avplayer 播放视频

标签:

原文地址:http://www.cnblogs.com/lsios/p/4811858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!