gpt4 book ai didi

ios - XCode - 让视频在 Vine 和 Instagram 等表格 View 单元格中播放

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:03 25 4
gpt4 key购买 nike

我正在尝试让一个 vine 类型的视频提要在 tableview 中工作。我希望视频在 tableview 单元格中一次播放一个,就像 Vine 和 Instagram 一样。

有人知道怎么做吗?我正在使用 MPMoviePlayer。

谢谢

----更新----

当每个表格单元格位于中心时,我的自动播放号召性用语运行良好(请参阅“播放这个”),但我无法让多个 MPMoviePlayer 加载/停止/播放.

我只需要一个视频在任何时候在其单元格中播放,但我似乎只能在第一个单元格中播放视频并且它没有响应停止调用(请参阅“不要播放这个” ').

下面是我的脚本片段。唯一不起作用的部分是让视频实际播放和停止。播放和停止的调用工作正常。

我的 CellForRow 中有这个:

MPMoviePlayerViewController *videoPlayer;

[videoPlayer.view removeFromSuperview];

videoPlayer = [[MPMoviePlayerViewController alloc] init];


int videoTag = 500+indexPath.row;

[videoPlayer.view setTag:videoTag];

videoPlayer.moviePlayer.repeatMode = MPMovieRepeatModeOne;

videoPlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;

videoPlayer.view.frame = CGRectMake( 0, 0, 320, 320);

[videoPlayer.view setBackgroundColor:[UIColor whiteColor]];

[playerView addSubview:videoPlayer.view];

我这样做是为了调用 View 中心的正确单元格:

- (void)scrollViewDidScroll:(UIScrollView *)theView{

UITableViewCell *cell = (UITableViewCell *) theView;


CGPoint currentOffset = theView.contentOffset;


for (UITableViewCell *cell in [videoTableView visibleCells]) {

NSIndexPath *indexPath = [videoTableView indexPathForCell:cell];

int videoTag = 500+indexPath.row;

MPMoviePlayerViewController *videoPlayer = (MPMoviePlayerViewController *)[cell viewWithTag:videoTag];

UILabel *test_label = (UILabel *)[cell viewWithTag:300];

CGRect cellRect = [videoTableView convertRect:cell.bounds fromView:cell];



int scrollPosition = currentOffset.y + 64; // 64px is the navigation bar height

int cellPosition = cellRect.origin.y;



if( scrollPosition > cellPosition-100 && scrollPosition < cellPosition+100 )
{
test_label.text = [NSString stringWithFormat:@"Play this one" ];



UIView *blankOut = (UIView *)[cell viewWithTag:200];

UIView *playerView = (UIView *)[cell viewWithTag:201];

UIImageView *videoCoverUp = (UIImageView *)[cell viewWithTag:202];


[playerView.layer setCornerRadius:138];
[playerView.layer setMasksToBounds:YES];


if(_isPlaying!=true){

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"video.mp4"];

NSData *video_data = _tableObjects[indexPath.row][9];// Where my video data is stored

[video_data writeToFile:path atomically:YES];

NSURL *moveUrl = [NSURL fileURLWithPath:path];


videoPlayer.moviePlayer.contentURL = moveUrl;

[videoPlayer.moviePlayer prepareToPlay];

[videoPlayer.moviePlayer play];


_isPlaying = true;

// Needed
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[self setNeedsStatusBarAppearanceUpdate];

}




} else {

test_label.text = [NSString stringWithFormat:@"Don't play this one" ];

[videoPlayer.moviePlayer stop];
[videoPlayer.moviePlayer setContentURL:[NSURL URLWithString:nil]];
[videoPlayer.moviePlayer prepareToPlay];

//[_videoPlayer.moviePlayer.view removeFromSuperview];

_isPlaying = false;

}


}
}

最佳答案

MPMoviePlayer 手册似乎描述了该怎么做?

https://developer.apple.com/library/prerelease/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html

它会在顶部向您展示要做什么。似乎您只需要一个带有 View 的单元格即可将电影播放器​​添加到其中。

MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player prepareToPlay];
[player.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];

根据评论,谷歌是你的 friend 。那里一定有很多例子。

关于ios - XCode - 让视频在 Vine 和 Instagram 等表格 View 单元格中播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28697338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com