Play video from Apple TV Top Shelf

I created a simple AppleTV project to display multiple videos by category, watching and playing videos works fine. It was implemented as a client-server application using TVML and TVJS, so most of the application logic is in Javascript files. They are statically generated in the background from dynamic content on a regular basis.

Then I added the TopShelf extension to the application, which pulls some popular videos from the API, it also works great, pulls the video on hold.

The problem I am facing is to detect and respond to a user selecting a video from the top shelf. I created an exampletvapp: // URL scheme, which I registered in my plist file.

I also added displayURL and playURL to TVContentItems. When you select one of the videos, my application launches as expected, but does not actually handle the playback of this video.

In my AppDelegate file, I added print statements to print launchOptions, and also added an openUrl function that calls, but only registers this if I close the application and quickly select something from the top shelf before the application closes and I lose the session debugging.

// AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    print("App Launch: \(launchOptions)")
    window = UIWindow(frame: UIScreen.mainScreen().bounds)

    let appControllerContext = TVApplicationControllerContext()

    guard let javaScriptURL = NSURL(string: AppDelegate.TVBootURL) else {
        fatalError("unable to create NSURL")
    }

    appControllerContext.javaScriptApplicationURL = javaScriptURL
    appControllerContext.launchOptions["BASEURL"] = AppDelegate.TVBaseURL

    if let options = launchOptions {
        for (kind, value) in options {
            if let kindStr = kind as? String {
                appControllerContext.launchOptions[kindStr] = value
            }
        }
    }

    appController = TVApplicationController(context: appControllerContext, window: self.window, delegate: self)

    return true
}

    func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {

        guard let javaScriptURL = NSURL(string: AppDelegate.TVBootURL) else {
            fatalError("unable to create NSURL")
        }

        let appControllerContext = TVApplicationControllerContext()
        appControllerContext.javaScriptApplicationURL = javaScriptURL
        appControllerContext.launchOptions["BASEURL"] = AppDelegate.TVBaseURL


        for (kind, value) in options {
            appControllerContext.launchOptions[kind] = value
        }

        appController = TVApplicationController(context: appControllerContext, window: self.window, delegate: self)

        return true
    }

    func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {
        print("handle Url called")
    }

JavaScript:

// Application.js
App.onLaunch = function(options) {
    var javascriptFiles = [
        `${options.BASEURL}js/ResourceLoader.js`,
        `${options.BASEURL}js/Presenter.js`
    ];

    console.log("options", options);

console.log JS, , , . , Player TopShelf. , URL- , , ajax , TVContentItem.

, : topshelf ( Swift) javascript . javascript, console.log? , .

UPDATE:

, ( Apple), Safari JS Context Apple TV "" > "". , URL- JS . URL- openURL, JS, .

, displayURL TVContentItem. JS openURL, ( )

+4
2

, , . , openUrl JS, . , .

AVPlayer AVPlayerViewController ViewController . , .

// AppDelegate.swift
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {

    let urlString = "http:\(url.path!)"
    let url = NSURL(string: urlString)!
    print("opening URL: \(url)")

    let item = AVPlayerItem(URL: url)

    // Need to listen to the end of the player so we can dismiss the AVPlayer
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: item)

    let player = AVPlayer(playerItem: item)
    player.actionAtItemEnd = AVPlayerActionAtItemEnd.None
    let playerVC = AVPlayerViewController()
    playerVC.player = player

    appController?.navigationController.presentViewController(playerVC, animated: true, completion: nil)
    player.play()

    return true
}

func playerDidFinishPlaying (note: NSNotification) {
    // Dimiss the AVPlayer View Controller from the Navigation Controller
    appController?.navigationController.dismissViewControllerAnimated(true, completion: nil)
}
+2

Top Shelf TVML.

Apple TVML , AppDelegate, URL- JavaScript:

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {

    if url.host == nil { return true }

    let urlString = url.absoluteString

    NSLog(urlString) // Log to XCode

    appController?.evaluateInJavaScriptContext({(evaluation: JSContext) -> Void in

        // Log to JS log (Safari)
        evaluation.evaluateScript("console.log('url string: \(urlString)')")

        // obviously we need to do something more interesting here


        },
        completion: {(Bool) -> Void in }

    )

    return true

}

- JavaScript, URL- TVML. , , Top Shelf.

0

Source: https://habr.com/ru/post/1611928/


All Articles