gpt4 book ai didi

ios - ios Objective C 应用程序上的 AVPlayer 不播放视频

转载 作者:行者123 更新时间:2023-11-29 11:39:10 25 4
gpt4 key购买 nike

您好,我有一个不使用 Storyboard的 iOS Objective-C 应用程序(它使用 XIB 文件)

我想添加启动画面来播放视频,所以我添加了一个派生自 UIViewController 的新 Coca Touch 类(并勾选了“同时创建 XIB 文件”)。

我已将此类替换为我的新主屏幕,它可以正确加载但无法播放视频。我已经添加了 AVFoundation 框架等,所以这不是问题。

这是我的代码。

.h文件

#import <UIKit/UIKit.h>

@interface VideoIntroViewController : UIViewController
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@end

.m 文件

import <AVFoundation/AVFoundation.h>

static const float PLAYER_VOLUME = 0.0;

@interface VideoIntroViewController ()

@property (nonatomic) AVPlayer *player;
@property (weak, nonatomic) IBOutlet UIView *playerView;

@end

@implementation VideoIntroViewController

- (void)viewDidLoad
{
[super viewDidLoad];
[self createVideoPlayer];
}

- (void)createVideoPlayer
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"welcome_video" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:filePath];

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];

self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player.volume = PLAYER_VOLUME;

AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.videoGravity = UIViewContentModeScaleToFill;
playerLayer.frame = self.playerView.layer.bounds;
[self.playerView.layer addSublayer:playerLayer];

[self.player play];
}

屏幕启动但没有播放视频。

可能出了什么问题?

最佳答案

试试这段代码:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"welcome_video" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:filePath];

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];

CALayer *superlayer = self.playerView.layer;
self.player.volume = 1.0;

AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;// you can also use AVLayerVideoGravityResizeAspect to clip video to view's bound
playerLayer.frame = self.playerView.layer.bounds;

[superlayer addSublayer:playerLayer];
[self.player seekToTime:kCMTimeZero];
[self.player play];

我检查了它是否有其他视频文件,它工作正常。

关于ios - ios Objective C 应用程序上的 AVPlayer 不播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47564063/

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