1.申请Licence,获得 LICENSEURL 和 LICENSEURLKEY
2.创建应用,获得 SDKAPPID 和 SECRETKEY
3.配置 PUSH_DOMAIN (推流域名,tx后台自动生成), PLAY_DOMAIN(拉流域名,要有自己的域名),LIVE_URL_KEY(推流鉴权使用到的key,必须配置)
4.推流url
pushUrl = RTMP + GenerateTestUserSig.PUSH_DOMAIN + File.separator + APP_NAME + File.separator + streamId
+ GenerateTestUserSig.getSafeUrl(streamId);
public static String getSafeUrl(String streamName) {
long txTime = System.currentTimeMillis() / 1000 + 60 * 60;
String input = new StringBuilder().append(LIVE_URL_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 new StringBuilder().append("?txSecret=").append(txSecret).append("&").append("txTime=")
.append(Long.toHexString(txTime).toUpperCase()).toString();
}
5.开始推流
int ret = mLivePusher.startPush(pushUrl);
6.如果是录屏推流,注意权限和前台服务
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
android:foregroundServiceType="mediaProjection"
android:exported="true"
public void startMediaProjectionService(Activity activity){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent(activity,MyCaptureService.class);
startForegroundService(intent);
}
}
public void stopMediaProjectionService(Activity activity){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
Intent intent = new Intent(activity,MyCaptureService.class);
stopService(intent);
}
}
//service里别忘了
startForeground(R.drawable.ic_launcher, notification);
7.拉流url
public static String generatePlayUrl(String streamId, String userId, int type) {
String playUrl = "";
if (type == 0) {
playUrl = RTMP + GenerateTestUserSig.PLAY_DOMAIN + File.separator + APP_NAME + File.separator + streamId;
} else if (type == 1) {
playUrl = HTTP + GenerateTestUserSig.PLAY_DOMAIN + File.separator + APP_NAME + File.separator + streamId
+ ".flv";
} else if (type == 2) {
playUrl = HTTP + GenerateTestUserSig.PLAY_DOMAIN + File.separator + APP_NAME + File.separator + streamId
+ ".m3u8";
} else if (type == 3) {
playUrl =
TRTC + TRTC_DOMAIN + "/play/" + streamId + "?sdkappid=" + GenerateTestUserSig.SDKAPPID + "&userid="
+ userId + "&usersig=" + GenerateTestUserSig.genTestUserSig(userId);
} else if (type == 4) {
playUrl = WEBRTC + GenerateTestUserSig.PLAY_DOMAIN + File.separator + APP_NAME + File.separator + streamId;
}
return playUrl;
}
7.开始拉流
int result = mLivePlayer.startLivePlay(playURL);
网友评论