gpt4 book ai didi

objective-c - 在 UICollectionView 上使用 AVPlayer 和 AVPlayerLayer 显示多个视频

转载 作者:行者123 更新时间:2023-12-03 16:15:05 26 4
gpt4 key购买 nike

我正在使用水平滚动的 UICollectionView Cell 上的 AVPlayer 和 AVPlayerLayer 类在全屏上显示“n”个视频的应用程序。我在 UIScrollView 上设置了分页。因为我只需要播放当前位置的播放器,否则“上一个和下一个”单元格播放器应该处于“暂停”状态。

我正在获取 url 格式的视频。所以最初我需要下载,然后它就会开始播放。所以每个人都不需要从 URL 下载视频。所以我决定将下载的视频缓存在本地。所以我下载并将其存储在本地文件中。然后我从本地加载视频,如果它存在于本地文件中。

问题 :

(一种)。 “播放和暂停”在其相关索引上无法正常工作。最初播放索引为“0”。当我此时滚动到下一个索引时,当前索引播放器应该正在播放,下一个索引播放器应该处于“暂停”状态。它没有正确同步。

(b)。我可以看到上一个播放器的视频剪辑,直到下一个播放器开始播放。

(C)。所有查看的音频同时在后台播放,也可以在释放播放器时播放。 (我的意思是,我可以在关闭一些 View 或 View Controller 后听到)

(d) 需要维护内存泄漏问题。

下面是我尝试的来源方式,

- (void)setVideoLoading:(NSString *)videoUrl{

NSString *urlString = videoUrl;

SHAREDATA.videoUrl = [NSURL URLWithString:urlString];

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:SHAREDATA.videoUrl options:nil];

NSArray *requestedKeys = @[@"playable"];

//Tells the asset to load the values of any of the specified keys that are not already loaded.
[asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler:^{

dispatch_async(dispatch_get_main_queue(), ^{

AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];

if (self.avPlayer)
{
/* Remove existing player item key value observers and notifications. */

[self.avPlayer removeObserver:self forKeyPath:@"status" context:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.avPlayer.currentItem];
}

self.avPlayer = [AVPlayer playerWithPlayerItem:item];

self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];

if (_typeOfContentMode == UIViewContentModeScaleAspectFit) {

self.avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
}
else if (_typeOfContentMode == UIViewContentModeScaleAspectFill)
{
self.avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
}


self.avPlayerLayer.frame = CGRectMake(0, 0, _videoBackgroundView.frame.size.width, _videoBackgroundView.frame.size.height);

[_videoBackgroundView.layer addSublayer:self.avPlayerLayer];

self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

self.avPlayer.muted = NO;

[self.avPlayer addObserver:self
forKeyPath:@"status"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:nil];
});
}];

下面是KVO方法。
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {

if (object == self.avPlayer && [keyPath isEqualToString:@"status"]) {

if (self.avPlayer.status == AVPlayerStatusReadyToPlay) {

[_videoProgressView setProgress:0 animated:NO];

[self setVideoPlayEnabled];

[self setAutoUpdateTimer];

[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avPlayer currentItem]];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avPlayer currentItem]];
}
else if (self.avPlayer.status == AVPlayerStatusFailed ||
self.avPlayer.status == AVPlayerStatusUnknown) {

[SVProgressHUD dismiss];

if (self.avPlayer.rate>0 && !self.avPlayer.error)
{
[self.avPlayer setRate:0.0];

[self.avPlayer pause];
}
}
}
}

- (void)playerItemDidReachEnd:(NSNotification *)notification {

[SVProgressHUD dismiss];

AVPlayerItem *p = [notification object];

[p seekToTime:kCMTimeZero completionHandler:^(BOOL finished)
{
[self.avPlayer play];
}];
}

我从“cellForItemAtIndexPath”传递url,我在“didEndDisplayingCell”上暂停当前播放器。

然后在“scrollViewDidEndDecelerating”中,我刚刚检查了我的 customCell 类的其他全局实例(在全局变量上声明)并暂停该单元格当前播放器。

但是对于 scrollViewDidEndDecelerating 的主要目的,我正在获取当前可见的单元格。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if (self.lastPlayingCell) {

[self.lastPlayingCell.avPlayer pause];
}

// Play/Pause Video
CGRect visibleRect = (CGRect){.origin = self.videoCollectionView.contentOffset, .size = self.videoCollectionView.bounds.size};

CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));

_visibleIndexPath = [self.videoCollectionView indexPathForItemAtPoint:visiblePoint];

DetailsCollectionCell *cell = (DetailsCollectionCell *)[self.videoCollectionView cellForItemAtIndexPath:_visibleIndexPath];

if (![cell isEqual: self.lastPlayingCell]) {

[self.avPlayer play];

self.lastPlayingCell = cell;//assigned to current visible cell to lastplayingcell instance.
}
}

我在这里通知的一件事,我只是将 AVPlayer 显示为 UIView 类的 subview 。当我点击视频拇指图像(UITableView 上已发布的视频信息列表在它的上一个 View 上)我的意思是 UITable didselect 功能,我只是显示 UIView 移动它的“Y”位置值(就像呈现 View Controller 一样)。

所以我可以像在“Facebook Messenger 应用程序相机访问 View ”中那样移动 View “上下”方向。

因此,当我关闭 View 时,我需要释放 AVPlayer,停止所有观看视频的背景上的音频并删除注册的观察者类,如下所示。
- (void)setRemoveRegisteredObserversAndPlayerRelatedElements
{
[self.avPlayer removeObserver:self forKeyPath:@"status" context:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avPlayer currentItem]];

[self.avPlayerLayer.player pause];

[self.avPlayer pause];

self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@""]];

[self.avPlayerLayer removeFromSuperlayer];

self.avPlayerLayer.player = nil;

self.avPlayerLayer = nil;

self.avPlayer = nil;
}

解决这些问题并在当前索引上流畅播放视频的任何想法,停止音频并处理内存泄漏。

任何人都可以分享或提出一些建议来实现这一目标。

cellForItemAtIndexPath
- (UICollectionViewCell *) collectionView: (UICollectionView *) collectionView cellForItemAtIndexPath: (NSIndexPath *) indexPath
{
videoDetailsCollectionCell *videoDetailCell = [collectionView dequeueReusableCellWithReuseIdentifier:kVideoDetailsCollectionCell forIndexPath:indexPath];

NSDictionary *publishedVideoObject = [self.videoDetailArray objectAtIndex:indexPath.row];

[videoDetailCell loadVideoURL:publishedVideoObject];

return videoDetailCell;
}

我已经添加了 ZOWVideoPlayer 库文件中的所有类文件。

最新编辑:
#pragma mark - Load video url for their current index cell
- (videoDetailsCollectionCell *)videoCellFor:(UICollectionView *)collectionView withIndex:(NSIndexPath *)indexPath
{
videoDetailsCollectionCell * videoCell = [collectionView dequeueReusableCellWithReuseIdentifier:kCustomVideoCell forIndexPath:indexPath];

//[videoCell.videoView.videoPlayer resume];

return videoCell;
}

**在 CellForItemAtIndexPath 方法中 **
 NSDictionary *publicationObject = [videoDetailArray objectAtIndex:indexPath.row];

videoDetailsCollectionCell * cell = [self videoCellFor:collectionView withIndex:indexPath];

cell.mediaUrl = [NSString replaceEmptyStringInsteadOfNull:[NSString stringWithFormat:@"%@",publicationObject.video]];

return cell;

然后在 scrollViewDidScroll 我是 暂停上次播放的视频 正如你之前提到的。

然后在 scrollViewDidEndDecelerating 除了使用 之外,我遵循以下答案部分UICollectionView 而不是 索引集合 View 用于检索页数信息的类。

最后我停止了所有播放的视频,如下所示,
  - (void)setPauseTheLastViewedPlayerAudio {

// Pause last played videos
if (self.lastPlayedVideo) {

[self.lastPlayedVideo mute];

[self.lastPlayedVideo stopVideoPlay];
}

_videoDetailArray = nil;

[_videoCollectionView reloadData];
}

因为我应该使用 关闭用手指从上到下移动的播放器 View UIPanGestureRecognizer .

最佳答案

前段时间我也在为同样的问题苦苦挣扎。
我解决此问题的方法与您的方法略有不同。

让我分步解释

  • 我已经在包含当前视频单元播放器实例的 View Controller 中声明了一个播放器的全局实例。
    @property (nonatomic, strong) InstagramVideoView *sharedVideoPlayer; // Share video player instance
  • 视频播放器对象是为每个单元格创建的,它们还保存每个视频播放器的视频 URL。
    - (CommonVideoCollectionViewCell *)videoCellFor:(UICollectionView *)collectionView withIndex:(NSIndexPath *)indexPath {
    CommonVideoCollectionViewCell *videoCell = [collectionView dequeueReusableCellWithReuseIdentifier:VideoCollectionViewCellIdentifier forIndexPath:indexPath];
    return videoCell;
    }
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    CommonVideoCollectionViewCell *cell = [self videoCellFor:collectionView withIndex:indexPath];
    cell.mediaUrl = tempMedia.media;
    return cell;
    }
  • 每次用户尝试滚动集合单元格时,都会暂停视频播放器全局实例中的视频。这解决了单元格的闪烁问题。
    // Pause last played videos
    if (self.sharedVideoPlayer) {
    [self.sharedVideoPlayer pause];
    [self.sharedVideoPlayer mute];
    }
  • 最后一步是在水平集合 View 的中心找到当前视频单元并从我们保存在当前视频单元中的 URL 播放视频。
    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if ([scrollView isKindOfClass:[UITableView class]]) {
    return;
    }

    IndexedCollectionView *pageContentCollectionView = (IndexedCollectionView *)scrollView;
    CGPoint centerPoint = CGPointMake(pageContentCollectionView.center.x + pageContentCollectionView.contentOffset.x,
    pageContentCollectionView.center.y + pageContentCollectionView.contentOffset.y);

    NSIndexPath *centerCellIndexPath = [pageContentCollectionView indexPathForItemAtPoint:centerPoint];
    NSArray *visibleCells = [pageContentCollectionView visibleCells];
    // Get cell for index & pause video playback
    UICollectionViewCell *cell = [pageContentCollectionView cellForItemAtIndexPath:centerCellIndexPath];

    for (UICollectionViewCell *tempCell in visibleCells) {
    if ([tempCell isKindOfClass:[CommonVideoCollectionViewCell class]]) {
    CommonVideoCollectionViewCell *videoCell = (CommonVideoCollectionViewCell *)tempCell;
    InstagramVideoView *videoPlayer = [videoCell videoView];
    // Pause & mute all the video player instance
    [videoCell.videoView setHidden:YES];
    [videoCell.placeholderImageView setHidden:NO];
    [videoPlayer pause];
    [videoPlayer mute];
    // Check if the central cell is present
    if (tempCell == cell) {
    self.sharedVideoPlayer = videoPlayer;// Save played video player instance
    [self.sharedVideoPlayer playVideoWithURL:[NSURL URLWithString:[(CommonVideoCollectionViewCell *)tempCell mediaUrl]]];
    [videoCell.videoView setHidden:NO];
    [videoCell.placeholderImageView setHidden:YES];
    [videoPlayer resume]; // resume video playback
    [videoPlayer mute];
    }
    }
    }
    }

    CommonVideoCollectionViewCell
    #import<UIKit/UIKit.h>
    #import "InstagramVideoView.h"
    @interface CommonVideoCollectionViewCell : UICollectionViewCell
    @property (weak, nonatomic) IBOutlet InstagramVideoView *videoView;
    @property (weak, nonatomic) IBOutlet UIImageView *placeholderImageView;
    // Set media url
    @property (strong, nonatomic) NSString *mediaUrl;
    @end

  • 最终结果 -
    enter image description here

    注意 - 为了流畅的视频播放,播放器将视频缓存在临时内存中。如果您在以下任何步骤中遇到问题,请告诉我。

    关于objective-c - 在 UICollectionView 上使用 AVPlayer 和 AVPlayerLayer 显示多个视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44603210/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com