在阅读了一些这样的问题和答案之后,这个,或者这个只是提到一些,我一直在尝试创建一个带有 4 个 UIViews 的 UIScrollView。
在这些 UIView 中,我正在创建一个 MPMoviePlayerController。
苹果的文档明确指出:
Note: Although you may create multiple MPMoviePlayerController objects and present
their views in your interface, only one movie player at a time may play its movie.
因此,当用户滚动时,我会暂停并播放存储在 playerArray 中的不同 MPMoviePlayerController。我这样做是从 UIScrollViewDelegate 协议调用两个方法:
scrollViewWillBeginDragging: - 使用此方法,我检测到用户正在滚动到另一个视图,因此我暂停当前的 MPMoviePlayerController 并插入带有电影当前播放时间的缩略图。
scrollViewDidEndDecelerating: - 在这里,我得到了用户停止滚动的视图,所以首先我隐藏缩略图,然后从该特定视图播放 MPMoviePlayerController
以下是我的实现方式:
我的.h:
@interface ViewController : UIViewController<UIScrollViewDelegate,UIGestureRecognizerDelegate>
@property(strong,nonatomic) NSMutableArray * viewsArray;
@property(strong,nonatomic) UIScrollView * myScrollView;
@property int numberOfViews;
@property(strong,nonatomic) NSMutableArray * playersArray;
@end
我的viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
//The number of views inside myScrollView
numberOfViews = 4;
//Initialization
viewsArray = [[NSMutableArray alloc] init];
myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
playersArray = [[NSMutableArray alloc] init];
NSMutableArray * videoNamesArray = [[NSMutableArray alloc] initWithObjects:@"field4min",@"water4min",@"beach4min",@"bushtails4min", nil];
//Customization
myScrollView.delaysContentTouches = NO;
myScrollView.contentSize = CGSizeMake(myScrollView.frame.size.width * numberOfViews, myScrollView.frame.size.height);
myScrollView.delegate = self;
myScrollView.showsHorizontalScrollIndicator = YES;
myScrollView.pagingEnabled = YES;
myScrollView.userInteractionEnabled = YES;
myScrollView.bounces = NO;
myScrollView.showsHorizontalScrollIndicator = NO;
//Create the views that will populate the scrollView
for (int i = 0; i < numberOfViews; i++) {
UIView *cView = [[UIView alloc] init];
//Set the frame for each view inside the ScrollView
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height);
frame.origin.x = frame.size.width * i;
cView.frame = frame;
//Set the path of the video to the MPMoviePlayerController
NSString * videoPath = [videoNamesArray objectAtIndex:i];
NSString *path = [[NSBundle mainBundle] pathForResource:videoPath ofType:@"mp4"];
MPMoviePlayerController * player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
//Customization
player.fullscreen = NO;
player.scalingMode = MPMovieScalingModeAspectFill;
[player.view setFrame:self.view.bounds];
player.controlStyle = MPMovieControlStyleNone;
player.shouldAutoplay = NO;
player.view.clipsToBounds = YES;
[player prepareToPlay];
[player stop];
//Get the image from the currentPlaybackTime to set it above the player
UIImage *thumbnail = [player thumbnailImageAtTime:[player currentPlaybackTime] timeOption:MPMovieTimeOptionNearestKeyFrame];
UIImageView *thumbnailView = [[UIImageView alloc] initWithFrame:player.view.frame];
[thumbnailView setImage:thumbnail];
[thumbnailView setContentMode:UIViewContentModeScaleAspectFill];
thumbnailView.clipsToBounds = YES;
//Add both views (player.view, and ImageView) to the currentView
[cView addSubview:player.view];
[cView addSubview:thumbnailView];
//Add the view to myScrollView
[myScrollView addSubview:cView];
//Add the player and the currentView to arrays for further manipulation
[playersArray addObject:player];
[viewsArray addObject:cView];
}
//add the ScrollView to the ViewController's view
[self.view addSubview:myScrollView];
}
我的scrollViewWillBeginDragging:
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//Get the view (page) that is currently showing in the screen, inside the scrollView
static NSInteger previousPage = 0;
CGFloat pageWidth = scrollView.frame.size.width;
float fractionalPage = scrollView.contentOffset.x / pageWidth;
int page = lround(fractionalPage);
NSLog(@"page: %i",page);
previousPage = page;
//Get the current player from playerArray
MPMoviePlayerController * startPlayer = [playersArray objectAtIndex:page];
//Pause the player;
[startPlayer pause];
//Get the thumbnail from the currentPlaybackTime
UIImage *thumbnail = [startPlayer thumbnailImageAtTime:[startPlayer currentPlaybackTime] timeOption:MPMovieTimeOptionExact];
UIImageView *thumbnailView = [[UIImageView alloc] initWithFrame:startPlayer.view.frame];
[thumbnailView setImage:thumbnail];
//Customization
[thumbnailView setContentMode:UIViewContentModeScaleAspectFill];
thumbnailView.clipsToBounds = YES;
//Get the second view (thumbnail) from the view.subviews array and unhide it (I hide it in scrollViewDidEndDecelerating method)
UIView * startView = [viewsArray objectAtIndex:page];
UIView * imagestartView = [startView.subviews objectAtIndex:1];
imagestartView.hidden = NO;
imagestartView = thumbnailView;
}
我的scrollViewDidEndDecelerating:
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//Get the view (page) that is currently showing in the screen, inside the scrollView
static NSInteger previousPage = 0;
CGFloat pageWidth = scrollView.frame.size.width;
float fractionalPage = scrollView.contentOffset.x / pageWidth;
int page = lround(fractionalPage);
NSLog(@"pageend: %i",page);
previousPage = page;
//Get the current player from playerArray and play it
MPMoviePlayerController * endPlayer = [playersArray objectAtIndex:page];
[endPlayer play];
//Get the second view (thumbnail) from the view.subviews array and hide it
UIView * endView = [viewsArray objectAtIndex:page];
UIView * imageEndView = [endView.subviews objectAtIndex:1];
imageEndView.hidden = YES;
}
我的问题:
当我在我的 iOS 6 模拟器中运行它时,它按预期工作,但是一旦我在我的设备(带有 iOS 5.1 的 iPhone)上运行它,中间视图(我的 viewsArray 中的索引 1 和 2),不显示任何视频,只有声音...
我错过了什么?
任何帮助将不胜感激。