我想直接播放来自画廊的所有视频,但在此代码中,每次播放最后一段视频,我应该如何逐一播放所有视频
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
if (asset)
{
NSDictionary *meta = [[asset defaultRepresentation] metadata];
if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
// asset is a video
NSLog(@"See Asset: %@", asset);
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[[asset defaultRepresentation] url]];
if (self.moviePlayer != nil)
{
[self stopPlayingVideo:nil];
}
if (self.moviePlayer != nil)
{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(videoHasFinishedPlaying:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
NSLog(@"Successfully instantiated the movie player.");
/* Scale the movie player to fit the aspect ratio */
self.moviePlayer.scalingMode = MPMovieScalingModeNone;
/* Let's start playing the video in full screen mode */
self.moviePlayer.view.frame = CGRectMake(100, 50, 650, 500);
[self.moviePlayer play];
// [self.moviePlayer.view setFrame:self.view.bounds];
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:NO
animated:YES];
volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-410, 300, 140, 52)] ;
[self.view addSubview:volumeView];
CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI_2);
volumeView.transform = trans;
for (id current in volumeView.subviews) {
if ([current isKindOfClass:[UISlider class]]) {
volumeSlider = (UISlider *)current;
volumeSlider.minimumTrackTintColor = [UIColor grayColor];
volumeSlider.maximumTrackTintColor = [UIColor whiteColor];
volumeSlider.value = volumeSlider.maximumValue;
[btnVol setBackgroundImage:[UIImage imageNamed:@"mx_vol.png"] forState:UIControlStateNormal];
}
}
[self.moviePlayer setControlStyle:MPMovieControlStyleNone];
} else {
NSLog(@"Failed to instantiate the movie player.");
}
[_moviePlayer setRepeatMode:MPMovieRepeatModeOne];
}
// NSLog(@"%@",asset);
NSLog(@"%@",meta);
}
}];
}
} failureBlock:^(NSError *error) {
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
答案 0 :(得分:1)
你正在枚举区内播放视频(这是一个循环的东西,它遍历你的所有视频并在最后一个停止,这就是为什么它只播放最后一个)。
您需要先检索视频的网址:
NSMutableArray *videos = [[NSMutableArray alloc] init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group)
{
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset)
{
NSURL *videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
[videos addObject:videoURL];
}
}];
}
}
failureBlock:^(NSError *error)
{
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
现在您需要播放第一个视频并收听MPMoviePlayerPlaybackDidFinishNotification并开始播放下一个视频。