大佬教程收集整理的这篇文章主要介绍了使用AVPlayer在iphone上播放许多不同的视频,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
- (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]; }
编辑:尝试使用不同的AVPlayerLayers为每个视频文件,相同的情况,在模拟器中工作,而不是在iPad上.
编辑2:我运行仪器分析核心动画性能,当视频正在播放时,它@L_673_10@大约30 fps的帧速率,当播放器空白时,它一直下降到1或2 fps.这可能是可能的,但仍然,如果它有助于提供更多的光…..
编辑3:好的,我终于到了某个地方,我知道问题是什么,我有一个核心动画内存泄漏,在模拟器中它“工作”,因为计算机比iPad有更多的内存,但自iPad内存非常有限,它很快停止工作.如果有人对Core Animation漏洞有任何建议,那将会非常受欢迎.
[player pause]; contentURL = [[NSURL alloc] initFileURLWithPath:newPath]; AVAsset *newAsset = [AVURLAsset URLAssetWithURL:contentURL options:nil]; AVPlayerItem *newPlayerItem = [AVPlayerItem playerItemWithAsset:newAsset]; [player replaceCurrentItemWithPlayerItem:newPlayerItem]; [contentURL release];
而现在它正在创造奇迹.感谢大家的帮助.
以上是大佬教程为你收集整理的使用AVPlayer在iphone上播放许多不同的视频全部内容,希望文章能够帮你解决使用AVPlayer在iphone上播放许多不同的视频所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。