package com.zcloud.util.ali;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.zcloud.entity.PageData;
import com.zcloud.util.Const;

import java.util.List;

public class GetPlayInfo {
	//填入AccessKey信息
	public static DefaultAcsClient initVodClient() throws ClientException {
	    String regionId = Const.ALIYUN_REGIONID;  // 点播服务接入地域
	    DefaultProfile profile = DefaultProfile.getProfile(regionId, Const.ALI_ACCESSKEY, Const.ALI_ACCESSKEY_SECRET);
	    DefaultAcsClient client = new DefaultAcsClient(profile);
	    return client;
	}

	/*获取播放地址函数*/
	public static GetPlayInfoResponse getPlayInfo(PageData pd) throws Exception {
		 DefaultAcsClient client = initVodClient();
	    GetPlayInfoRequest request = new GetPlayInfoRequest();
	    request.setVideoId(pd.getString("videoId"));
	    return client.getAcsResponse(request);
	}



	/*以下为调用示例*/
	public static void main(String[] argv) throws ClientException {
	    DefaultAcsClient client = initVodClient();
	    GetPlayInfoResponse response = new GetPlayInfoResponse();
	    try {
			PageData pd = new PageData();
			//pd = this.getPageData();
			pd.put("videoId", "6a892c4fd47e46e7bec8fea6d3ed813f");
	        response = getPlayInfo(pd);
	        List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
	        System.out.println(JSON.toJSONString(response));
	        //播放地址
	        for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
	            System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
	        }
	        //Base信息
	        System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
	        System.out.print("VideoBase.Title = " + JSON.toJSONString(response.getVideoBase()) + "\n");
	    } catch (Exception e) {
	        System.out.print("ErrorMessage = " + e.getLocalizedMessage());
	    }
	    System.out.print("RequestId = " + response.getRequestId() + "\n");
	}

}
