美文网首页
AVPlayer音频播放--进度的坑

AVPlayer音频播放--进度的坑

作者: 你飞跃俊杰 | 来源:发表于2020-03-24 16:54 被阅读0次

    以下是我对音频播放插件的设计图

    1585040056731.jpg
    //只能播放单个资源
    [AVPlayer playerWithURL:[NSURL URLWithString:url]]; 
    

    //增加监听

    //秒轮询监听
    - (void)addPeriodicTimeObserver {
        // Invoke callback every half second
        CMTime interval = CMTimeMakeWithSeconds(1, NSEC_PER_SEC);
        // Queue on which to invoke the callback
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        // Add time observer
        self.timeObserverToken =
            [self.player addPeriodicTimeObserverForInterval:interval
                                                      queue:mainQueue
                                                 usingBlock:^(CMTime time) {
                // Use weak reference to self
                // Update player transport UI
                double currentTime = CMTimeGetSeconds(time);
                NSLog(@"%f",currentTime);
    
    //由于是边播放边下载进度和总时长都是不正常的
                //进度 当前时间/总时间
                currentTime = CMTimeGetSeconds(sSelf.player.currentItem.currentTime);
                double duration = CMTimeGetSeconds(sSelf.player.currentItem.duration);
                double progress = currentTime/ duration;
                NSLog(@"%f:%f:%f",currentTime,duration,progress);
    //            if ([NSValue valueWithCMTime:currentTime]==1) {
    //                NSLog(@"success");
    //            }
            }];
    }
    
    //百分比轮询监听
    //由于是边播放边下载,百分比是不正常的
    - (void)addBoundaryTimeObserver {
        NSMutableArray *times = [NSMutableArray array];
     
        // Set initial time to zero
        CMTime currentTime = kCMTimeZero;
        // Get asset duration
        CMTime assetDuration = self.player.currentItem.duration;
        // Divide the asset duration into quarters
        CMTime interval = CMTimeMultiplyByFloat64(assetDuration, 0.25);
     
        // Build boundary times at 25%, 50%, 75%, 100%
        while (CMTIME_COMPARE_INLINE(currentTime, <, assetDuration)) {
            currentTime = CMTimeAdd(currentTime, interval);
            [times addObject:[NSValue valueWithCMTime:currentTime]];
        }
        __weak typeof(self) wSelf = self;
        // Add time observer
        self.timeObserverToken =
            [self.player addBoundaryTimeObserverForTimes:times
                                                   queue:dispatch_get_main_queue()
                                              usingBlock:^{
                // Use weak reference to self
                // Update user interface state
                __strong typeof(wSelf) sSelf = wSelf;
                NSLog(@"%f",sSelf.player.currentItem.currentTime.value/sSelf.player.currentItem.currentTime.timescale-NSTimeIntervalSince1970);
                NSLog(@"%@",times);
            }];
    }
    
    移除监听
    - (void)removeBoundaryTimeObserver {
        if (self.timeObserverToken) {
            [self.player removeTimeObserver:self.timeObserverToken];
            self.timeObserverToken = nil;
        }
    }
    

    相关文章

      网友评论

          本文标题:AVPlayer音频播放--进度的坑

          本文链接:https://www.haomeiwen.com/subject/sibgdhtx.html