遊び方による音AVAssetReader
-
28-10-2019 - |
質問
を使用していAVAssetReaderの個々のフレームからビデオファイルです。思いっきのオーディオをMp4ファイルです。
戻り値の方法[プレイヤーの遊び"がfalseの場合、音が出なくなってしまい、なぜ
感謝。
を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];
解決
一つのオプションサービスの利用AVAudioPlayer、代わりに使用PCMデータを保存します復号を埋める AudioQueue 出力します。
最適な方法であるを AVComposition からのオーディオトラック AVAsset から作成されたソースmp4ファイルです。そのAVComposition(ラスのサブクラスAVAsset)を使用できる内AVPlayer、そしてるだけで劇中のオーディオトラックです。
ませんでしたブロックのPCMデータをファイルの拡張子".mp3"が符号化された形式です。のAVAudioPlayer見るのは一定の符号化されたデータのファイルのファイルを書いていますの互換性のない、受けている戻り値がfalseです。していれば心のオーディオ出力ファイルを使用 AVAssetWriter 適切な設定します。これを書き出中核として、オーディオファイルを用いています。このステップがエンコード、オーディオとして別の形式(例えば、mp3又はデスクトップ、エンタープライズ)しかし、これにより重いコストパフォーマンスが関連付けられ、そのことには適さなリアルタイム再生ができます。
他のヒント
資産はすぐに再生できません。キー値の観察を使用してステータスの値を観察する必要があります。 AVCAMサンプルのAVCAMサンプルを参照してください。