ScreenStreamPlugin.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package com.izouma.mobilecybergames;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.media.projection.MediaProjectionManager;
  6. import android.text.TextUtils;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Toast;
  10. import com.alivc.live.pusher.AlivcAudioAACProfileEnum;
  11. import com.alivc.live.pusher.AlivcFpsEnum;
  12. import com.alivc.live.pusher.AlivcLivePushConfig;
  13. import com.alivc.live.pusher.AlivcLivePushInfoListener;
  14. import com.alivc.live.pusher.AlivcLivePusher;
  15. import com.alivc.live.pusher.AlivcPreviewOrientationEnum;
  16. import com.alivc.live.pusher.AlivcResolutionEnum;
  17. import java.util.regex.Pattern;
  18. import io.flutter.plugin.common.MethodCall;
  19. import io.flutter.plugin.common.MethodChannel;
  20. import io.flutter.plugin.common.PluginRegistry;
  21. public class ScreenStreamPlugin implements MethodChannel.MethodCallHandler, PluginRegistry.ActivityResultListener {
  22. private static final String TAG = "ScreenStreamPlugin";
  23. public static final int CAPTURE_PERMISSION_REQUEST_CODE = 0x1123;
  24. private final PluginRegistry.Registrar registrar;
  25. private AlivcLivePushConfig mAlivcLivePushConfig;
  26. private AlivcLivePusher mAlivcLivePusher = null;
  27. private boolean started = false;
  28. private String url;
  29. private MethodChannel.Result result;
  30. public static void registerWith(PluginRegistry.Registrar registrar) {
  31. final MethodChannel channel = new MethodChannel(registrar.messenger(), "screen_stream");
  32. MethodChannel.MethodCallHandler methodCallHandler = new ScreenStreamPlugin(registrar);
  33. registrar.addActivityResultListener((PluginRegistry.ActivityResultListener) methodCallHandler);
  34. channel.setMethodCallHandler(methodCallHandler);
  35. }
  36. public ScreenStreamPlugin(PluginRegistry.Registrar registrar) {
  37. this.registrar = registrar;
  38. AlivcLivePushConfig.setMediaProjectionPermissionResultData(null);
  39. mAlivcLivePushConfig = new AlivcLivePushConfig();//初始化推流配置类
  40. mAlivcLivePushConfig.setResolution(AlivcResolutionEnum.RESOLUTION_720P);//分辨率540P,最大支持720P
  41. mAlivcLivePushConfig.setFps(AlivcFpsEnum.FPS_10); //建议用户使用20fps
  42. mAlivcLivePushConfig.setEnableBitrateControl(true); // 打开码率自适应,默认为true
  43. mAlivcLivePushConfig.setPreviewOrientation(AlivcPreviewOrientationEnum.ORIENTATION_LANDSCAPE_HOME_RIGHT); // 默认为竖屏,可设置home键向左或向右横屏。
  44. mAlivcLivePushConfig.setAudioProfile(AlivcAudioAACProfileEnum.AAC_LC);//设置音频编码模式
  45. mAlivcLivePushConfig.setEnableBitrateControl(true);// 打开码率自适应,默认为true
  46. }
  47. @Override
  48. public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
  49. this.result = result;
  50. if ("start".equals(methodCall.method)) {
  51. url = methodCall.argument("url");
  52. if (!TextUtils.isEmpty(url)) {
  53. if (Pattern.matches("^(?i)rtmp://.*", url)) {
  54. if (!start()) {
  55. result.error("start fail", "need android 5.0", null);
  56. }
  57. return;
  58. }
  59. }
  60. result.error("wrong url", url, null);
  61. } else if ("stop".equals(methodCall.method)) {
  62. stop();
  63. result.success("success");
  64. }
  65. }
  66. public boolean start() {
  67. if (started) {
  68. mAlivcLivePusher.stopPush();
  69. return true;
  70. } else {
  71. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
  72. MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) registrar.activeContext().getSystemService(Context.MEDIA_PROJECTION_SERVICE);
  73. registrar.activity().startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), CAPTURE_PERMISSION_REQUEST_CODE);
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. public boolean stop() {
  80. if (mAlivcLivePusher != null && mAlivcLivePusher.isPushing()) {
  81. mAlivcLivePusher.stopPush();
  82. }
  83. return true;
  84. }
  85. public void startPushWithoutSurface() {
  86. mAlivcLivePusher = new AlivcLivePusher();
  87. try {
  88. mAlivcLivePusher.init(registrar.context(), mAlivcLivePushConfig);
  89. } catch (IllegalArgumentException e) {
  90. e.printStackTrace();
  91. } catch (IllegalStateException e) {
  92. e.printStackTrace();
  93. }
  94. mAlivcLivePusher.setLivePushInfoListener(new AlivcLivePushInfoListener() {
  95. @Override
  96. public void onPreviewStarted(AlivcLivePusher pusher) {
  97. }
  98. @Override
  99. public void onPreviewStoped(AlivcLivePusher pusher) {
  100. }
  101. @Override
  102. public void onPushStarted(AlivcLivePusher pusher) {
  103. Log.d(TAG, "onPushStarted");
  104. started = true;
  105. registrar.activity().runOnUiThread(new Runnable() {
  106. @Override
  107. public void run() {
  108. Toast.makeText(registrar.context(), "开始录屏", Toast.LENGTH_SHORT).show();
  109. }
  110. });
  111. }
  112. @Override
  113. public void onFirstAVFramePushed(AlivcLivePusher alivcLivePusher) {
  114. Log.d(TAG, "onFirstAVFramePushed");
  115. }
  116. @Override
  117. public void onPushPauesed(AlivcLivePusher pusher) {
  118. Log.d(TAG, "onPushPauesed");
  119. }
  120. @Override
  121. public void onPushResumed(AlivcLivePusher pusher) {
  122. Log.d(TAG, "onPushResumed");
  123. }
  124. @Override
  125. public void onPushStoped(AlivcLivePusher pusher) {
  126. Log.d(TAG, "onPushStopped");
  127. started = false;
  128. mAlivcLivePusher = null;
  129. registrar.activity().runOnUiThread(new Runnable() {
  130. @Override
  131. public void run() {
  132. Toast.makeText(registrar.context(), "录屏结束", Toast.LENGTH_SHORT).show();
  133. }
  134. });
  135. }
  136. @Override
  137. public void onPushRestarted(AlivcLivePusher pusher) {
  138. Log.d(TAG, "onPushRestarted");
  139. }
  140. @Override
  141. public void onFirstFramePreviewed(AlivcLivePusher pusher) {
  142. Log.d(TAG, "onFirstFramePreviewed");
  143. }
  144. @Override
  145. public void onDropFrame(AlivcLivePusher pusher, int countBef, int countAft) {
  146. Log.d(TAG, "onDropFrame");
  147. }
  148. @Override
  149. public void onAdjustBitRate(AlivcLivePusher pusher, int curBr, int targetBr) {
  150. Log.d(TAG, "onAdjustBitRate");
  151. }
  152. @Override
  153. public void onAdjustFps(AlivcLivePusher pusher, int curFps, int targetFps) {
  154. Log.d(TAG, "onAdjustFps");
  155. }
  156. });
  157. mAlivcLivePusher.startPreview(null);
  158. mAlivcLivePusher.startPush(url);
  159. mAlivcLivePusher.setCaptureVolume(50);
  160. }
  161. @Override
  162. public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
  163. switch (requestCode) {
  164. case CAPTURE_PERMISSION_REQUEST_CODE: {
  165. if (resultCode == Activity.RESULT_OK) {
  166. AlivcLivePushConfig.setMediaProjectionPermissionResultData(data);
  167. if (mAlivcLivePushConfig.getMediaProjectionPermissionResultData() != null) {
  168. if (mAlivcLivePusher == null) {
  169. startPushWithoutSurface();
  170. if (result != null) {
  171. result.success("success");
  172. return true;
  173. }
  174. }
  175. }
  176. }
  177. if (result != null) {
  178. result.error("needs permission", "permission not granted", null);
  179. }
  180. }
  181. break;
  182. }
  183. return false;
  184. }
  185. }