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