對AVPlayer的一些簡單應用

jialebaba發表於2017-12-21

最近專案中要簡單用到一寫線上播放和本地播放的技術,其中包括一段錄音的定位精確播放:

//self.dianDuModel.audioStartPos:錄音的起始播放時間毫秒單位
//toleranceBefore:從哪開始找
//toleranceAfter:找到哪兒結束,設定引數有利於快速查詢
[self.player seekToTime:CMTimeMake(self.dianDuModel.audioStartPos/1000, 1) toleranceBefore:kCMTimeZero toleranceAfter:CMTimeMake(self.dianDuModel.audioStartPos/1000, 1) completionHandler:^(BOOL finished) {
        //找到後播放
        [weakSelf.player play];

    }];
複製程式碼

播放到什麼時間停止:

@property (strong, nonatomic) id                        timeObser;

self.timeObser = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        //        CMTime timeEnd = CMTimeMake(dianDuModel.audioEndPos, 1000);
        //當前播放的時間
        CGFloat current = CMTimeGetSeconds(time);

        NSLog(@"current:%f --- audioEndPos :%ld",current,self.dianDuModel.audioEndPos/1000);

        if (current >= (self.dianDuModel.audioEndPos/1000)) {

            [weakSelf.player pause];
            
        }
    }];
    //頁面退出時需要釋放掉self.timeObser:[self.player removeTimeObserver:self.timeObser];複製程式碼

相關文章