我正在使用AVAssetReader從視頻文件中獲取各個幀。我想知道如何從Mp4文件播放音頻。如何通過AVAssetReader播放聲音
的方法[玩家遊戲]的返回值是假的,所以沒有播放聲音,但爲什麼
感謝。
創建AVAssetReader
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"zhang" ofType:@"mp4"]];
AVURLAsset *avasset = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetTrack *track1 = [[avasset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey, [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsNonInterleaved, nil];
output1 = [[AVAssetReaderTrackOutput alloc] initWithTrack:track1 outputSettings:dic2];
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:avasset error:nil];
[reader addOutput:output1];
[reader startReading];
輸出代碼如下:
CMSampleBufferRef sample = [output1 copyNextSampleBuffer];
CMBlockBufferRef blockBufferRef = CMSampleBufferGetDataBuffer(sample);
size_t length = CMBlockBufferGetDataLength(blockBufferRef);
UInt8 buffer[length];
CMBlockBufferCopyDataBytes(blockBufferRef, 0, length, buffer);
NSData * data = [[NSData alloc] initWithBytes:buffer length:length];
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/test.mp3", docDirPath];
[data writeToFile:filePath atomically:YES];
[data release];
NSError *error;
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
player.numberOfLoops = 0;
[player play];
你不會一直在給視頻幀的OpenGL你會嗎?因爲我試圖做非常類似的事情,如果是的話,並想知道你是如何繼續這樣做的。 – 2017-04-05 12:24:38