我正在使用AVFoundation爲iOS設置自定義視頻播放器。這個想法是能夠用手勢切換到不同的視頻(點擊,輕掃,無論)。 現在玩家在模擬器上完美地工作,但是當我在實際設備上測試它時,視圖在3或4次滑動後變爲空白。我甚至爲我的播放器創建了播放控件,當視圖變爲空白時,這些控件正確加載,但什麼都不做。任何想法傢伙? 這是玩家的初始化使用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:我運行樂器來分析核心動畫表現,當視頻播放時顯示的幀速率約爲30 fps,當播放器變爲空白時,它一路下降到1或2 fps。這可能是ovibious,但仍然,如果它有助於給更多的光.....
編輯3:好吧,我終於到了某個地方,我知道什麼問題是,我有一個核心動畫內存泄漏,在模擬器中它「起作用」,因爲電腦比iPad有更多的內存,但是由於iPad內存非常有限,它會很快停止工作。如果任何人有任何關於核心動畫泄漏的建議,它將會很受歡迎。
所有保留的對象都在'dealloc'方法中釋放。這足夠嗎?事實上,我曾經讓玩家沒有'保留'關鍵字,但它更糟糕,最終導致崩潰,因爲對象在運行時的某個時刻被釋放。 – Samssonart 2011-06-09 23:34:53
不確定沒有看到確切的代碼。但第一次保留似乎並不遵循標準模式。 – 2011-06-09 23:47:40
感謝您的幫助Himadri,我已經嘗試了一些不同的內存/參考計數解決方案,但它不起作用。我嘗試過使用儀器,有幾次泄漏,我照顧它們,而且什麼也沒有。我會繼續嘗試。 – Samssonart 2011-06-10 15:47:32