我在 iOS 上工作,我想一个接一个地读取视频文件,视频之间没有延迟。
此外,我必须能够在播放时刷新播放列表(将视频添加到队列中)。
我已经尝试使用 AvQueuePlayer,它工作正常,我可以刷新播放列表,但每个视频之间有一点延迟。
我试过 AVMutableComposition,视频之间没有延迟,但我在播放视频时无法刷新播放列表。
是否存在任何其他解决方案?视频之间没有延迟,我可以在哪里刷新播放列表?
谢谢。
编辑 :
下面的代码有效,我播放了一些 0.5 秒的视频文件,没有间隙!但是有一个问题,声音很好,但是视频每次都卡顿!为什么 ?
AVMutableComposition *myCompo = [[AVMutableComposition alloc] init];
for (int countName = 0; countName<10; countName++)
{
//Initialize Asset
NSURL *MyURL = [[NSBundle mainBundle]
URLForResource: [NSString stringWithFormat:@"%d", countName] withExtension:@"mp4"];
AVURLAsset * myAsset = [[AVURLAsset alloc] initWithURL:MyURL options:nil];
//Track AUDIO
AVMutableCompositionTrack *compositionAudioTrack = [myCompo addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack setPreferredVolume:1.0];
AVAssetTrack *clipAudioTrack = [[myAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
//Track VIDEO
AVMutableCompositionTrack *compositionVideoTrack = [myCompo addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipVideoTrack = [[myAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
//Time lenght
CMTime durationFile = clipAudioTrack.timeRange.duration;
CMTime timeCompo = myCompo.duration;
//Insert TRACK AUDIO
BOOL result = [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, durationFile) ofTrack:clipAudioTrack atTime:timeCompo error:nil];
if(result)
{
NSLog(@"Audio Ok");
}
else
{
NSLog(@"Audio not ok");
}
//Insert TRACK VIDEO
result =[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, durationFile) ofTrack:clipVideoTrack atTime:timeCompo error:nil];
if(result)
{
NSLog(@"Video Ok");
}
else
{
NSLog(@"Video not ok");
}
}
dispatch_sync(dispatch_get_main_queue(), ^{
player2 = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:myCompo]];
playerLayer2 = [AVPlayerLayer playerLayerWithPlayer:player2];
playerLayer2.frame = [self view].layer.bounds;
playerLayer2.videoGravity = AVLayerVideoGravityResizeAspectFill;
[[self view].layer insertSublayer:playerLayer2 atIndex:0];
});
[player2 play];