简体   繁体   中英

Using AVAssetReader and copyNextSampleBuffer without polling

I am using AVAssetReader to read audio data from a file, on a secondary thread. Every code example I have seen has at the heart of it a loop like this:

    while (assetReader.status == AVAssetReaderStatusReading) 
    {    
        // ...
        buffer = [theAssetReaderTrackOutput copyNextSampleBuffer];
        // ...
    }

(or the Swift equivalent). My question is, does copyNextSampleBuffer block while waiting for data from the AVAsset , or does it return NULL if the data is not yet available? If the latter, then we have a polling loop, which is poor practice, burning CPU cycles while repeatedly checking status . The documentation for copyNextSampleBuffer doesn't say whether or not it blocks the thread while waiting. I also can't tell if it attempts to return immediately with whatever data it has available, or fails/blocks unless it has enough data for a buffer of a certain minimum size.

This case is actually documented (always a pleasant surprise). The header file for copyNextSampleBuffer says

Copies the next sample buffer for the output synchronously.

And copyNextSampleBuffer is documented to return NULL when no more samples can be read on EOF status will be set to AVAssetReaderStatusCompleted https://developer.apple.com/documentation/avfoundation/avassetreaderoutput/1385732-copynextsamplebuffer

https://developer.apple.com/documentation/avfoundation/avassetreaderstatus?language=objc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM