gpt4 book ai didi

ios - 如何: Local MP4 Video as Background in iOS App Objective C iOS 8

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

我尝试使用一个简单的 13 秒 mp4 视频作为登录屏幕的背景循环。

我希望视频自动播放和循环播放。它没有音频,我不需要控件。

我需要在它前面放置按钮和其他对象。

我尝试使用 WebView 并将 MP4 制作为本教程中的 GIF 文件:https://medium.com/swift-programming/ios-make-an-awesome-video-background-view-objective-c-swift-318e1d71d0a2

但问题是我的 5 MB MP4(转换为 GIF)大小为 95 MB。

我不能使用这种方法。

有什么“好用”的方法吗?

编辑:

好的,这就是我现在所做的。

我导入了 AVFoundation。

这是 View 中的代码

- (void)viewDidLoad {
[super viewDidLoad];

NSURL* mURL = [[NSBundle mainBundle] URLForResource:@"App-BG-Loop" withExtension:@"mp4"];

AVPlayer* player = [AVPlayer playerWithURL:mURL];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[player currentItem]];

AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = _videoView.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
playerLayer.needsDisplayOnBoundsChange = YES;
[_videoView.layer addSublayer:playerLayer];

_videoView.layer.needsDisplayOnBoundsChange = YES;

[player play];

// Do any additional setup after loading the view, typically from a nib.}

- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];}

这在 iOS 模拟器中运行良好,但当我尝试在设备上启动它时,它无法启动或播放视频。 View 只是保持白色。没有错误。

有什么想法吗?

编辑 2:

无法在设备上播放的问题是视频的大小太大。 Apple 在设备上仅支持 1080p。

最佳答案

我最近也不得不使用动画。我先用 UIImageView 尝试过,但内存管理不太好。我最终从中制作了一个 .mp4 并在 MPMoviePlayerController 中使用它:

-(void)setupDashboardAnimation
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkMovieStatus:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];

NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"some_movie" ofType:@"some_extention"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

_backgroundAnimationMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
_backgroundAnimationMoviePlayer.controlStyle = MPMovieControlStyleNone;
_backgroundAnimationMoviePlayer.repeatMode = MPMovieRepeatModeOne;
_backgroundAnimationMoviePlayer.view.backgroundColor = [UIColor clearColor];
for (UIView *aSubView in _backgroundAnimationMoviePlayer.view.subviews)
{
aSubView.backgroundColor = [UIColor clearColor];
}
[_backgroundAnimationMoviePlayer.view setFrame:imv_background.frame];
[_backgroundAnimationMoviePlayer prepareToPlay];
}

-(void)checkMovieStatus:(NSNotification *)notification
{
if (_backgroundAnimationMoviePlayer.loadState & (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK) && _backgroundAnimationMoviePlayer.playbackState != MPMoviePlaybackStatePlaying)
{
[self.view insertSubview:_backgroundAnimationMoviePlayer.view aboveSubview:imv_background];
[_backgroundAnimationMoviePlayer play];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
dashboardAnimationDidSetup = YES;
}
}

关于ios - 如何: Local MP4 Video as Background in iOS App Objective C iOS 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31005491/

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