使用AVPlayer在iphone上播放许多不同的视频

使用AVPlayer在iphone上播放许多不同的视频,第1张

概述我正在使用AVFoundation为iOS定制视频播放器.我们的想法是能够通过手势切换到不同的视频(点按,滑动等等).现在玩家在模拟器上完美运行,但是当我在实际设备上测试时,在3或4次滑动后视图变为空白.我甚至为我的播放器创建了播放控件,当视图变为空白时,这些控件正确加载但什么都不做.有什么想法吗? 这是播放器的初始化 - (id)initWithContentURL:(NSString *)aC 我正在使用AVFoundation为iOS定制视频播放器.我们的想法是能够通过手势切换到不同的视频(点按,滑动等等).现在玩家在模拟器上完美运行,但是当我在实际设备上测试时,在3或4次滑动后视图变为空白.我甚至为我的播放器创建了播放控件,当视图变为空白时,这些控件正确加载但什么都不做.有什么想法吗?
这是播放器的初始化

- (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.这可能是可能的,但仍然,如果它有助于提供更多的光…..

编辑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上播放许多不同的视频所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1021410.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存