我在iOS上使用此VideoController,以便用户可以加载本地视频并随意播放。每次加载第三个视频时,都会生成“已接收内存警告”,并立即抛出EXC_BAD_ACCESS异常。我猜有些东西因为上面的警告而被解除分配,可能是AVPLayerItemOutput对象?
这是我用来控制播放的代码:
- (void)setupVideoPlayer:(NSURL*) movieUrl completion:(void(^)(void))callback;
{
if (player) {
[player pause];
player = nil;
}
if (movieFile){
movieFile = nil;
}
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:movieUrl options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CGImageRef oneRef = [gen copyCGImageAtTime:CMTimeMake(1, 2) actualTime:NULL error:&err];
_size = CGSizeMake(CGImageGetWidth(oneRef), CGImageGetHeight(oneRef));
dispatch_async(dispatch_get_main_queue(), ^{
player = [AVPlayer playerWithURL:movieUrl];
[[player currentItem] addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusAndTracksContext];
[[player currentItem] addObserver:self forKeyPath:@"tracks" options:0 context:&ItemStatusAndTracksContext];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[player currentItem]];
movieFile = [[GPUImageMovie alloc] initWithPlayerItem:[player currentItem]];
movieFile.playAtActualSpeed = YES;
movieFile.shouldRepeat = YES;
videoInput = &*movieFile;
callback();
player.rate = 1.0;
__weak typeof(self) weakSelf = self;
[player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1.0 / 60.0, NSEC_PER_SEC) queue:NULL usingBlock:^(CMTime time)
{
[weakSelf updateDelegateProgressView];
}
];
[movieFile startProcessing];
double delayToStartRecording = 0.5;
dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, delayToStartRecording * NSEC_PER_SEC);
dispatch_after(startTime, dispatch_get_main_queue(), ^(void){
[player play];
});
});
}