如何使用AVPlayer获取HLS流当前时间?

时间:2017-09-29 13:13:35

标签: ios swift avplayer avplayeritem

我正在尝试获取AVPlayer正在重现的HLS流的位置,相对于流的开头。

我能够得到的是从播放开始的时间(AVPlayer.currentTime())和流开始的日期(AVPlayerItem.currentDate())。

我100%确定每秒MPEG TS文件中都嵌入了以秒为单位的偏移量,我无法找到从AVPlayer或AVPlayerItem访问该值的方法。

测试TS块的FFProbe输出:

ffprobe version 3.3.4 Copyright (c) 2007-2017 the FFmpeg developers
  built with Apple LLVM version 8.1.0 (clang-802.0.42)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3.4 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma --enable-vda
  libavutil      55. 58.100 / 55. 58.100
  libavcodec     57. 89.100 / 57. 89.100
  libavformat    57. 71.100 / 57. 71.100
  libavdevice    57.  6.100 / 57.  6.100
  libavfilter     6. 82.100 /  6. 82.100
  libavresample   3.  5.  0 /  3.  5.  0
  libswscale      4.  6.100 /  4.  6.100
  libswresample   2.  7.100 /  2.  7.100
  libpostproc    54.  5.100 / 54.  5.100
Input #0, mpegts, from 'toCharlie.ts':
  Duration: 00:00:08.40, start: 16117.400000, bitrate: 1808 kb/s
  Program 1 
    Stream #0:0[0x102]: Data: timed_id3 (ID3  / 0x20334449)
    Stream #0:1[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(progressive), 720x1280, 30 fps, 30 tbr, 90k tbn, 60 tbc
    Stream #0:2[0x101]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 132 kb/s

以秒为单位的偏移(start: 16117.400000)。

甚至可以从流的开始获取实际当前时间吗?

提前致谢。

编辑:为清楚起见,HLS流是实时流,而不是VOD流。

1 个答案:

答案 0 :(得分:0)

您是否尝试过addObserver这样的AVPlayer

player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)

并使用功能:

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {.....

通过iOS中的KVO概念跟踪状态响应