Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVFoundation: Video to OpenGL texture working - How to play and sync audio?

I've managed to load a video-track of a movie frame by frame into an OpenGL texture with AVFoundation. I followed the steps described in the answer here: iOS4: how do I use video file as an OpenGL texture? and took some code from the GLVideoFrame sample from WWDC2010 which can be downloaded here.

How do I play the audio-track of the movie synchronously to the video? I think it would not be a good idea to play it in a separate player, but to use the audio-track of the same AVAsset.

AVAssetTrack* audioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

I retrieve a videoframe and it's timestamp in the CADisplayLink-callback via

CMSampleBufferRef sampleBuffer = [self.readerOutput copyNextSampleBuffer];
CMTime timestamp = CMSampleBufferGetPresentationTimeStamp( sampleBuffer );

where readerOutput is of type AVAssetReaderTrackOutput*

How to get the corresponding audio-samples? And how to play them?


Edit:

I've looked around a bit and I think, best would be to use AudioQueue from the AudioToolbox.framework using the approach described here: AVAssetReader and Audio Queue streaming problem

There is also an audio-player in the AVFoundation: AVAudioPlayer. But I don't know exactly how I should pass data to its initWithData-initializer which expects NSData. Furthermore, I don't think it's the best choice for my case because a new AVAudioPlayer-instance would have to be created for every new chunk of audio samples, as I understand it.

Any other suggestions? What's the best way to play the raw audio samples which I get from the AVAssetReaderTrackOutput?

like image 891
j00hi Avatar asked Aug 27 '11 10:08

j00hi


1 Answers

You want do do an AV composition. You can merge multiple media sources, synchronized temporally, into one output.

http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVComposition_Class/Reference/Reference.html

like image 84
Jody Hagins Avatar answered Oct 20 '22 14:10

Jody Hagins