Implementing Cross-Device Resume in Player V3

You can use Ruby and JavaScript to implement Ooyala's Cross-Device Resume functionality.

Important: Ooyala Player V3 is deprecated and is scheduled to be disabled on 2018-01-31. After that date, Player V3 will no longer play your video or audio content. Customers still using Player V3 need to migrate to Player V4 (see Migrating from Player V3 and Player Migration FAQ).
To implement Cross-Device Resume, you need to implement several high-level steps including:
  1. Loading a Player.
  2. Getting the Playback Position.
  3. Seeking to the Correct Playback Position.
Note: For more information about these high-level steps, see Loading Player for Cross-Device Resume.

Example: Ruby and JavaScript Sample

The following example illustrates the Ruby and JavaScript code for implementing the entire Cross-Device Resume experience.

 
file: player_teamplate.erb

<!DOCTYPE html">
<html>
 <head>
   <script src="http://player.ooyala.com/v3/<%= PLAYER_BRANDING_ID %>"></script>
  </head>
  <body>
     <div id='playerwrapper' style='width:920px;height:400px;'></div>
     <script>
         var playheadTime = <%= playhead_position %>;
         var videoPlayer =
            OO.Player.create('playerwrapper','<%= params[:embed_code] %>', {
            embedToken: "<%= signed_embed_code_url %>",
            initialTime: playheadTime
        });
      </script>
  </body>
</html>


file
: sample_sinatra_server_app.rb

require 'sinatra/base'
require "net/http"
require "cgi"
require "digest/sha2"
require "base64"

class SampleSinatraServerApp < Sinatra::Base


 PCODE = "INSERT_PCODE"
 API_KEY = "INSERT_API_KEY"
 PLAYER_BRANDING_ID = "INSERT_PLAYER_BRANDING_ID"
 SECRET = ENV["SECRET_API_KEY"]

 get "/sample_player/:embed_code/:account_id" do
   erb :player_template
 end

 helpers do
   def generate_signature(secret, http_method, request_path, params, body)
     # See docs for signing requests.
   end

   def append_signature_to_uri(uri)
     params_hash = {}      uri.query.split("&").map { |pair| pair.split("=", 2) }.each do |key,value|
       if (key && !key.empty? && value)                                                                                                  
         params_hash[CGI.unescape(key).to_sym] = (value && CGI.unescape(value))
       end
     end
     signature = generate_signature(SECRET, "GET", uri.path, params_hash, "")
     "#{uri.to_s}&signature=#{CGI.escape(signature)}"
   end

   def signed_embed_code_url
     uri = URI.parse("http://player.ooyala.com/sas/embed_token/#{PCODE}/#{params[:embed_code]}?" +
                     "account_id=#{params[:account_id]}&" +
                     "api_key=#{API_KEY}&" +
                     "expires=#{Time.now.to_i + 60 * 60 * 24}")
     return append_signature_to_uri(uri)
   end

   def playhead_position
     uri = URI.parse("http://api.ooyala.com/v2/cross_device_resume/accounts/" +
         "#{params[:account_id]}/viewed_assets/#{params[:embed_code]}/" +
         "playhead_info?expires=#{Time.now.to_i + 60 * 5}&api_key=#{API_KEY}")
     response = Net::HTTP.get_response(URI.parse(append_signature_to_uri(uri)))
     response.code == "200" ? JSON.parse(response.body)["playhead_seconds"]:0
   end
 end
end

해당 내용이 도움 되었습니까?