zoukankan      html  css  js  c++  java
  • AVKit & MediaPlayer简写

    @import AVKit;

    @import AVFoundation;

     NSString *address = [[NSBundle mainBundle]pathForResource:@"show" ofType:@"mp4"];

        NSURL *urlPath = [NSURL fileURLWithPath:address];

        self.playerController = [[AVPlayerViewController alloc]init];

        self.playerController.player = [AVPlayer playerWithURL:urlPath];

        self.playerController.showsPlaybackControls = NO;     //不显示播放器控件

        self.playerController.videoGravity = AVLayerVideoGravityResizeAspectFill;  //设置拉伸模式

        self.playerController.view.frame = CGRectMake(0, 0, KWidth, KHeight);

         [self.navigationController.view addSubview:self.playerController.view];

        [self.playerController.player play];

    @import AVFoundation;

    @import MediaPlayer;

    self.mPlayer = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL  URLWithString:@"http://192.168.42.1/DCIM/100MEDIA/FILE0001.mp4"]];

       

        self.mPlayer.view.frame = CGRectMake(0, 150, self.view.frame.size.width, 220);

        [self.view addSubview:self.mPlayer.view];

        self.mPlayer.scalingMode = MPMovieScalingModeAspectFill;

        [self.mPlayer play];

      [self.mPlayer requestThumbnailImagesAtTimes:@[@1.2,@2.1] timeOption:MPMovieTimeOptionNearestKeyFrame];

     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(noti:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];

    - (void)noti:(NSNotification*)noti{

        static int a = 1;

        if (a == 1) {

             _img1.image = noti.userInfo[MPMoviePlayerThumbnailImageKey];

        }else{

            _img2.image = noti.userInfo[MPMoviePlayerThumbnailImageKey];

        }

        a ++;

     }

     NSURL *videoUrl = [NSURL URLWithString:@"http://192.168.42.1/DCIM/100MEDIA/FILE0140.mp4"];

        self.playerItem = [AVPlayerItem playerItemWithURL:videoUrl];

        [self.playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];// 监听status属性

        [self.playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];// 监听loadedTimeRanges属性

        self.player = [AVPlayer playerWithPlayerItem:self.playerItem];

        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerItem];

        self.layer = [AVPlayerLayer playerLayerWithPlayer:self.player];

        _layer.frame = self.view.layer.frame;

        [self.view.layer addSublayer:_layer];

        [self.player setVolume:0.8];

         [self.player play];

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

        

        AVPlayerItem *playerItem = (AVPlayerItem *)object;

        if ([keyPath isEqualToString:@"status"]) {

            if ([playerItem status] == AVPlayerStatusReadyToPlay) {

                NSLog(@"AVPlayerStatusReadyToPlay");

              //  self.stateButton.enabled = YES;

                CMTime duration = self.playerItem.duration;// 获取视频总长度

                CGFloat totalSecond = playerItem.duration.value / playerItem.duration.timescale;// 转换成秒

               // _totalTime = [self convertTime:totalSecond];// 转换成播放时间

                 //[self customVideoSlider:duration];// 自定义UISlider外观

                NSLog(@"movie total duration:%f",CMTimeGetSeconds(duration));

                [self monitoringPlayback:self.playerItem];// 监听播放状态

            } else if ([playerItem status] == AVPlayerStatusFailed) {

                NSLog(@"AVPlayerStatusFailed");

            }

        } else if ([keyPath isEqualToString:@"loadedTimeRanges"]) {

            NSTimeInterval timeInterval = [self availableDuration];// 计算缓冲进度

            NSLog(@"Time Interval:%f",timeInterval);

            CMTime duration = self.playerItem.duration;

            CGFloat totalDuration = CMTimeGetSeconds(duration);

            [self.gress setProgress:timeInterval/totalDuration animated:YES];

            NSLog(@"%f",timeInterval / totalDuration);

        }

    }

    - (NSTimeInterval)availableDuration {

        

        NSArray *loadedTimeRanges = [[self.player currentItem] loadedTimeRanges];

        CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];// 获取缓冲区域

        float startSeconds = CMTimeGetSeconds(timeRange.start);

        float durationSeconds = CMTimeGetSeconds(timeRange.duration);

        NSTimeInterval result = startSeconds + durationSeconds;// 计算缓冲总进度

        return result;

    }

    - (NSString *)convertTime:(CGFloat)second{

        NSDate *d = [NSDate dateWithTimeIntervalSince1970:second];

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

        if (second/3600 >= 1) {

            [formatter setDateFormat:@"HH:mm:ss"];

        } else {

            [formatter setDateFormat:@"mm:ss"];

        }

        NSString *showtimeNew = [formatter stringFromDate:d];

        return showtimeNew;

    }

    - (void)monitoringPlayback:(AVPlayerItem *)playerItem {

        

        [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:NULL usingBlock:^(CMTime time) {

            CGFloat currentSecond = playerItem.currentTime.value/playerItem.currentTime.timescale;// 计算当前在第几秒

            [self updateVideoSlider:currentSecond];

            NSString *timeString = [self convertTime:currentSecond];

            //self.timeLabel.text = [NSString stringWithFormat:@"%@/%@",timeString,_totalTime];

            NSLog(@"%f,%@",currentSecond,timeString);

        }];

    }

    -(void)updateVideoSlider:(CGFloat)f{

        CMTime duration = self.playerItem.duration;// 获取视频总长度

        CGFloat totalSecond = self.playerItem.duration.value / self.playerItem.duration.timescale;

        self.playerSlider.value = f / totalSecond;

    }

    -(void)moviePlayDidEnd:(NSNotification*)noti{

    }

    提高技能如同提升自信心。
  • 相关阅读:
    itextpdf 和 pdfbox 测试用例
    Java base64 编码、解码
    常用字体
    DIY 单片机 入门
    太难了。
    Java 之 native:JNI 本地方法测试实现
    Java 之 String 引用传递
    Java class 文件 方法的指令含义
    Spring学习文章链接
    数据结构_树_图_总结
  • 原文地址:https://www.cnblogs.com/chims-liu-touch/p/5740229.html
Copyright © 2011-2022 走看看