开发者

Playing sound using AVAssetReader

I am using AVAssetReader to get the individual frames from a video file (see code below). I would like to know how I can play the audio from the video at the same time.

I tried to separate the audio into a new file and play it using AVAudioPlayer, but that crashes if I am using it at the same time as the AVAssetReader.

NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];    
NSString* fullPath = [[documentsDirectory stringByAppendingPathComponent:@"sample_video.mp4"] retain];
NSString *urlAddress = fullPath;
NSURL *url = [NSURL fileURLWithPath:urlAddress];
AVURLAsset *urlAsset = [[AVURLAsset alloc] initWithURL:url options:nil];
NSArray *tracks = [urlAsset tracksWithMediaType:AVMedi开发者_开发百科aTypeVideo];
AVAssetTrack *track = [tracks objectAtIndex:0];
NSDictionary* trackDictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] 
                                                            forKey:(id)kCVPixelBufferPixelFormatTypeKey];

AVAssetReaderTrackOutput *asset_reader_output = [[AVAssetReaderTrackOutput alloc] initWithTrack:track outputSettings:trackDictionary];

AVAssetReader *asset_reader = [[AVAssetReader alloc] initWithAsset:urlAsset error:nil];
[asset_reader addOutput:asset_reader_output];
[asset_reader startReading];

while (asset_reader.status == AVAssetReaderStatusReading){

    CMSampleBufferRef sampleBufferRef = [asset_reader_output copyNextSampleBuffer];

    if (sampleBufferRef){
            [self performSelectorOnMainThread:@selector(processFrame) withObject:nil waitUntilDone:YES];
    }
    CMSampleBufferInvalidate(sampleBufferRef);
    CFRelease(sampleBufferRef);
}
[pool release];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜