- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用水平滚动的 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];
});
}];
- (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];
}];
}
- (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.
}
}
- (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;
}
- (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;
}
#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;
}
NSDictionary *publicationObject = [videoDetailArray objectAtIndex:indexPath.row];
videoDetailsCollectionCell * cell = [self videoCellFor:collectionView withIndex:indexPath];
cell.mediaUrl = [NSString replaceEmptyStringInsteadOfNull:[NSString stringWithFormat:@"%@",publicationObject.video]];
return cell;
- (void)setPauseTheLastViewedPlayerAudio {
// Pause last played videos
if (self.lastPlayedVideo) {
[self.lastPlayedVideo mute];
[self.lastPlayedVideo stopVideoPlay];
}
_videoDetailArray = nil;
[_videoCollectionView reloadData];
}
最佳答案
前段时间我也在为同样的问题苦苦挣扎。
我解决此问题的方法与您的方法略有不同。
让我分步解释
@property (nonatomic, strong) InstagramVideoView *sharedVideoPlayer; // Share video player instance
- (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];
}
-(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];
}
}
}
}
#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
关于objective-c - 在 UICollectionView 上使用 AVPlayer 和 AVPlayerLayer 显示多个视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44603210/
我有一个 AVPlayer当我到达持续时间结束时,我想回到开始,但我不想重新启动AVPlayer . //Do something when video ended NotificationCente
我正在使用 AVPlayer 从服务器流式传输音频,我现在想做的是显示一个显示缓冲进度的自定义 UISlider。 有人想出这个吗?请给我解决方案。现在播放器一次加载整个持续时间并在 UISlider
我知道 AVPlayerItem(AVPlayer 的子类)能够订阅 AVPlayerItemDidPlayToEndTimeNotification AVPlayer 没有这样的功能,我需要使用 A
总结: 如果您只使用 AVPlayer 在您的应用程序中播放流音频,那么锁屏更新将工作得很好(许多流 - 一个接一个)。但是,如果您使用 AVPlayer 播放流式音频,然后播放本地视频文件(即使在使
我正在尝试创建像 Vine 这样的应用程序。我有一个 Collection View ,其中每个项目都包含一个带有 AVPlayerLayer 和 AVPlayer 实例的 View ,并且相应的 a
我创建了一个简单的 AVPlayer。 (如果我没有正确创建它,请帮我修复它...😀)我想用 swift ui 在我的 VStack 中显示它,但我有点卡住了... 如果库里至少有AVPlayer
我已将观察者添加到 AVPlayer,但它不会在 avplayer 状态准备好播放时调用该函数。该函数在视频开始播放时调用两次,但 AVPlayer 的持续时间参数都返回“nan”,我需要在 AVPl
我有 2 个 View Controller ,第一个包含一个视频信息列表表,然后从列表中选择一个项目,通过导航打开详细 View Controller 。我在这个细节 Controller 上使用了
关于这个主题的所有线程都非常老(5+ 哟),我似乎无法形成一个清晰的现代方法,所以希望这可以在 2020 年(Swift 5,Xcode 11)进行总结。 如果您要使用 为 iPhone 构建一个 A
我有一个 AVPlayer 可以从网络流式传输 mp3 文件。我已经激活了一个 AVAudioSession 以便在应用程序退出/屏幕关闭时播放音频。音乐播放完美,但手机顶部状态菜单中出现的“播放”三
我正在使用 Apple 的文档演示。这是演示链接: AVPlayerDemo 我的应用程序在执行以下步骤后崩溃了: 1) 播放歌曲 2) 从搜索栏快进。 3)点击下一步,4) 从搜索栏快进。 这是我的
因此看来,实际起作用的唯一值是0.0、0.5、1.0和2.0 ... 我尝试将其设置为0.25,因为我希望它以自然速度的1/4播放,但它却以自然速度的1/2播放。有人可以确认吗? 最佳答案 已确认。实
我有一个应用程序,它通过 AVMutableComposition 将本地文件加载到 avPlayer 中,它可能有多达 6 个音频和视频轨道作为合成的一部分。 我有一个 UISlider,用于调整播
在我的 IOS 应用程序中使用 AVPlayer 显示短视频的屏幕。我成功离开并重新进入此屏幕几次,但在七次或更多次后,我得到空白屏幕并且 AVPlayerLayer 的 IsReadyForDisp
我正在开发一个应用程序,其中包括使用每帧动画播放视频的功能。 您可以看到 example这样的功能。 我已经尝试添加 CAKeyFrameAnimation到 AVSynchronizedLayer
如何在 osx 自动播放中制作 avplayer? 我试过这个: NSURL *videoURL = [[NSBundle mainBundle] URLForResource:@"video nam
iOS AVPlayer 的默认最大和最小缓冲区大小是多少?如何增加 mp3 流媒体的大小?谢谢你 最佳答案 如果您的 缓冲区大小 意味着播放缓冲区,那么我相信这个大小没有记录。 此行为由 AVFou
我正在使用多个 AVPlayer 在我的屏幕上显示内容,并且它们都同时播放视频。 问题是有时 avplayer 正在播放,但 avplayerlayer 没有显示任何内容 - 你得到的只是一个空白屏幕
我正在使用 AVPlayer 在我的应用程序中播放视频,现在我想让视频在后台模式下播放。 这是我放入 - (BOOL)application:(UIApplication *)application
我在 iOS 6.1 中使用 AVPlayer 对象。我目前正在连接到 Icecast 服务器以传输现场音乐源。但是,当流由于任何原因(例如重新启动或信号更改)而中断时,AVPlayer 会停止播放。
我是一名优秀的程序员,十分优秀!