Objective-C 相当于简单的 Swift 视频播放器不显示视频

问题描述 投票:0回答:0

我正在创建一个播放 mp4 视频的简单故事板。这在 Swift 中按预期工作,但是当我尝试在 Objective-C 中做完全相同的事情时,没有任何反应。 谁能看到我在从 Swift 转换的 Objective-C 代码中做错了什么?

备注:

  • 除了视图控制器实现之外,这两个项目都是空的
  • 视频文件anim2.mp4确实包含在两个项目中
  • 由于技术原因,视频播放器必须使用 Objective-C

代码:

// Swift implementation
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    let path = Bundle.main.path(forResource: "anim2", ofType:"mp4");
    let url = NSURL(fileURLWithPath: path!) as URL;
    let player = AVPlayer(url: url);
    let playerLayer = AVPlayerLayer(player: player);
    playerLayer.frame = self.view.bounds;
    self.view.layer.addSublayer(playerLayer);
    player.play();
  }
}

// Objective-C implementation
#import "ViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  NSString* path = [[NSBundle mainBundle] pathForResource:@"anim2" ofType:@"mp4"];
  NSURL* url = [NSURL fileURLWithPath:path isDirectory:false];
  AVPlayer* player = [[AVPlayer alloc] initWithURL:url];
  AVPlayerLayer* playerLayer = [[AVPlayerLayer alloc] initWithLayer:player];
  playerLayer.frame = self.view.bounds;
  [self.view.layer addSublayer:playerLayer];
  [player play];
}
@end
swift objective-c uiviewcontroller xcode-storyboard avkit
© www.soinside.com 2019 - 2024. All rights reserved.