I would like to make several H264 mp4 videos at several presentations simultaneously. The goal is to read about 8 short videos, each 100x100 pixels in size and allows you to simultaneously display their contents at several positions on the screen.
Imagine 24 squares on the screen, each of which shows one video from 8 videos.
MoviePlayer does not work because it only shows one full-screen video. AVPlayer with several AVPlayerLayers is limited, because only the most recently created Layer will show its contents on the screen (according to the documentation and my testing).
So, I wrote a short video class and created an instance for each .mp4 file in my package, using AVAssetReader to read it. When updating, each video fragment is restored, converted to UIImage and displayed in accordance with the frame rate of the video. In addition, these images are cached for quick access when cycling.
- (id) initWithAsset:(AVURLAsset*)asset withTrack:(AVAssetTrack*)track
{
self = [super init];
if (self)
{
NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA], (NSString*)kCVPixelBufferPixelFormatTypeKey, nil];
mOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:track outputSettings:settings];
mReader = [[AVAssetReader alloc] initWithAsset:asset error:nil];
[mReader addOutput:mOutput];
BOOL status = [mReader startReading];
}
return self;
}
- (void) update:(double)elapsed
{
CMSampleBufferRef buffer = [mOutput copyNextSampleBuffer];
if (buffer)
{
UIImage* image = [self imageFromSampleBuffer:sampleBuffer];
}
[...]
}
Actually it works very well, but only for 4 videos. The fifth never appears. At first I thought about memory issues, but I tested them on the following devices:
- iPhone 3GS
- iPhone 4
- iPad
- iPad 2
I had the same behavior on each device: 4 videos playing in a smooth cycle, without differences.
If this were a memory problem, I would expect at least the iPad 2 to show 5 or 6 videos (hardware is better because of this) or 3GS to show only 1 or a crash somewhere.
.
,
BOOL status = [mReader startReading];
false 5,6,7 8.
, - ( ), AVAssetReaders? . , .