我有一个使用QTKit
读取和创建QuickTime
电影文件的类。我在电影中添加音轨的功能在 Snow Leopard 上运行良好,但在 Lion 或 Mountain Lion 上不起作用。音频信息确实出现在QuickTime
播放器的“显示电影检查器”中,并且电影的持续时间正确。但是,在电影播放时音频是听不见的。这是我在 10.6 中运行良好但在 10.7 或 10.8 中运行良好的代码:
// here's the code that creates the exported movie
NSString *filePath = [NSString stringWithUTF8String:"Foo.mov"];
NSError *errorPtr;
qtMovie = [[QTMovie alloc] initToWritableFile:filePath error:&errorPtr];
// and now here is my function to add an audio track
bool PLQTMovie::addSoundTrack(const std::string &fileName, const long long srcStartTime, const long long srcEndTime, const long long dstStartTime)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString *file = [NSString stringWithUTF8String:fileName.c_str()];
NSError *errorPtr = nil;
QTMovie *audioMovie = [QTMovie movieWithFile:file error:&errorPtr];
NSArray *tracks = [audioMovie tracksOfMediaType:QTMediaTypeSound];
if([tracks count] == 0)
return false;
QTTrack *soundTrack = [tracks objectAtIndex:0];
QTMedia *media = [soundTrack media];
NSValue *durationValue = [media attributeForKey:QTMediaDurationAttribute];
QTTime soundTrackTime;
[durationValue getValue:&soundTrackTime];
long audioScale = soundTrackTime.timeScale;
long long srcDuration = 0;
if(srcEndTime < 0) // use entire source track duration
{
srcDuration = soundTrackTime.timeValue;
}
else
{
srcDuration = srcEndTime - srcStartTime;
}
QTTime startTime = QTMakeTime(srcStartTime, audioScale);
QTTime srcDurationTime = QTMakeTime(srcDuration, audioScale);
QTTimeRange srcTimeRange = QTMakeTimeRange(startTime, srcDurationTime);
QTTime dstTime = QTMakeTime(dstStartTime, audioScale);
QTTrack *audioTrack = [(QTMovie*)qtMovie insertSegmentOfTrack:soundTrack timeRange:srcTimeRange atTime:dstTime];
[pool drain];
return true;
}
我知道 Apple 鼓励从QTKit
to过渡AV Foundation
,但旧QTKit
代码应该仍然有效。我不知道我应该在这里做些什么不同的事情。有任何想法吗?