java后台程序员转android 《三》之 集成腾讯云 云直播 直播推流端,接受展示端,后台server端 及采坑记录
16lz
2021-01-26
目录
前言:
1.腾讯云直播文档:
2.demo 地址:
3.android 主播端实例代码如下:
3.1.activity.xml
3.2.java代码
4.查看主播端图片效果:
5.通过webview 的方式 实现html客户端查看直播
5.1html 代码
6.查看 直播客户端图片效果
7.集成server 后台服务派发URL(防盗链签名)
7.1用来生成直播验证代码签名
7.2介绍:
8.总结
前言:
最近公司打算做先关android 方面的技术,然后招andirod 还挺贵,老板打算让我来搞搞,那就搞一搞,顺便做些总结,从java后台转型快速入手 android 的小策略。
andoird 官网地址 https://developer.android.google.cn/
1.腾讯云直播文档:
直播文档接入:https://cloud.tencent.com/document/product/267/30559
android 集成 :https://cloud.tencent.com/document/product/454/14407
android 集成接入 :https://cloud.tencent.com/document/product/454/7877
2.demo 地址:
- https://download.csdn.net/download/weixin_42749765/10989862
- 功能包括:开启直播,推流,关闭直播,切换摄像头
- 一些其他的功能可以通过文档中自行加入
3.android 主播端实例代码如下:
3.1.activity.xml
<?xml version="1.0" encoding="utf-8"?>
3.2.java代码
package com.example.appvediotxy;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import com.tencent.imsdk.TIMCallBack;import com.tencent.imsdk.TIMConversation;import com.tencent.imsdk.TIMConversationType;import com.tencent.imsdk.TIMLogLevel;import com.tencent.imsdk.TIMManager;import com.tencent.imsdk.TIMMessage;import com.tencent.imsdk.TIMSdkConfig;import com.tencent.imsdk.TIMTextElem;import com.tencent.imsdk.TIMValueCallBack;import com.tencent.rtmp.TXLivePlayer;import com.tencent.rtmp.TXLivePushConfig;import com.tencent.rtmp.TXLivePusher;import com.tencent.rtmp.ui.TXCloudVideoView;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //推流 private TXLivePusher mLivePusher; private TXLivePushConfig mLivePushConfig; //推流地址音频 推流域名从腾讯云服务器上获取 private String rtmpUrl = "rtmp://推流域名/live/222?txSecret=36d31b175e76b7e3b87e2e5e4e225f71&txTime=5C75627F"; private TXLivePlayer mLivePlayer; private TXCloudVideoView mView; //推流地址视频 private String flvUrl = "http://推流域名/live/222.flv"; //初始化直播 public void initInfo(){ //从服务端获取直播信息 statuplive(); } //开启直播推流 public void clickLiveStart(View v) { statuplive(); } //开启直播 public void statuplive(){ //推流 mLivePusher = new TXLivePusher(MainActivity.this);//获取开启 mLivePushConfig = new TXLivePushConfig();//配置内容 mLivePusher.setConfig(mLivePushConfig); //加载配置 mLivePusher.startPusher(rtmpUrl);//开启推流 //视频顺便展示在当前界面上 TXCloudVideoView mCaptureView = (TXCloudVideoView) findViewById(R.id.video_view); mLivePusher.startCameraPreview(mCaptureView); mView = (TXCloudVideoView) findViewById(R.id.video_view); mLivePlayer = new TXLivePlayer(MainActivity.this);//创建 player 对象 mLivePlayer.setPlayerView(mView);//关键 player 对象与界面 view mLivePlayer.startPlay(flvUrl, TXLivePlayer.PLAY_TYPE_LIVE_FLV); //推荐 FLV } //关闭直播结束推流 public void clickLiveShutdown(View v){ mLivePusher.stopCameraPreview(true); //停止摄像头预览 mLivePusher.stopPusher(); //停止推流 mLivePusher.setPushListener(null); //解绑 listener } //切换前后摄像头 public void clickliveqhCamera(View v){ // 默认是前置摄像头 mLivePusher.switchCamera(); } //关闭直播 @Override public void onDestroy() { super.onDestroy(); mLivePlayer.stopPlay(true); // true 代表清除最后一帧画面 mView.onDestroy(); }}
4.查看主播端图片效果:
5.通过webview 的方式 实现html客户端查看直播
5.1html 代码
liveClient ;
6.查看 直播客户端图片效果
7.集成server 后台服务派发URL(防盗链签名)
- 文档:https://cloud.tencent.com/document/product/454/9875
- 去控制台获取推流防盗链key 就是下边代码 7d5xxxxxxxxxxxxxxxxxf03c8e
7.1用来生成直播验证代码签名
package com.supermap.test;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.text.ParseException;import java.text.SimpleDateFormat;import java.time.Instant;import java.time.LocalDateTime;import java.time.LocalTime;import java.time.ZoneId;import java.util.Date;/** * 生成 直播验证内容 * @author yushen * */public class TestTxyLive {public static void main(String[] args) {Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Long maxTime = dateToTimestamp(sdf.format(getEndOfDay(date)).toString());String newurl = getSafeUrl("7d5xxxxxxxxxxxxxxxxxf03c8e", "222", maxTime);System.out.println(newurl);}//时间转时间戳public static long dateToTimestamp(String time) {SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try {Date date = simpleDateFormat.parse(time);long ts = date.getTime() / 1000;return ts;} catch (ParseException e) {return 0;}}// 获得某天最大时间 2017-10-15 23:59:59public static Date getEndOfDay(Date date) {LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()),ZoneId.systemDefault());LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());}private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd','e', 'f' };/* * KEY+ streamName + txTime */private static String getSafeUrl(String key, String streamName, long txTime) {String input = new StringBuilder().append(key).append(streamName).append(Long.toHexString(txTime).toUpperCase()).toString();String txSecret = null;try {MessageDigest messageDigest = MessageDigest.getInstance("MD5");txSecret = byteArrayToHexString(messageDigest.digest(input.getBytes("UTF-8")));} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();}return txSecret == null ? "": new StringBuilder().append("txSecret=").append(txSecret).append("&").append("txTime=").append(Long.toHexString(txTime).toUpperCase()).toString();}private static String byteArrayToHexString(byte[] data) {char[] out = new char[data.length << 1];for (int i = 0, j = 0; i < data.length; i++) {out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];out[j++] = DIGITS_LOWER[0x0F & data[i]];}return new String(out);}}
7.2介绍:
- 执行完后
- 会生成这么个东东
- txSecret=c072522e6b96e521cdcfebce5cbb1b86&txTime=5C7AA87F
- 用来验证安全的
- 将这个签名放到推流地址的后边就ok楼
8.总结
- 1.第一步查看官方文档
- 2.实现主播端android 开发
- 3.实现客户端html的查看视频
- 4.实现服务端的配置推流安全防盗链
- ok
持续更新!
更多相关文章
- Android(安卓)“退一步”的布局加载优化
- 火线扫描Android静态代码
- 你真的了解Instant Run吗?
- Android(安卓)PC端截图源代码
- Android(安卓)NDK开发: 通过C/C++调用第三方so库
- Android(安卓)来电(包括铃声),短信拦截的实现方法
- 从零开始--系统深入学习android(实践-让我们开始写代码-Android框
- 在android中使用proguard混淆代码出现“Conversion to Dalvik fo
- 联网疯狂坦克简陋版