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

前端 未结 6 1328
北荒
北荒 2020-12-24 02:29

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

- (void)viewDidLoad  
{  
    [super viewDidLoad];  

    AVPlayer *avPlayer = [AVPlayer playe         


        
6条回答
  •  借酒劲吻你
    2020-12-24 03:02

    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 
    

    ...

    @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

    
      
          
          
          
          
      
    
    

    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.

提交回复
热议问题