directory

Preface:

1. Tencent Cloud Live Document:

2. The demo address:

3. The example code of Android anchor side is as follows:

3.1. The activity. The XML

3.2. Java code

4. Check the image effect of the anchor side:

5. Through webview, HTML client can view live broadcast

5.1 HTML code

6. View the image effect of the live broadcast client

7. Integrate server background service to distribute URL (anti-theft chain signature)

7.1 Used to generate live verification code signatures

7.2 introduction:

8. To summarize


 

 

Preface:

Recently, the company plans to first learn android technology, and then recruit Andirod, which is quite expensive. The boss wants me to do it, so I will do it, and by the way, make some summaries, and quickly start android strategy from Java background transformation.

 

Andoird website address developer. The android. Google. Cn /

 

1. Tencent Cloud Live Document:

Live document access: cloud.tencent.com/document/pr…

Android integration: cloud.tencent.com/document/pr…

Android integration access: cloud.tencent.com/document/pr…

 

2. The demo address:

  • Download.csdn.net/download/we…
  • Functions include: open live broadcast, push stream, close live broadcast, switch camera
  • Some of the additional functionality can be self-documented

 

3. The example code of Android anchor side is as follows:

3.1. The activity. The XML

<? The XML version = "1.0" encoding = "utf-8"? > <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#DC4D41" tools:context=".MainActivity"> <Button android:id="@+id/s1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginLeft="16dp" Android :layout_marginBottom="10dp" Android :onClick="clickLiveStart" Android :text=" enable" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/s4" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_marginStart="10dp" android:layout_marginLeft="10dp" android:layout_marginBottom="10dp" android:onClick="clickLiveShutdown" Android :text=" close "app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@+id/s5" /> <Button android:id="@+id/s5" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_marginStart="10dp" android:layout_marginLeft="10dp" android:layout_marginBottom="10dp" Android :onClick="clickliveqhCamera" Android :text=" Switch camera "app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@+id/s1" /> <com.tencent.rtmp.ui.TXCloudVideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:visibility="gone" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginLeft="20dp" Android :layout_marginTop="20dp" Android :text=" exit "app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" Android :layout_height="wrap_content" Android :layout_marginTop="25dp" Android :text=" # FFF "android:layout_height="wrap_content" Android :layout_marginTop="25dp" Android :text=" # FFF" android:textSize="18sp" app:layout_constraintTop_toTopOf="parent" tools:layout_editor_absoluteX="196dp" /> </android.support.constraint.ConstraintLayout>Copy the code

 

3.2. Java code

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 :// RTMP /live/222? txSecret=36d31b175e76b7e3b87e2e5e4e225f71&txTime=5C75627F"; private TXLivePlayer mLivePlayer; private TXCloudVideoView mView; Private String flvUrl = "http://twitter.com/live/222.flv "; private String flvUrl =" http://twitter.com/live/222.flv "; Public void initInfo(){// Get live broadcast information from the server statuplive(); Public void clickLiveStart(View v) {statuplive(); } public void statuplive(){mLivePusher = new TXLivePusher(mainActivity.this); MLivePushConfig = new TXLivePushConfig(); // Configure the content mlivepusher.setConfig (mLivePushConfig); // Load the configuration mlivePusher. startPusher(rtmpUrl); TXCloudVideoView mCaptureView = (TXCloudVideoView) findViewById(r.id.ideo_view); mLivePusher.startCameraPreview(mCaptureView); mView = (TXCloudVideoView) findViewById(R.id.video_view); mLivePlayer = new TXLivePlayer(MainActivity.this); / / create the player object mLivePlayer. SetPlayerView (mView); View mlivePlayer.startPlay (flvUrl, txliveplayer.play_type_live_flv); Recommended FLV} / / / / close the broadcast public push flow void clickLiveShutdown (View v) {mLivePusher. StopCameraPreview (true); // Stop camera preview mlivepusher.stoppusher (); / / stop pushing flow mLivePusher. SetPushListener (null). Public void clickliveqhCamera(View v){mlivePusher.switchCamera (); } @override public void onDestroy() {super.ondestroy (); mLivePlayer.stopPlay(true); // true clears the last frame mview.ondestroy (); }}Copy the code

 

4. Check the image effect of the anchor side:

 

5. Through webview, HTML client can view live broadcast

 

5.1 HTML code

<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, <meta HTTP-equiv =" x-UA-compatible "content="ie=edge"> <title>liveClient</title> <! <script type="text/javascript" SRC =".. / libs/jquery. Js "> < / script > < script SRC =" / / imgcache.qq.com/open/qcloud/video/vcplayer/TcPlayer-2.2.1.js" charset="utf-8"></script>; </head> <body> <! <div id="id_test_video" style="width:100%; height:auto;" ></div> <div class="demoA"></div> </div> </body> <script> $(document).ready(function(){// Check the live module var nowimgUrl = "http://pic40.photophoto.cn/20160710/1155115962721749_b.jpg"; Var player = new TcPlayer (' id_test_video '{" m3u8 ":" http://abc.xxx.com/live/222.m3u8 ", / / please replace the actual usable broadcast address "the autoplay" : {"style":"cover"," SRC ":nowimgUrl}, "width" : '480',// video display width, please use the video resolution width "height" : '320',// video display height, please use the video resolution height}); }); </script> </html>Copy the code

 

6. View the image effect of the live broadcast client

 

7. Integrate server background service to distribute URL (anti-theft chain signature)

  • Documents: cloud.tencent.com/document/pr…
  • To the console access to push flow hotlinking prevention is the key code below 7 d5xxxxxxxxxxxxxxxxxf03c8e

 

7.1 Used to generate live verification code signatures

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; 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; Public 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); }}Copy the code

7.2 introduction:

  •  
  • After the execution
  • It’s going to produce something like this
  • txSecret=c072522e6b96e521cdcfebce5cbb1b86&txTime=5C7AA87F
  • To verify security
  • Place this signature after the stream address to create the OK building

 

8. To summarize

  • 1. The first step is to check official documents
  • 2. Realize android development of anchor terminal
  • 3. Realize the client HTML view video
  • 4. Configure the server to push traffic security and prevent chain theft
  • ok

 

 

Keep updating!