Commit 01018d0a8a9e7573a10cabbb76d35b684bbe3b45

Authored by 朱显松
1 parent f4d14f7c

添加推流水印图片,增加推流过程横竖屏切换逻辑

app/src/main/assets/cnlive_logo.png 0 → 100644

6.02 KB

app/src/main/java/com/cnlive/gundam/main/ui/activity/MainActivity.java
@@ -20,6 +20,7 @@ import com.cnlive.gundam.main.ui.widget.FragmentTabHost; @@ -20,6 +20,7 @@ import com.cnlive.gundam.main.ui.widget.FragmentTabHost;
20 import com.cnlive.gundam.main.utils.SharedPreferencesHelper; 20 import com.cnlive.gundam.main.utils.SharedPreferencesHelper;
21 import com.cnlive.gundam.stream.model.ChannelInfoModel; 21 import com.cnlive.gundam.stream.model.ChannelInfoModel;
22 import com.cnlive.gundam.stream.ui.activity.StreamActivity; 22 import com.cnlive.gundam.stream.ui.activity.StreamActivity;
  23 +import com.cnlive.gundam.stream.util.FileUtil;
23 import com.cnlive.gundam.user.activity.LoginActivity; 24 import com.cnlive.gundam.user.activity.LoginActivity;
24 import com.cnlive.gundam.user.auth.UserService; 25 import com.cnlive.gundam.user.auth.UserService;
25 import com.cnlive.gundam.user.model.User; 26 import com.cnlive.gundam.user.model.User;
@@ -51,6 +52,9 @@ public class MainActivity extends AppCompatActivity implements FragmentTabHost.O @@ -51,6 +52,9 @@ public class MainActivity extends AppCompatActivity implements FragmentTabHost.O
51 )); 52 ));
52 53
53 mAnchorInfoPresenter = new AnchorInfoPresenter(this); 54 mAnchorInfoPresenter = new AnchorInfoPresenter(this);
  55 +
  56 + //加载水印图片
  57 + FileUtil.copyFile(this, "cnlive_logo.png");
54 } 58 }
55 59
56 @Override 60 @Override
@@ -106,6 +110,7 @@ public class MainActivity extends AppCompatActivity implements FragmentTabHost.O @@ -106,6 +110,7 @@ public class MainActivity extends AppCompatActivity implements FragmentTabHost.O
106 110
107 /** 111 /**
108 * 开启直播推流页面 112 * 开启直播推流页面
  113 + *
109 * @param info 主播信息 114 * @param info 主播信息
110 */ 115 */
111 private void startStreamActivity(ChannelInfoModel info) { 116 private void startStreamActivity(ChannelInfoModel info) {
@@ -113,7 +118,7 @@ public class MainActivity extends AppCompatActivity implements FragmentTabHost.O @@ -113,7 +118,7 @@ public class MainActivity extends AppCompatActivity implements FragmentTabHost.O
113 String channelId = info.getData().getChannelId(); 118 String channelId = info.getData().getChannelId();
114 String channelName = info.getData().getChannelName(); 119 String channelName = info.getData().getChannelName();
115 String coverImgUrl = info.getData().getCoverImgUrl(); 120 String coverImgUrl = info.getData().getCoverImgUrl();
116 - StreamActivity.start(this, activityId, channelId, channelName, coverImgUrl, false); 121 + StreamActivity.start(this, activityId, channelId, channelName, coverImgUrl);
117 } 122 }
118 123
119 /** 124 /**
app/src/main/java/com/cnlive/gundam/main/utils/AccelerometerSensor.java 0 → 100644
  1 +package com.cnlive.gundam.main.utils;
  2 +
  3 +import android.content.Context;
  4 +import android.content.pm.ActivityInfo;
  5 +import android.hardware.Sensor;
  6 +import android.hardware.SensorEvent;
  7 +import android.hardware.SensorEventListener;
  8 +import android.hardware.SensorManager;
  9 +
  10 +/**
  11 + * Created by xiansong on 2017/6/30.
  12 + */
  13 +
  14 +public class AccelerometerSensor {
  15 +
  16 + private static final int ORIENTATION_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
  17 + private static final int ORIENTATION_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
  18 + private static final int ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
  19 +
  20 + private Context context;
  21 + private SensorManager mSensorManager = null;
  22 + private ScreenOrientationListener listener;
  23 +
  24 + private int orientation = -1;
  25 + private double boundaryValue;
  26 +
  27 + public interface ScreenOrientationListener {
  28 + void onOrientationChange(int orientation);
  29 + }
  30 +
  31 + public AccelerometerSensor(Context context) {
  32 + this.context = context;
  33 + }
  34 +
  35 + public void unregister() {
  36 + mSensorManager.unregisterListener(lsn);
  37 + }
  38 +
  39 + public void register() {
  40 + boundaryValue = 8.5d;
  41 + mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
  42 + Sensor mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  43 + mSensorManager.registerListener(lsn, mSensor, SensorManager.SENSOR_DELAY_UI);
  44 + }
  45 +
  46 + private SensorEventListener lsn = new SensorEventListener() {
  47 +
  48 + @SuppressWarnings("deprecation")
  49 + @Override
  50 + public void onSensorChanged(SensorEvent event) {
  51 + double x = event.values[SensorManager.DATA_X];
  52 + double y = event.values[SensorManager.DATA_Y];
  53 + if (x > boundaryValue && orientation != ORIENTATION_LANDSCAPE) {
  54 + orientation_change(orientation = ORIENTATION_LANDSCAPE);
  55 + } else if (y > boundaryValue && orientation != ORIENTATION_PORTRAIT) {
  56 + orientation_change(orientation = ORIENTATION_PORTRAIT);
  57 + } else if (x < (-boundaryValue) && orientation != ORIENTATION_REVERSE_LANDSCAPE) {
  58 + orientation_change(orientation = ORIENTATION_REVERSE_LANDSCAPE);
  59 + }
  60 + }
  61 +
  62 + @Override
  63 + public void onAccuracyChanged(Sensor sensor, int accuracy) {
  64 +
  65 + }
  66 + };
  67 +
  68 + public int getOrientation() {
  69 + return orientation;
  70 + }
  71 +
  72 + public void setScreenOrientationListener(ScreenOrientationListener listener) {
  73 + this.listener = listener;
  74 + }
  75 +
  76 + private void orientation_change(int orientation) {
  77 + if (listener != null) listener.onOrientationChange(orientation);
  78 + }
  79 +
  80 +}
app/src/main/java/com/cnlive/gundam/stream/ui/activity/BaseStreamActivity.java
@@ -48,6 +48,8 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal @@ -48,6 +48,8 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal
48 48
49 private final static int PERMISSION_REQUEST_CAMERA_AUDIOREC = 1; 49 private final static int PERMISSION_REQUEST_CAMERA_AUDIOREC = 1;
50 50
  51 + private final static String path = Environment.getExternalStorageDirectory() + File.separator + "cnlive_logo.png";
  52 +
51 @Override 53 @Override
52 protected void onCreate(@Nullable Bundle savedInstanceState) { 54 protected void onCreate(@Nullable Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState); 55 super.onCreate(savedInstanceState);
@@ -55,10 +57,7 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal @@ -55,10 +57,7 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal
55 mHandler = new Handler(this); 57 mHandler = new Handler(this);
56 } 58 }
57 59
58 - protected void initStreamer(boolean landScape, GLSurfaceView glSurfaceView) {  
59 - setRequestedOrientation(landScape ?  
60 - ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE :  
61 - ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 60 + protected void initStreamer(GLSurfaceView glSurfaceView) {
62 mStreamer = new Streamer(this); 61 mStreamer = new Streamer(this);
63 //设置预览编码帧率 62 //设置预览编码帧率
64 mStreamer.setPreviewFps(15); 63 mStreamer.setPreviewFps(15);
@@ -75,19 +74,13 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal @@ -75,19 +74,13 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal
75 //设置编码模式 74 //设置编码模式
76 mStreamer.setEncodeMethod(IStreamer.ENCODE_METHOD_SOFTWARE_COMPAT); 75 mStreamer.setEncodeMethod(IStreamer.ENCODE_METHOD_SOFTWARE_COMPAT);
77 //设置横竖屏方向 76 //设置横竖屏方向
78 - mStreamer.setRotateDegrees(landScape ? 90 : 0);  
79 - if (landScape) {  
80 - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏  
81 - } else {  
82 - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏  
83 - } 77 + mStreamer.setRotateDegrees(getOrientationAngle());
84 //设置预览view 78 //设置预览view
85 mStreamer.setDisplayPreview(glSurfaceView); 79 mStreamer.setDisplayPreview(glSurfaceView);
86 //设置前置摄像头镜像 80 //设置前置摄像头镜像
87 mStreamer.setFrontCameraMirror(true); 81 mStreamer.setFrontCameraMirror(true);
88 //设置水印 82 //设置水印
89 - String path = Environment.getExternalStorageDirectory() + File.separator + "cnlive_logo.png";  
90 - mStreamer.showWaterMarkLogo(path, 0.03f, 0.02f, landScape ? 0.0911f : 0.15f, landScape ? 0.0545f : 0.033f, 1f); 83 + mStreamer.showWaterMarkLogo(path, 0.03f, 0.02f, isLandscape() ? 0.0911f : 0.15f, isLandscape() ? 0.0545f : 0.033f, 1f);
91 //设置视频编码是的帧间隔,默认3秒 84 //设置视频编码是的帧间隔,默认3秒
92 mStreamer.setIFrameInterval(2); 85 mStreamer.setIFrameInterval(2);
93 //设置音频通道 86 //设置音频通道
@@ -100,6 +93,32 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal @@ -100,6 +93,32 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal
100 mStreamer.setOnErrorListener(mErrorListener); 93 mStreamer.setOnErrorListener(mErrorListener);
101 } 94 }
102 95
  96 + protected int getOrientationAngle() {
  97 + switch (getRequestedOrientation()) {
  98 + case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
  99 + return 0;
  100 + case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
  101 + return 90;
  102 + case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
  103 + return 270;
  104 + default:
  105 + return 0;
  106 + }
  107 + }
  108 +
  109 + protected boolean isLandscape() {
  110 + int angle = getOrientationAngle();
  111 + return angle == 90 || angle == 270;
  112 + }
  113 +
  114 + protected void orientationChange() {
  115 + //设置横竖屏方向
  116 + mStreamer.setRotateDegrees(getOrientationAngle());
  117 + //水印重绘
  118 + mStreamer.hideWaterMarkLogo();
  119 + mStreamer.showWaterMarkLogo(path, 0.03f, 0.02f, isLandscape() ? 0.0911f : 0.15f, isLandscape() ? 0.0545f : 0.033f, 1f);
  120 + }
  121 +
103 protected void switchCamera() { 122 protected void switchCamera() {
104 mStreamer.switchCamera(); 123 mStreamer.switchCamera();
105 } 124 }
app/src/main/java/com/cnlive/gundam/stream/ui/activity/StreamActivity.java
@@ -5,6 +5,7 @@ import android.app.Dialog; @@ -5,6 +5,7 @@ import android.app.Dialog;
5 import android.content.Context; 5 import android.content.Context;
6 import android.content.DialogInterface; 6 import android.content.DialogInterface;
7 import android.content.Intent; 7 import android.content.Intent;
  8 +import android.content.res.Configuration;
8 import android.databinding.DataBindingUtil; 9 import android.databinding.DataBindingUtil;
9 import android.os.Bundle; 10 import android.os.Bundle;
10 import android.os.Handler; 11 import android.os.Handler;
@@ -21,6 +22,7 @@ import android.widget.TextView; @@ -21,6 +22,7 @@ import android.widget.TextView;
21 22
22 import com.cnlive.gundam.R; 23 import com.cnlive.gundam.R;
23 import com.cnlive.gundam.databinding.ActivityStreamPortraitBinding; 24 import com.cnlive.gundam.databinding.ActivityStreamPortraitBinding;
  25 +import com.cnlive.gundam.main.utils.AccelerometerSensor;
24 import com.cnlive.gundam.stream.model.ChatItem; 26 import com.cnlive.gundam.stream.model.ChatItem;
25 import com.cnlive.gundam.stream.model.ExtensionModel; 27 import com.cnlive.gundam.stream.model.ExtensionModel;
26 import com.cnlive.gundam.stream.model.ShowView; 28 import com.cnlive.gundam.stream.model.ShowView;
@@ -43,7 +45,8 @@ import org.json.JSONObject; @@ -43,7 +45,8 @@ import org.json.JSONObject;
43 import java.util.ArrayList; 45 import java.util.ArrayList;
44 import java.util.List; 46 import java.util.List;
45 47
46 -public class StreamActivity extends BaseStreamActivity implements ChatConnect.ChatCallback, TimerUtil.TimerListener { 48 +public class StreamActivity extends BaseStreamActivity implements ChatConnect.ChatCallback,
  49 + TimerUtil.TimerListener, AccelerometerSensor.ScreenOrientationListener {
47 50
48 private boolean isFace = false; 51 private boolean isFace = false;
49 private boolean isFlash = false; 52 private boolean isFlash = false;
@@ -56,7 +59,6 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch @@ -56,7 +59,6 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch
56 private String channelId; 59 private String channelId;
57 private String channelName; 60 private String channelName;
58 private String coverImgUrl; 61 private String coverImgUrl;
59 - private boolean isLandscape = false;  
60 62
61 private Dialog endDialog; 63 private Dialog endDialog;
62 private ActivityStreamPortraitBinding binding; 64 private ActivityStreamPortraitBinding binding;
@@ -72,13 +74,14 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch @@ -72,13 +74,14 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch
72 private TimerUtil timerUtil; 74 private TimerUtil timerUtil;
73 private String extensions; 75 private String extensions;
74 76
75 - public static void start(Context context, String activityId, String channelId, String channelName, String coverImgUrl, boolean landscage) { 77 + private AccelerometerSensor accelerometerSensor;
  78 +
  79 + public static void start(Context context, String activityId, String channelId, String channelName, String coverImgUrl) {
76 Intent intent = new Intent(context, StreamActivity.class); 80 Intent intent = new Intent(context, StreamActivity.class);
77 intent.putExtra("activityId", activityId); 81 intent.putExtra("activityId", activityId);
78 intent.putExtra("channelId", channelId); 82 intent.putExtra("channelId", channelId);
79 intent.putExtra("channelName", channelName); 83 intent.putExtra("channelName", channelName);
80 intent.putExtra("coverImgUrl", coverImgUrl); 84 intent.putExtra("coverImgUrl", coverImgUrl);
81 - intent.putExtra("landscape", landscage);  
82 context.startActivity(intent); 85 context.startActivity(intent);
83 } 86 }
84 87
@@ -93,7 +96,7 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch @@ -93,7 +96,7 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch
93 initData(); 96 initData();
94 97
95 binding = DataBindingUtil.setContentView(this, R.layout.activity_stream_portrait); 98 binding = DataBindingUtil.setContentView(this, R.layout.activity_stream_portrait);
96 - initStreamer(isLandscape, binding.cameraPreview); 99 + initStreamer(binding.cameraPreview);
97 showView.setStartBtn(true); 100 showView.setStartBtn(true);
98 showView.setRetryLayout(false); 101 showView.setRetryLayout(false);
99 showView.setEndLayout(false); 102 showView.setEndLayout(false);
@@ -106,6 +109,21 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch @@ -106,6 +109,21 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch
106 chatItemList = new ArrayList<>(); 109 chatItemList = new ArrayList<>();
107 110
108 timerUtil = new TimerUtil(this); 111 timerUtil = new TimerUtil(this);
  112 +
  113 + accelerometerSensor = new AccelerometerSensor(this);
  114 + accelerometerSensor.setScreenOrientationListener(this);
  115 + }
  116 +
  117 +
  118 + @Override
  119 + public void onOrientationChange(int orientation) {
  120 + setRequestedOrientation(orientation);
  121 + }
  122 +
  123 + @Override
  124 + public void onConfigurationChanged(Configuration newConfig) {
  125 + super.onConfigurationChanged(newConfig);
  126 + orientationChange();
109 } 127 }
110 128
111 private void initData() { 129 private void initData() {
@@ -113,7 +131,6 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch @@ -113,7 +131,6 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch
113 channelId = getIntent().getStringExtra("channelId"); 131 channelId = getIntent().getStringExtra("channelId");
114 channelName = getIntent().getStringExtra("channelName"); 132 channelName = getIntent().getStringExtra("channelName");
115 coverImgUrl = getIntent().getStringExtra("coverImgUrl"); 133 coverImgUrl = getIntent().getStringExtra("coverImgUrl");
116 - isLandscape = getIntent().getBooleanExtra("landscape", false);  
117 134
118 ExtensionModel extensionModel = new ExtensionModel(channelName, coverImgUrl); 135 ExtensionModel extensionModel = new ExtensionModel(channelName, coverImgUrl);
119 extensions = new Gson().toJson(extensionModel); 136 extensions = new Gson().toJson(extensionModel);
@@ -276,7 +293,7 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch @@ -276,7 +293,7 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch
276 293
277 @Override 294 @Override
278 public int getIsHorizontalScreen() { 295 public int getIsHorizontalScreen() {
279 - return isLandscape ? 1 : 0; 296 + return isLandscape() ? 1 : 0;
280 } 297 }
281 298
282 @Override 299 @Override
@@ -315,12 +332,14 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch @@ -315,12 +332,14 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch
315 super.onPause(); 332 super.onPause();
316 if (endDialog != null) endDialog.dismiss(); 333 if (endDialog != null) endDialog.dismiss();
317 if (timerUtil != null) timerUtil.cancelTimer(); 334 if (timerUtil != null) timerUtil.cancelTimer();
  335 + if (accelerometerSensor != null) accelerometerSensor.unregister();
318 } 336 }
319 337
320 @Override 338 @Override
321 protected void onResume() { 339 protected void onResume() {
322 super.onResume(); 340 super.onResume();
323 if (timerUtil != null) timerUtil.startTimer(0, TimerUtil.UPDATE_ONLINE_PERSON); 341 if (timerUtil != null) timerUtil.startTimer(0, TimerUtil.UPDATE_ONLINE_PERSON);
  342 + if (accelerometerSensor != null) accelerometerSensor.register();
324 } 343 }
325 344
326 @Override 345 @Override
app/src/main/java/com/cnlive/gundam/stream/util/FileUtil.java 0 → 100644
  1 +package com.cnlive.gundam.stream.util;
  2 +
  3 +import android.content.Context;
  4 +import android.content.res.AssetManager;
  5 +import android.os.Environment;
  6 +import android.util.Log;
  7 +
  8 +import java.io.FileOutputStream;
  9 +import java.io.InputStream;
  10 +import java.io.OutputStream;
  11 +
  12 +/**
  13 + * Created by xiansong on 2017/6/30.
  14 + */
  15 +
  16 +public class FileUtil {
  17 + public static void copyFile(Context context, String filename) {
  18 + AssetManager assetManager = context.getAssets();
  19 +
  20 + InputStream in = null;
  21 + OutputStream out = null;
  22 + try {
  23 + in = assetManager.open(filename);
  24 + String newFileName = Environment.getExternalStorageDirectory() + java.io.File.separator + filename;
  25 + out = new FileOutputStream(newFileName);
  26 +
  27 + byte[] buffer = new byte[1024];
  28 + int read;
  29 + while ((read = in.read(buffer)) != -1) {
  30 + out.write(buffer, 0, read);
  31 + }
  32 + in.close();
  33 + in = null;
  34 + out.flush();
  35 + out.close();
  36 + out = null;
  37 + } catch (Exception e) {
  38 + Log.e("tag", e.getMessage());
  39 + }
  40 + }
  41 +}