【发布时间】:2023-03-06 02:15:01
【问题描述】:
您好,我有一个不使用 Storyboard(它使用 XIB 文件)的 iOS Objective-C 应用程序
我想添加一个闪屏来播放视频,所以我添加了一个从 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];
}
屏幕启动但没有视频播放。
可能出了什么问题?
【问题讨论】:
-
你做了什么调试?
createVideoPlayer方法在运行时实际发生了什么?您分配给playerLayer.frame的实际框架是什么?有什么nil? -
我在模拟器中调试,createVideoPlayer 中没有任何内容为空。我在调试输出窗口中看到很多 AQDefaultDevice 跳过输入流 0 0 0x0 消息。
标签: ios objective-c avplayer xib