Objective-C版本的简单Swift视频播放器不显示视频。

huangapple go评论60阅读模式
英文:

Objective-C equivalent of simple Swift video player does not show video

问题

我正在创建一个简单的故事板,用于播放一个mp4视频。在Swift中,这个代码工作得很正常,但当我尝试在Objective-C中做完全相同的事情时,什么都不会发生。有人能看看我从Swift转换成Objective-C的代码中是否有什么问题吗?

注:

  • 两个项目都只有视图控制器的实现
  • 视频文件anim2.mp4确实包含在两个项目中
  • 出于技术原因,视频播放器必须使用Objective-C

以下是代码的翻译部分:

// Swift implementation
#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController
@end

@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  NSString* path = [[NSBundle mainBundle] pathForResource:@"anim2" ofType:@"mp4"];
  NSURL* url = [NSURL fileURLWithPath:path isDirectory:NO];
  AVPlayer* player = [[AVPlayer alloc] initWithURL:url];
  AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
  playerLayer.frame = self.view.bounds;
  [self.view.layer addSublayer:playerLayer];
  [player play];
}
@end

请注意,代码的翻译部分已经从Swift转换为Objective-C,并修复了一些可能引起问题的语法。如果仍然有问题,请检查您的项目设置和文件路径等方面,确保视频文件正确包含在Objective-C项目中。

英文:

I'm creating a simple storyboard that plays an mp4 video. This works as expected in Swift, but when I try to do the exact same thing in Objective-C nothing happens.
Can anyone see if I'm doing anything wrong in the Objective-C code converted from Swift?

Notes:

  • Both projects are empty except for the view controller implementation
  • The video file anim2.mp4 is indeed included in both projects
  • For technical reasons, the video player must utilize Objective-C

Code:

// Swift implementation
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    let path = Bundle.main.path(forResource: &quot;anim2&quot;, ofType:&quot;mp4&quot;);
    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 &quot;ViewController.h&quot;
#import &lt;AVKit/AVKit.h&gt;
#import &lt;AVFoundation/AVFoundation.h&gt;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  NSString* path = [[NSBundle mainBundle] pathForResource:@&quot;anim2&quot; ofType:@&quot;mp4&quot;];
  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

答案1

得分: 1

这行代码不同:

AVPlayerLayer* playerLayer = [[AVPlayerLayer alloc] initWithLayer:player];

这试图将AVPlayer视为CALayer,这将悄悄失败。您在这里没有警告,因为initWithLayer:id作为其类型。

您的意思是:

AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer: player];
英文:

This line is not the same:

AVPlayerLayer* playerLayer = [[AVPlayerLayer alloc] initWithLayer:player];

That tries to treat an AVPlayer as a CALayer, which is going to quietly fail. You don't get a warning here because initWithLayer: takes id as its type.

What you meant was:

AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer: player];

huangapple
  • 本文由 发表于 2023年2月18日 23:53:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定