xcode 如何在 Apple TV 的 tvOS 上播放视频?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32493872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:10:15  来源:igfitidea点击:

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

xcodeapple-tvtvos

提问by Ethan Allen

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 应用程序在应用程序启动时播放示例视频的正确方法是什么?

采纳答案by lemonmojo

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 ATSand 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 URL,您很可能会遇到此问题,通过使用 HTTPS URL、完全禁用 ATS 或在 Info.plist 中允许特定的非 HTTPs URL 可以轻松解决该问题。

P.S.: I used this videofor testing.

PS:我用这个视频来测试。

回答by Alex Hedley

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
  • index.tvml
  • 主文件
  • 索引.tml

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 urlin the mediaItem

注意mediaItem 中的这个url

Set up a templateof your choice.

设置您选择的模板

index.tvml

索引.tml

<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. The Apple examples use 9001.

确保这里的端口是你的 ObjC url 中的端口。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. This SOanswer pointed me there.

我遇到的一个问题是尝试播放本地视频文件。它不起作用,并且存在约束问题等。看起来您无法使用 python 播放视频,因此请尝试使用 apache 或链接到网络上的视频。这个SO答案指向我那里。

回答by Jess Bowers

The best way to play video in your app on AppleTV is going to be AVKit's AVPlayerViewController. If you use AVKit, you get a lot of stuff for free.

在 AppleTV 上的应用程序中播放视频的最佳方式将是 AVKit 的 AVPlayerViewController。如果你使用 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 中设置正确的标志来禁用应用传输安全。

回答by Simon Tillson

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. Also, it works great with the remote control using all the standard functions.

当您点击 MENU 按钮时,这非常简洁,动画演示并淡入前一视图。此外,它与使用所有标准功能的遥控器配合得很好。

回答by Gautam

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];

}

回答by flame3

Swift version Make a PlayViewController which inherit the AVPlayerViewController. In the viewcontroller which has play button, add such function

Swift 版本制作一个继承 AVPlayerViewController 的 PlayViewController。在具有播放按钮的视图控制器中,添加这样的功能

@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()
}

NoticeThe 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. 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! The link to the stream video is not like 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

注意问题和一些答案可能有点误导,因此您可能会认为Apple TV只能播放末尾带有“.mp4”的url。我第一次看到这个帖子的时候也是这么认为的。这不是真的。事实上,使用 AVPlayerViewController 您可以播放 Vimeo 流媒体视频!流视频的链接不像https://vimeo.com/92655878。可以通过从 json 文件中提取它来从 Vimeo 站点获取它,该文件可以从此链接下载

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 用户访问权限来获取特定视频的流链接。