Android 接入场景

临境直播观看 SDK 的接入可以通过两种场景;
一、原生场景:直接从 App 原生进入直播观看;
二、H5 嵌入场景:从嵌套在 webview 的 H5 中,进入直播观看;

原生场景

原生场景接入方式

  • 首先接入临境直播观看 SDK,并配置相应环境,具体请参见 Android 接入说明文档。
  • 直接在需要跳转的 Activity 里面添加如下代码:
  1. //设置跳转到临境直播时对应的参数
  2. LivePullIntentDataOptions options = new LivePullIntentDataOptions();
  3. options.setLiveRoomCode("");//设置直播室roomCode
  4. options.setLivePlayPwd("");//设置直播观看密码
  5. options.setUserId("");//设置用户userId
  6. options.setUserAvatar("");//设置用户头像url
  7. options.setUserName("");//设置用户名
  8. options.setUserLoginName("");//可选: 设置用户登录名
  9. options.setUserPoint(0);//设置用户积分
  10. //跳转到相应临境直播播放页面,context为Activity的实例
  11. LivePullManager.intentToLjLive(context, options);

H5嵌入场景

H5嵌入场景接入方式

  • 首先接入临境直播观看 SDK,并配置相应环境,具体请参见 Android 接入说明文档。
  • 在app的webview中添加云学堂直播协议代理回调;
  1. package com.yxt.demo.liveyxtsdk;
  2. import android.net.Uri;
  3. import android.os.Build;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.text.TextUtils;
  7. import android.util.Log;
  8. import android.webkit.CookieManager;
  9. import android.webkit.GeolocationPermissions;
  10. import android.webkit.JsPromptResult;
  11. import android.webkit.WebChromeClient;
  12. import android.webkit.WebSettings;
  13. import android.webkit.WebView;
  14. import android.webkit.WebViewClient;
  15. import android.widget.Toast;
  16. import com.yxt.sdk.live.pull.LivePullIntentDataOptions;
  17. import com.yxt.sdk.live.pull.LivePullManager;
  18. import org.json.JSONObject;
  19. import java.util.Objects;
  20. public class MainWebViewActivity extends AppCompatActivity {
  21. protected WebView yxt_webview;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main_web_test);
  26. yxt_webview = findViewById(R.id.yxt_webview);
  27. //webview基本属性设置
  28. yxt_webview.getSettings().setJavaScriptEnabled(true);
  29. yxt_webview.getSettings().setDefaultTextEncodingName("utf-8");
  30. yxt_webview.getSettings().setGeolocationEnabled(true);
  31. yxt_webview.getSettings().setLoadWithOverviewMode(true);//设置webView自适应屏幕大小
  32. yxt_webview.getSettings().setSupportMultipleWindows(true);
  33. yxt_webview.getSettings().setAppCacheEnabled(true);//设置H5的缓存打开,默认关闭
  34. yxt_webview.getSettings().setDomStorageEnabled(true);
  35. yxt_webview.getSettings().setLoadsImagesAutomatically(true);
  36. yxt_webview.getSettings().setAllowFileAccess(true);
  37. yxt_webview.getSettings().setAllowFileAccessFromFileURLs(true);
  38. yxt_webview.getSettings().setPluginState(WebSettings.PluginState.ON);// 可以使用插件
  39. yxt_webview.getSettings().setSupportMultipleWindows(true);
  40. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  41. CookieManager.getInstance().setAcceptThirdPartyCookies(yxt_webview, true);
  42. }
  43. //添加云学堂直播的 user agent
  44. yxt_webview.getSettings().setUserAgentString(yxt_webview.getSettings().getUserAgentString() + " XuanLiveThirdPlatform/0");
  45. yxt_webview.setWebChromeClient(new WebChromeClient() {
  46. @Override
  47. public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
  48. callback.invoke(origin, true, false);
  49. super.onGeolocationPermissionsShowPrompt(origin, callback);
  50. }
  51. @Override
  52. public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
  53. url = message;
  54. Log.e("TAG", "onJsPrompt url: " + url);
  55. return process(view, url, result);
  56. }
  57. });
  58. yxt_webview.setWebViewClient(new WebViewClient() {
  59. @Override
  60. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  61. view.loadUrl(url);
  62. return true;
  63. }
  64. });
  65. }
  66. /**
  67. * YXT协议处理
  68. *
  69. * @param webView
  70. * @param url
  71. * @param result
  72. * @return
  73. */
  74. public boolean process(WebView webView, String url, JsPromptResult result) {
  75. if (!url.startsWith("http")) {
  76. if (result != null) {
  77. result.confirm();
  78. result.cancel();
  79. }
  80. if (url.startsWith("yxtthirdapp://")) {
  81. try {
  82. Uri uri = Uri.parse(url);
  83. String host = Objects.requireNonNull(uri.getHost()).toLowerCase();
  84. // 域名 空判断
  85. if (TextUtils.isEmpty(host)) {
  86. return false;
  87. }
  88. String param = uri.getQueryParameter("param");
  89. JSONObject jsonObject = new JSONObject(param);
  90. String name = jsonObject.optString("name", "");
  91. if ("yxt_app_live_open".equals(name)) {
  92. String roomcode = jsonObject.optString("roomid", "");
  93. String password = jsonObject.optString("password", "");
  94. LivePullIntentDataOptions options = new LivePullIntentDataOptions();
  95. options.setLivePlayPwd(password);
  96. options.setLiveRoomCode(roomcode);
  97. //传入真实用户身份 此处是测试写死的
  98. options.setUserId("5ff44dff-82b5-4dd1-8a0a-c4c6a0a70733");
  99. options.setUserName("王阳明");
  100. options.setUserAvatar("https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoKnUXeQnOQicPKWM8JHWH704OmhM3FRbq5icT5mhykg2NVL7epHuWIOmIDMBTCia5LiaTFmVL6Y9659Q/132");
  101. LivePullManager.intentToLjLive(this, options);
  102. }
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. @Override
  112. public void onBackPressed() {
  113. if (yxt_webview.canGoBack()) {
  114. yxt_webview.goBack();
  115. } else {
  116. super.onBackPressed();
  117. }
  118. }
  119. }
  • 当获取到协议之后需要获取用户可用积分;
  1. // 正式api地址
  2. public static final String DEFAULT_FORMAL_BASE_API = "https://api-qida.yunxuetang.cn/v1/qdl/";
  3. // 测试api地址
  4. public static final String DEFAULT_TEST_BASE_API = "https://devinner.yunxuetang.com.cn/qidaliveapi/v1/";
  5. /**
  6. * 此API用于获取用户可用积分
  7. */
  8. public static final String GETVALIDPOINTS = API地址 + "live/getvalidpoints";
  9. public static void openLive(final Context mContext, final String roomId, final String playPwd) {
  10. Alert.getInstance().showDialog();//此处是显示等待的loading效果 需自己实现
  11. //此处为通过JavaAPI获取当前用户的可用积分,用于观看直播时对主播进行礼物赠送是的兑换
  12. //HttpUtil需要自己实现 此处就是简单的get请求
  13. //注意该请求头中需要携带以下参数
  14. //1."Source" "506"
  15. //2."Token" token从h5的localstorage获取
  16. HttpUtil.get(GETVALIDPOINTS, new JsonHttpHandler() {
  17. @Override
  18. public void onSuccessJSONObject(int statusCode, JSONObject response) {
  19. super.onSuccessJSONObject(statusCode, response);
  20. //此处是获取到后台返回的积分是个jsonobject 此处需要在自己的http框架中解析得
  21. //此处跳转直播
  22. //设置跳转到临境直播时对应的参数
  23. LivePullIntentDataOptions options = new LivePullIntentDataOptions();
  24. options.setLivePlayPwd(playPwd);//设置直播观看密码
  25. options.setLiveRoomCode(roomId);//设置直播室roomCode
  26. options.setUserAvatar("");//设置用户头像url
  27. options.setUserId("");//设置用户userId
  28. options.setUserLoginName("");//设置用户登录名
  29. options.setUserPoint(response.optInt("validPoints", 0));//设置用户积分
  30. //跳转到相应临境直播播放页面,context为Activity的实例
  31. LivePullManager.intentToLjLive(context, options);
  32. }
  33. @Override
  34. public void onFailure(int statusCode, String responseString) {
  35. super.onFailure(statusCode, responseString);
  36. //获取失败,此处跳转直播积分为0
  37. LivePullIntentDataOptions options = new LivePullIntentDataOptions();
  38. options.setLivePlayPwd(playPwd);//设置直播观看密码
  39. options.setLiveRoomCode(roomId);//设置直播室roomCode
  40. options.setUserAvatar("");//设置用户头像url
  41. options.setUserId("");//设置用户userId
  42. options.setUserLoginName("");//设置用户登录名
  43. options.setUserPoint(0);//设置用户积分
  44. }
  45. @Override
  46. public void onFinish() {
  47. super.onFinish();
  48. Alert.getInstance().hideDialog();//此处是隐藏
  49. 等待的loading效果 需自己实现
  50. }
  51. });
  52. }

关于作者

江苏云学堂网络科技有限公司开发人员,如有问题请发送邮件至
lingy@yxt.com
cuiqq@yxt.com