以下是我对音频播放插件的设计图
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;
}
}
网友评论