Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I started a blank tvOS project and created the following code:

- (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.

What's the proper way to play a sample video on app launch for an Apple TV app from an HTTP source?

like image 479
Ethan Allen Avatar asked Sep 10 '15 05:09

Ethan Allen


People also ask

How do I play video files on Apple TV?

If you have video files on your computer, you can import them into the Apple TV app so they appear in your media library. In the Apple TV app on your Mac, choose File > Import. Locate a file or folder, then click Open.

Why can't I play videos on Apple TV?

Update to the latest version of the Apple TV app Update to the latest version of iOS or iPadOS, macOS, or tvOS. If you're using a compatible smart TV, streaming device, or gaming console that's connected to the internet, the Apple TV app should update automatically.

How do I play videos from Safari on Apple TV?

Play a web video on your HDTVIn the Safari app on your Mac, navigate to the web video you want to play. Compatible web videos have an AirPlay icon in the video controls in the bottom of the video window. Click the AirPlay icon , then choose your Apple TV.


2 Answers

I just pasted your code in my tvOS sample project, replaced the URL and ran it.

Nothing happened. Well, except for the fact that there's a log entry telling me that App Transport Security has blocked my URL request.

So I headed to the Info.plist, disabled ATS and upon next launch the video showed up just fine.

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.

P.S.: I used this video for testing.

like image 130
lemonmojo Avatar answered Oct 22 '22 22:10

lemonmojo


You could also use TVML and TVMLJS https://developer.apple.com/library/prerelease/tvos/documentation/TVMLJS/Reference/TVJSFrameworkReference/

Adhere to the 'TVApplicationControllerDelegate' protocol and add some properties.

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate, TVApplicationControllerDelegate> 

...

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

Then add the following to 'didFinishLaunchingWithOptions'

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

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

Set up a template of your choice.

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

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 SO answer pointed me there.

like image 27
Alex Hedley Avatar answered Oct 22 '22 21:10

Alex Hedley