使用AVPlayer在iphone上播放许多不同的视频
我正在使用AVFoundation为iOS开发一个自定义视频播放器。这个想法是能够切换到一个不同的视频与一个手势(点击,滑动,无论什么)现在播放器在模拟器上完美地工作,但当我在实际设备上测试它时,在3或4次滑动后,视图变为空白。我甚至为我的播放器创建了播放控件,当视图变为空白时,这些控件会正确加载,但不会执行任何操作。有什么想法吗,伙计们? 这是播放器的初始化使用AVPlayer在iphone上播放许多不同的视频,iphone,xcode,ipad,media-player,avfoundation,Iphone,Xcode,Ipad,Media Player,Avfoundation,我正在使用AVFoundation为iOS开发一个自定义视频播放器。这个想法是能够切换到一个不同的视频与一个手势(点击,滑动,无论什么)现在播放器在模拟器上完美地工作,但当我在实际设备上测试它时,在3或4次滑动后,视图变为空白。我甚至为我的播放器创建了播放控件,当视图变为空白时,这些控件会正确加载,但不会执行任何操作。有什么想法吗,伙计们? 这是播放器的初始化 - (id)initWithContentURL:(NSString *)aContentURL delegat
- (id)initWithContentURL:(NSString *)aContentURL delegate:(id)aDelegate {
self = [super initWithNibName:@"NoCashMoviePlayer" bundle:nil];
if (self == nil)
return nil;
delegate = aDelegate;
systemPath = [aContentURL retain];
contentURL = [[NSURL alloc]initFileURLWithPath:systemPath];
asset = [AVURLAsset URLAssetWithURL:contentURL options:nil];
playerItem = [AVPlayerItem playerItemWithAsset:asset];
isPaused = false;
controlsHidden = false;
self.player = [[AVPlayer playerWithPlayerItem:playerItem] retain];
duration = self.player.currentItem.asset.duration;
return self;
}
这是播放视频的代码:
-(void)playMovie{
UITapGestureRecognizer *tapRecon = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toggleControls:)];
[tapRecon setNumberOfTapsRequired:2];
[self.movieContainer addGestureRecognizer:tapRecon];
[tapRecon release];
NSLog(@"Playing item: %@",contentURL);
playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
[movieContainer.layer addSublayer:playerLayer];
playerLayer.frame = movieContainer.layer.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
self.seeker.alpha = 1.0;
[self.view addSubview:movieContainer];
[self.movieContainer addSubview:controls];
[self setSlider];
[player play];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[player currentItem]];
}
选择要播放的剪辑的代码:
-(void)viewSelect: (double) curTime{
self.myView.backgroundColor = [UIColor blackColor];
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.myView addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFromLeft:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.myView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
if(isMain){
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationTransitionFlipFromLeft animations:^{
self.myView.alpha = 1.0;
moviePlayer = [[NoCashMoviePlayer alloc]initWithContentURL:[self movieURL:vidIndex] delegate:self];
self.moviePlayer.view.frame = self.myView.bounds;
self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
[self.myView addSubview:moviePlayer.view];
}completion:^(BOOL finished) {
[self.moviePlayer.player seekToTime:CMTimeMake(curTime, 1)];
[self.moviePlayer playMovie];
}];
}else{
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationTransitionFlipFromLeft animations:^{
self.otherView.alpha = 1.0;
moviePlayer = [[NoCashMoviePlayer alloc]initWithContentURL:[self movieURL:vidIndex] delegate:self];
self.moviePlayer.view.frame = self.otherView.bounds;
self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
[self.otherView addSubview:moviePlayer.view];
}completion:^(BOOL finished) {
[self.moviePlayer.player seekToTime:CMTimeMake(curTime, 1)];
[self.moviePlayer playMovie];
}];
}
}
最后是手势动作:
- (void)handleSwipeFromLeft:(UISwipeGestureRecognizer *)recognizer {
double elapsedTime = 0.0;
if(vidIndex==0){
vidIndex = 3;
}else vidIndex = vidIndex --;
elapsedTime = [self.moviePlayer currentTimeInSeconds];
[self.moviePlayer stopMovie];
isMain = !isMain;
[self viewSelect: elapsedTime];
}
编辑:尝试对每个视频文件使用不同的AVPlayerLayer,相同的情况下,在模拟器中工作,而不是在iPad上
编辑2:我运行instruments来分析核心动画性能,当播放视频时,它显示的帧速率约为30 fps,当播放器变为空白时,它会一直下降到1或2 fps。这可能是卵生的,但如果它有助于提供更多的光
编辑3:好的,我终于有了进展,我知道问题是什么,我有一个核心动画内存泄漏,在模拟器中它“工作”,因为计算机的内存比iPad多得多,但由于iPad的内存非常有限,它很快停止工作。如果任何人对核心动画泄漏有任何建议,都会受到欢迎。代码中有两个地方保留了一个对象,我在您发布的代码中没有看到相应的版本。首先,我要确保内存引用计数的增量和减量都是适当平衡的 在我看来,第一个问题很可疑。通常,您应该首先自动释放指定给的变量,然后按如下方式保留变量:
[systemPath autorelease];
systemPath = [aContentURL retain]
在上面的代码中,很容易看出保留/释放匹配得到了维护,并且比其他方案更不容易出错。请注意,如果
systemPath
==aContentURL
对象将不会被释放。我最终解决了它。我的设计确实很差,内存管理很差等等。我所做的不是发布我能发布的一切,包括视图、图层、玩家、资产等,而是创建了一个方法来更新玩家的资产和物品,回收玩家、视图、图层等
[player pause];
contentURL = [[NSURL alloc] initFileURLWithPath:newPath];
AVAsset *newAsset = [AVURLAsset URLAssetWithURL:contentURL options:nil];
AVPlayerItem *newPlayerItem = [AVPlayerItem playerItemWithAsset:newAsset];
[player replaceCurrentItemWithPlayerItem:newPlayerItem];
[contentURL release];
现在它正在创造奇迹。谢谢大家的帮助。所有保留的对象都将通过“dealloc”方法释放。够了吗?事实上,我以前的播放器没有'retain'关键字,但更糟糕的是,它最终导致了崩溃,因为对象在运行时的某个点被释放。如果没有看到确切的代码,我不确定。但是第一次保留似乎没有遵循标准模式。感谢Himadri的帮助,我尝试了几种不同的内存/引用计数解决方案,但它不起作用。我试过使用仪器,但有一些泄漏,我处理好了,仍然没有发现。我会继续努力的。