Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在滚动条上添加视频?_Iphone_Ios_Xcode_Ios6 - Fatal编程技术网

Iphone 如何在滚动条上添加视频?

Iphone 如何在滚动条上添加视频?,iphone,ios,xcode,ios6,Iphone,Ios,Xcode,Ios6,我正在开发一个视频管理应用程序。我在滚动视图中添加视频 除了黑屏,什么也看不见。此代码工作正常,仅在滚动条上添加黑色视图,内容为显示 如果有人有任何想法,请提供给我 (void)LoadVideosToScrollView { NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory=

我正在开发一个视频管理应用程序。我在滚动视图中添加视频

除了黑屏,什么也看不见。此代码工作正常,仅在滚动条上添加黑色视图,内容为显示

如果有人有任何想法,请提供给我

(void)LoadVideosToScrollView
{    
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory=[paths objectAtIndex:0];
    NSString *videoDirectory=[documentDirectory stringByAppendingPathComponent:@"/videos"];
    NSFileManager *fileManger=[NSFileManager defaultManager];
    NSDirectoryEnumerator *docEnum=[fileManger enumeratorAtPath:videoDirectory];
    MPMoviePlayerController *localMoviePlayer;
    CGFloat xAxis=10;
    NSInteger n=0;
    NSString *fileName;

    [self.scrollView setContentSize:CGSizeMake(560, 200)];
    while ((fileName=[docEnum nextObject]))
    {
        NSString *filePath=[videoDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",fileName]];
        //if file exist then file will be added on ScrollView
        if ([fileManger fileExistsAtPath:filePath])
        {
            NSURL *urlForFile=[NSURL fileURLWithPath:filePath];
            NSLog(@"%@",[urlForFile description]);
            localMoviePlayer=[[MPMoviePlayerController alloc]initWithContentURL:urlForFile];
            localMoviePlayer.scalingMode=MPMovieScalingModeAspectFit;
            localMoviePlayer.shouldAutoplay=NO;
            localMoviePlayer.controlStyle=MPMovieControlStyleNone;
            [localMoviePlayer prepareToPlay];
            tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
            [localMoviePlayer.view addGestureRecognizer:tap];
            [localMoviePlayer.view setTag:n];
            [localMoviePlayer.view setFrame:CGRectMake(xAxis,5,150,120)];
            [self.scrollView addSubview:localMoviePlayer.view];
            [moviePlayerContainer addObject:urlForFile];
            xAxis+=155;
            n++;
        }
    }
}

我已经开发了这段代码。

您需要使用
AVPlayer
来代替
MPMoviePlayerController
,就像使用
AVPlayer
一样,您可以将视频渲染到特定的源代码视图-在您的例子中是滚动视图


祝你好运

你没有调用play method

// Plays items from the current queue, resuming paused playback if possible.
- (void)play;

请尝试下一个代码

 _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];

[[_moviePlayer view] setFrame:[[self view] bounds]];
[[self view] addSubview: [_moviePlayer view]];

_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

[_moviePlayer play];

如果您在一个scrollview上有许多视频,您将在memmory管理方面遇到问题,为了优化视频列表的显示过程,您可以使用UITableView并将视频放在每个单元格上,因为UITableView已经为其单元格设置了自己的内存管理器