我需要你的帮助。我想ScrollView
用一些AVPlayers
. 我ScrollView
用一些图片试过这个,它有效。现在的问题是我可以滚动View
,但是所有的AVPlayerViews
都在同一个位置,所以我只能看到最后加载的视频,而所有其他滚动位置都是空白的。
这是我的课程的来源:
@implementation VPVideoScroller
const CGFloat kScrollObjHeight = 748.0;
const CGFloat kScrollObjWidth = 1024.0;
NSUInteger numImages = 0;
- (void)viewDidLoad
{
numImages = _pictureArray.count;
[super viewDidLoad];
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
// 1. setup the scrollview for multiple images and add it to the view controller
self.scrollView.clipsToBounds = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= numImages; i++)
{
if (i<3) {
/*
//------------
MPMediaItem *item = [self.pictureArray objectAtIndex:(i-1)];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
*/
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:self.currentURL];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
//------------
UIView *imageView = [[UIView alloc] init];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
[player play];
/*
UIImage *image = [self.pictureArray objectAtIndex:(i-1)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
*/
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i; // tag our images for later use when we place them in serial fashion
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imageView.layer addSublayer:playerLayer];
playerLayer.frame = imageView.layer.bounds;
[self.scrollView addSubview:imageView];
}
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
}
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [self.scrollView subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
//[self.scrollView setContentSize:CGSizeMake((numImages * kScrollObjWidth), [self.scrollView bounds].size.height)];
[self.scrollView setContentSize:CGSizeMake((numImages * kScrollObjWidth), kScrollObjHeight)];
}
pictureArray
包含一个Array
URL,但它们都是相同的。所以AVPlayers
应该显示所有相同的视频。
先感谢您。凯文