package com.izouma.mobilecybergames; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.media.projection.MediaProjectionManager; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Toast; import com.alivc.live.pusher.AlivcAudioAACProfileEnum; import com.alivc.live.pusher.AlivcFpsEnum; import com.alivc.live.pusher.AlivcLivePushConfig; import com.alivc.live.pusher.AlivcLivePushInfoListener; import com.alivc.live.pusher.AlivcLivePusher; import com.alivc.live.pusher.AlivcPreviewOrientationEnum; import com.alivc.live.pusher.AlivcResolutionEnum; import java.util.regex.Pattern; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.PluginRegistry; public class ScreenStreamPlugin implements MethodChannel.MethodCallHandler, PluginRegistry.ActivityResultListener { private static final String TAG = "ScreenStreamPlugin"; public static final int CAPTURE_PERMISSION_REQUEST_CODE = 0x1123; private final PluginRegistry.Registrar registrar; private AlivcLivePushConfig mAlivcLivePushConfig; private AlivcLivePusher mAlivcLivePusher = null; private boolean started = false; private String url; private MethodChannel.Result result; public static void registerWith(PluginRegistry.Registrar registrar) { final MethodChannel channel = new MethodChannel(registrar.messenger(), "screen_stream"); MethodChannel.MethodCallHandler methodCallHandler = new ScreenStreamPlugin(registrar); registrar.addActivityResultListener((PluginRegistry.ActivityResultListener) methodCallHandler); channel.setMethodCallHandler(methodCallHandler); } public ScreenStreamPlugin(PluginRegistry.Registrar registrar) { this.registrar = registrar; AlivcLivePushConfig.setMediaProjectionPermissionResultData(null); mAlivcLivePushConfig = new AlivcLivePushConfig();//初始化推流配置类 mAlivcLivePushConfig.setResolution(AlivcResolutionEnum.RESOLUTION_720P);//分辨率540P,最大支持720P mAlivcLivePushConfig.setFps(AlivcFpsEnum.FPS_10); //建议用户使用20fps mAlivcLivePushConfig.setEnableBitrateControl(true); // 打开码率自适应,默认为true mAlivcLivePushConfig.setPreviewOrientation(AlivcPreviewOrientationEnum.ORIENTATION_LANDSCAPE_HOME_RIGHT); // 默认为竖屏,可设置home键向左或向右横屏。 mAlivcLivePushConfig.setAudioProfile(AlivcAudioAACProfileEnum.AAC_LC);//设置音频编码模式 mAlivcLivePushConfig.setEnableBitrateControl(true);// 打开码率自适应,默认为true } @Override public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { this.result = result; if ("start".equals(methodCall.method)) { url = methodCall.argument("url"); if (!TextUtils.isEmpty(url)) { if (Pattern.matches("^(?i)rtmp://.*", url)) { if (!start()) { result.error("start fail", "need android 5.0", null); } return; } } result.error("wrong url", url, null); } else if ("stop".equals(methodCall.method)) { stop(); result.success("success"); } } public boolean start() { if (started) { mAlivcLivePusher.stopPush(); return true; } else { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) registrar.activeContext().getSystemService(Context.MEDIA_PROJECTION_SERVICE); registrar.activity().startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), CAPTURE_PERMISSION_REQUEST_CODE); return true; } } return false; } public boolean stop() { if (mAlivcLivePusher != null && mAlivcLivePusher.isPushing()) { mAlivcLivePusher.stopPush(); } return true; } public void startPushWithoutSurface() { mAlivcLivePusher = new AlivcLivePusher(); try { mAlivcLivePusher.init(registrar.context(), mAlivcLivePushConfig); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } mAlivcLivePusher.setLivePushInfoListener(new AlivcLivePushInfoListener() { @Override public void onPreviewStarted(AlivcLivePusher pusher) { } @Override public void onPreviewStoped(AlivcLivePusher pusher) { } @Override public void onPushStarted(AlivcLivePusher pusher) { Log.d(TAG, "onPushStarted"); started = true; registrar.activity().runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(registrar.context(), "开始录屏", Toast.LENGTH_SHORT).show(); } }); } @Override public void onFirstAVFramePushed(AlivcLivePusher alivcLivePusher) { Log.d(TAG, "onFirstAVFramePushed"); } @Override public void onPushPauesed(AlivcLivePusher pusher) { Log.d(TAG, "onPushPauesed"); } @Override public void onPushResumed(AlivcLivePusher pusher) { Log.d(TAG, "onPushResumed"); } @Override public void onPushStoped(AlivcLivePusher pusher) { Log.d(TAG, "onPushStopped"); started = false; mAlivcLivePusher = null; registrar.activity().runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(registrar.context(), "录屏结束", Toast.LENGTH_SHORT).show(); } }); } @Override public void onPushRestarted(AlivcLivePusher pusher) { Log.d(TAG, "onPushRestarted"); } @Override public void onFirstFramePreviewed(AlivcLivePusher pusher) { Log.d(TAG, "onFirstFramePreviewed"); } @Override public void onDropFrame(AlivcLivePusher pusher, int countBef, int countAft) { Log.d(TAG, "onDropFrame"); } @Override public void onAdjustBitRate(AlivcLivePusher pusher, int curBr, int targetBr) { Log.d(TAG, "onAdjustBitRate"); } @Override public void onAdjustFps(AlivcLivePusher pusher, int curFps, int targetFps) { Log.d(TAG, "onAdjustFps"); } }); mAlivcLivePusher.startPreview(null); mAlivcLivePusher.startPush(url); mAlivcLivePusher.setCaptureVolume(50); } @Override public boolean onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case CAPTURE_PERMISSION_REQUEST_CODE: { if (resultCode == Activity.RESULT_OK) { AlivcLivePushConfig.setMediaProjectionPermissionResultData(data); if (mAlivcLivePushConfig.getMediaProjectionPermissionResultData() != null) { if (mAlivcLivePusher == null) { startPushWithoutSurface(); if (result != null) { result.success("success"); return true; } } } } if (result != null) { result.error("needs permission", "permission not granted", null); } } break; } return false; } }