英文:
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: "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
答案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];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论