gpt4 book ai didi

objective-c - 如何在 Objective-C 中播放视频 - 加载时导致 Jerk

转载 作者:行者123 更新时间:2023-12-01 17:35:15 25 4
gpt4 key购买 nike

我正在开发一个在应用程序启动时加载视频的应用程序。当视频播放并从屏幕上删除时,我观察到一个巨大的 SCSS 。

我的代码如下:

视频播放器.m

-(id)initWithVideo: (CGRect)frame file:(NSString *)videoFile 
{
if (self = [super initWithFrame:frame])
{
NSArray *file = [videoFile componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];

NSString *moviePath = [[NSBundle mainBundle] pathForResource:[file objectAtIndex:0] ofType:[file objectAtIndex:1]];

if (nil != moviePath)
{
if (nil != theMovie)
{
[theMovie.view removeFromSuperview];
}

theMovie = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];

theMovie.view.frame = self.bounds;
theMovie.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
theMovie.moviePlayer.controlStyle = MPMovieControlStyleNone;
theMovie.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
theMovie.moviePlayer.fullscreen = NO;

[self addSubview:theMovie.view];
}
}

return self;
}

请指导我在哪里错过它。

最佳答案

-(void)prepareMoviePlayer:(NSURL *)moviePath {
self.moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:moviePath];
if ([[self.moviePlayerViewController moviePlayer] respondsToSelector:@selector(loadState)]) {
[[self.moviePlayerViewController moviePlayer] setControlStyle:MPMovieControlStyleEmbedded];
[[self.moviePlayerViewController moviePlayer] setFullscreen:NO];
[[self.moviePlayerViewController moviePlayer] prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateDidChange:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}


- (void)moviePlayerLoadStateDidChange:(NSNotification *)notification {
if ([[self.moviePlayerViewController moviePlayer] loadState] == MPMovieLoadStateStalled) {
NSLog(@"Movie Playback Stalled");
} else if([[self.moviePlayerViewController moviePlayer] loadState] != MPMovieLoadStateUnknown) {

[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];

[[[self.moviePlayerViewController moviePlayer] view] setFrame:self.view.bounds];
[[self view] addSubview:[[self.moviePlayerViewController moviePlayer] view]];
[[self.moviePlayerViewController moviePlayer] play];
}
}

NSMoviePlayerViewController prepareToPlay 方法似乎是这里的关键。我还建议添加其他通知来处理播放完成和播放停止。此外,其中一些代码旨在从窗口的 subview 中工作。

关于objective-c - 如何在 Objective-C 中播放视频 - 加载时导致 Jerk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5140187/

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