I'm familiar with how to stream audio data from the ipod library using AVAssetReader, but I'm at a loss as to how to seek within the track. e.g. start playback at the halfway point, etc. Starting from the beginning and then sequentially getting successive samples is easy, but surely there must be a way to have random access?
-
Short of starting from the beginning and simply throwing the unwanted samples away, I mean. (which would be crudely effective but probably counts as a venal programming sin.) – Will Apr 01 '11 at 02:43
2 Answers
AVAssetReader has a property, timeRange, which determines the time range of the asset from which media data will be read.
@property(nonatomic) CMTimeRange timeRange
The intersection of the value of this property and CMTimeRangeMake(kCMTimeZero, asset.duration) determines the time range of the asset from which media data will be read.
The default value is CMTimeRangeMake(kCMTimeZero, kCMTimePositiveInfinity). You cannot change the value of this property after reading has started.
So, if you want to seek to the middle the track, you'd create a CMTimeRange from asset.duration/2 to asset.duration, and set that as the timeRange on the AVAssetReader.

- 3,645
- 24
- 24
-
5The implication is that to seek within an AVAsset you have to recreate the AVAssetReader. – Rhythmic Fistman Apr 08 '11 at 14:24
-
1Just to add some clarity... I've attempted to stop the reader, set the value and start reading again. It failed. The error indicated that you cannot set the range after starting (even if stopping). I was able to use Rhythmic Fistman's suggestion and re-create the AVAssetReader. It works quickly in my situation with files on the device. – ptoinson Jul 16 '11 at 19:46
-
@ptoinson when you say it works *quickly*.. how quickly? as in can you play the contents of an entire file without any jitter or gaps? B/c that's what's happening to me.. see [here](http://stackoverflow.com/questions/13210684/reading-samples-with-avassetreader-and-timerange-in-real-time) – abbood Nov 03 '12 at 15:42
-
@abbood this will be horribly slow, and not real-time by a long shot. I've tried it. AVPlayerItem and AVPlayerItemVideoOutput have much faster seeking but they come with their own quirks and weirdness such as pixel buffers not coming back as expected. – jjxtra Mar 19 '15 at 00:20
-
1@jjxtra Not to mention that `AVPlayerItemVideoOutput` _forces_ you to copy pixel buffers, whereas `AVAssetReaderTrackOutput` can be instructed to not copy sample buffers if possible...makes minimizing memory footprints really obnoxious. – CIFilter Nov 18 '19 at 22:05
AVAssetReader
is amazingly slow when seeking. If you try to recreate an AVAssetReader
to seek while the user is dragging a slider, your app will bring iOS to its knees.
Instead, you should use an AVAssetReader
for fast forward only access to video frames, and then also use an AVPlayerItem
and AVPlayerItemVideoOutput
when the user wants to seek with a slider.
It would be nice if Apple combined AVAssetReader
and AVPlayerItem
/ AVPlayerItemVideoOutput
into a new class that was performant and was able to seek quickly.
Be aware that AVPlayerItemVideoOutput
will not give back pixel buffers unless there is an AVPlayer
attached to the AVPlayerItem
. This is obviously a strange implementation detail, but it is what it is.
If you are using AVPlayer
and AVPlayerLayer
, then you can simply use the seek methods on AVPlayer
itself. The above details are only important if you are doing custom rendering with the pixel buffers and/or need to send the pixel buffers to an AVAssetWriter
.

- 20,415
- 16
- 100
- 140