简体   繁体   English

如何在Apple TV上播放视频?

[英]How do I play a video on tvOS for Apple TV?

I started a blank tvOS project and created the following code: 我开始了一个空白的tvOS项目并创建了以下代码:

- (void)viewDidLoad  
{  
    [super viewDidLoad];  

    AVPlayer *avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://www.myurl.com/myvideo.mp4"]];  
    AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];  

    avPlayerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);  
    [self.view.layer addSublayer:avPlayerLayer];  

    [avPlayer play];  
}  

Nothing happens in the simulator though once the app loads. 一旦应用加载,模拟器中没有任何事情发生。 No video, nothing, just a blank translucent screen in my Apple TV simulator. 没有视频,没有,只是我的Apple TV模拟器中的空白半透明屏幕。

What's the proper way to play a sample video on app launch for an Apple TV app from an HTTP source? 从HTTP源播放Apple TV应用程序的应用程序启动示例视频的正确方法是什么?

I just pasted your code in my tvOS sample project, replaced the URL and ran it. 我只是将您的代码粘贴到我的tvOS示例项目中,替换了URL并运行它。

Nothing happened. 没啥事儿。 Well, except for the fact that there's a log entry telling me that App Transport Security has blocked my URL request. 好吧,除了有一个日志条目告诉我App Transport Security阻止了我的URL请求。

So I headed to the Info.plist, disabled ATS and upon next launch the video showed up just fine. 所以我前往Info.plist, 禁用了ATS ,在下次发布时,视频显示正常。

So if you're also using a non-HTTPS URL you're very likely running into this issue which is easily fixed by either using an HTTPS URL, disabling ATS completely or allowing specific non-HTTPs URLs in your Info.plist. 因此,如果您还使用非HTTPS网址,则很可能遇到此问题,可以通过使用HTTPS网址,完全禁用ATS或允许Info.plist中的特定非HTTP网址轻松解决此问题。

PS: I used this video for testing. PS:我用这个视频进行测试。

You could also use TVML and TVMLJS https://developer.apple.com/library/prerelease/tvos/documentation/TVMLJS/Reference/TVJSFrameworkReference/ 你也可以使用TVML和TVMLJS https://developer.apple.com/library/prerelease/tvos/documentation/TVMLJS/Reference/TVJSFrameworkReference/

Adhere to the 'TVApplicationControllerDelegate' protocol and add some properties. 坚持'TVApplicationControllerDelegate'协议并添加一些属性。

AppDelegate.h AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate, TVApplicationControllerDelegate>

... ...

@property (strong, nonatomic) TVApplicationController *appController;
@property (strong, nonatomic) TVApplicationControllerContext *appControllerContext;

Then add the following to 'didFinishLaunchingWithOptions' 然后将以下内容添加到'didFinishLaunchingWithOptions'

AppDelegate.m AppDelegate.m

#define url @"http://localhost:8000/main.js"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.appControllerContext = [[TVApplicationControllerContext alloc] init];
    NSURL *javascriptURL = [NSURL URLWithString:url];

    self.appControllerContext.javaScriptApplicationURL= javascriptURL;

    for (id key in launchOptions) {
        id val=[launchOptions objectForKey:key];
        NSLog(@"key=%@ value=%@", key, val);
        if([val isKindOfClass:[NSString class]]) [self.appControllerContext.launchOptions objectForKey:val];

        self.appController = [[TVApplicationController alloc] initWithContext:self.appControllerContext window:self.window delegate:self];
    }

    return YES;
}

create a folder and add the following files 创建一个文件夹并添加以下文件

  • main.js main.js
  • index.tvml index.tvml

main.js main.js

function launchPlayer() {  
   var player = new Player();  
   var playlist = new Playlist();  
   var mediaItem = new MediaItem("video", "http://trailers.apple.com/movies/focus_features/9/9-clip_480p.mov");  
   player.playlist = playlist;  
   player.playlist.push(mediaItem);  
   player.present();
   //player.play() 
}

//in application.js  
App.onLaunch = function(options) {  
   launchPlayer();  
}

careful with this url in the mediaItem 小心使用mediaItem中的这个url

Set up a template of your choice. 设置您选择的模板

index.tvml index.tvml

<document>
  <alertTemplate>
      <title>…</title>
      <description>…</description>
      <button>
          <text>…</text>
      </button>
      <text>…</text>
  </alertTemplate>
</document>

open terminal and navigate to this folder then run 打开终端并导航到此文件夹然后运行

python -m SimpleHTTPServer 8000

make sure the port here is the port in your ObjC url. 确保此处的端口是ObjC网址中的端口。 The Apple examples use 9001. Apple示例使用9001。

See these tutorials for more info 有关详细信息,请参阅这些教程

http://jamesonquave.com/blog/developing-tvos-apps-for-apple-tv-with-swift/ http://jamesonquave.com/blog/developing-tvos-apps-for-apple-tv-part-2/ http://jamesonquave.com/blog/developing-tvos-apps-for-apple-tv-with-swift/ http://jamesonquave.com/blog/developing-tvos-apps-for-apple-tv-part- 2 /

One issue I ran into was trying to play a local video file. 我遇到的一个问题是尝试播放本地视频文件。 It wouldn't work and there were constraint issues etc. It looks like you can't use python to play the videos so either try apache or link to a video on the web. 它不起作用,有约束问题等。看起来你不能使用python播放视频,所以要么尝试apache或链接到网络上的视频。 This SO answer pointed me there. SO答案指出我在那里。

The best way to play video in your app on AppleTV is going to be AVKit's AVPlayerViewController. 在AppleTV上在您的应用中播放视频的最佳方式是AVKit的AVPlayerViewController。 If you use AVKit, you get a lot of stuff for free. 如果你使用AVKit,你可以免费获得很多东西。

https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVPlayerViewController_Class/index.html https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVPlayerViewController_Class/index.html

You simply add that player to the viewController's player property: 您只需将该播放器添加到viewController的播放器属性中:

// instantiate here or in storyboard
AVPlayerViewController *viewController = [[AVPlayerViewController alloc] initWithNibName:nil bundle:nil];
viewController.player = player;

[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];

// setup constraints, etc.

// play the video
[player play];

Also as mentioned below, make sure the video you're trying to play is coming either from an HTTPS connection or that you've disabled App Transport Security by setting the proper flags in the plist. 同样如下所述,请确保您尝试播放的视频来自HTTPS连接,或者您已通过在plist中设置正确的标记来禁用App Transport Security。

I didn't like the answers which messed about with subviews etc. For full-screen playback, I use the following (Non-ARC) code: 我不喜欢与子视图等有关的答案。对于全屏播放,我使用以下(非ARC)代码:

// Play the stream
NSString *wifiStreamAddress = @"http://yourmoviefile.m3u8";
AVPlayer *player = [[AVPlayer alloc] initWithURL: [NSURL URLWithString: wifiStreamAddress] ];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = player;

// Keep pointers to player and controller using retained properties:
self.player = player;
self.playerViewController = playerViewController;

[player release];
[playerViewController release];

[self presentViewController: playerViewController animated: true completion: ^{
    [self.player play];
}];

This works really neat, animating presentation and fading back to previous view when you tap the MENU button. 当您点击MENU按钮时,这非常简洁,动画显示和淡出回到上一个视图。 Also, it works great with the remote control using all the standard functions. 此外,它使用所有标准功能的遥控器很好。

Its working for me. 它为我工作。 May be helpful for you 对您有所帮助

 -(void)playAction
    {
   AVPlayerViewController *viewController = [[AVPlayerViewController    alloc] initWithNibName:nil bundle:nil];

   viewController.player = player;

  [self addChildViewController:viewController];

   [self.view addSubview:viewController.view];

   [viewController didMoveToParentViewController:self];

   // play the video

   [player play];

}

Swift version Make a PlayViewController which inherit the AVPlayerViewController. Swift版本制作一个继承AVPlayerViewController的PlayViewController。 In the viewcontroller which has play button, add such function 在具有播放按钮的viewcontroller中,添加这样的功能

@IBAction func onClickPlay(sender: AnyObject) {
    let playerVC = PlayerViewController()
    playerVC.playVideo(urlString)
    [self.presentViewController(playerVC, animated: true, completion: nil)]
}

In the PlayerViewController 在PlayerViewController中

func playVimeoVideo(link : String) {

    player = AVPlayer(URL: NSURL(string: link)!)   
    player?.play()
}

Notice The question and some answers may be a little misleading so that you might think that only the url with ".mp4" at the end can be played by the Apple TV. 注意问题和一些答案可能有点误导,因此您可能会认为Apple TV只能播放最后带有“.mp4”的网址。 I believed so at the first time I saw the post. 我第一次见到帖子时就这么信了。 It is not true. 这不是真的。 In fact, with AVPlayerViewController you can play Vimeo streaming video! 事实上,使用AVPlayerViewController,您可以播放Vimeo流媒体视频! The link to the stream video is not like https://vimeo.com/92655878 . 流视频的链接与https://vimeo.com/92655878不同 It is possible to get it from Vimeo site by extracting it from a json file, which can be downloaded from this link 可以从Vimeo站点通过从json文件中提取它来获取它,该文件可以从此链接下载

let link = "https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/" + videoId

To be able to get correct url for the video, you need to use the Vimeo Pro user access to get the stream link for a specific video. 为了能够获得正确的视频网址,您需要使用Vimeo Pro用户访问权限来获取特定视频的流链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM