Commit a008f33d authored by 代凡's avatar 代凡
Browse files

添加native播放界面及逻辑

parent 75f2c006
......@@ -95,14 +95,15 @@ def enableProguardInReleaseBuilds = false
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "com.polyvrncloudclassdemo"
applicationId "com.easefun.polyvsdk.cloudclass"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
multiDexEnabled true
ndk {
abiFilters "armeabi-v7a", "x86"
}
......@@ -134,9 +135,17 @@ android {
}
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
compile project(':react-native-gesture-handler')
compile project(':react-native-reanimated')
api project(':polyvsdk')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
implementation "com.facebook.react:react-native:+" // From node_modules
......
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.polyvrncloudclassdemo">
xmlns:tools="http://schemas.android.com/tools"
package="com.easefun.polyvsdk.cloudclass">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:allowBackup="false"
android:theme="@style/AppTheme">
android:name=".MainApplication"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@drawable/ic_launcher"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
tools:replace="android:icon,android:theme">
<activity
android:name=".MainActivity"
android:name="com.easefun.polyvsdk.cloudclass.MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
......
package com.polyvrncloudclassdemo;
package com.easefun.polyvsdk.cloudclass;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
public class MainActivity extends ReactActivity {
......@@ -12,4 +15,14 @@ public class MainActivity extends ReactActivity {
protected String getMainComponentName() {
return "PolyvRNCloudClassDemo";
}
@Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
@Override
protected ReactRootView createRootView() {
return new RNGestureHandlerEnabledRootView(MainActivity.this);
}
};
}
}
package com.polyvrncloudclassdemo;
package com.easefun.polyvsdk.cloudclass;
import android.app.Application;
import com.blankj.utilcode.util.AppUtils;
import com.easefun.polyv.cloudclassdemo.PolyvCloudClassApp;
import com.easefun.polyv.foundationsdk.utils.PolyvAppUtils;
import com.easefun.polyvsdk.cloudclass.rn.PolyvCloudClassRNPackage;
import com.facebook.react.ReactApplication;
import com.swmansion.gesturehandler.react.RNGestureHandlerPackage;
import com.swmansion.reanimated.ReanimatedPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
......@@ -11,7 +17,7 @@ import com.facebook.soloader.SoLoader;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
public class MainApplication extends PolyvCloudClassApp implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
......@@ -22,7 +28,10 @@ public class MainApplication extends Application implements ReactApplication {
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage()
new MainReactPackage(),
new RNGestureHandlerPackage(),
new ReanimatedPackage(),
new PolyvCloudClassRNPackage()
);
}
......@@ -39,6 +48,7 @@ public class MainApplication extends Application implements ReactApplication {
@Override
public void onCreate() {
PolyvAppUtils.init(this);
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
}
......
package com.easefun.polyvsdk.cloudclass;
import android.app.Application;
import android.content.Context;
import com.easefun.polyv.businesssdk.vodplayer.PolyvVodSDKClient;
import com.easefun.polyv.cloudclass.config.PolyvLiveSDKClient;
import com.easefun.polyv.foundationsdk.log.PolyvCommonLog;
/**
* @author df
* @create 2019/9/6
* @Describe
*/
public class PolyvInitManager {
public static void init(String vodKey, String decodeKey, String decodeIv, Context context){
// Normal app init code...
PolyvCommonLog.setDebug(true);
PolyvLiveSDKClient liveSDKClient = PolyvLiveSDKClient.getInstance();
liveSDKClient.initContext(null);
PolyvVodSDKClient client = PolyvVodSDKClient.getInstance();
//使用SDK加密串来配置
client.setConfig(vodKey, decodeKey, decodeIv);
}
}
package com.easefun.polyvsdk.cloudclass.rn;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import com.blankj.utilcode.util.ToastUtils;
import com.easefun.polyv.businesssdk.PolyvChatDomainManager;
import com.easefun.polyv.businesssdk.model.chat.PolyvChatDomain;
import com.easefun.polyv.businesssdk.model.video.PolyvPlayBackVO;
import com.easefun.polyv.businesssdk.service.PolyvLoginManager;
import com.easefun.polyv.businesssdk.vodplayer.PolyvVodSDKClient;
import com.easefun.polyv.cloudclass.config.PolyvLiveSDKClient;
import com.easefun.polyv.cloudclass.model.PolyvLiveStatusVO;
import com.easefun.polyv.cloudclass.net.PolyvApiManager;
import com.easefun.polyv.cloudclassdemo.login.PolyvCloudClassLoginActivity;
import com.easefun.polyv.cloudclassdemo.watch.PolyvCloudClassHomeActivity;
import com.easefun.polyv.foundationsdk.log.PolyvCommonLog;
import com.easefun.polyv.foundationsdk.net.PolyvResponseBean;
import com.easefun.polyv.foundationsdk.net.PolyvResponseExcutor;
import com.easefun.polyv.foundationsdk.net.PolyvrResponseCallback;
import com.easefun.polyv.linkmic.PolyvLinkMicClient;
import com.easefun.polyvsdk.cloudclass.PolyvInitManager;
import com.easefun.polyvsdk.cloudclass.R;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import java.io.IOException;
import io.reactivex.disposables.Disposable;
import retrofit2.adapter.rxjava2.HttpException;
/**
* @author df
* @create 2019/9/6
* @Describe
*/
public class PolyvCloudClassRNModule extends ReactContextBaseJavaModule {
private static final String TAG = "PolyvRNCloudClassLoginModule";
private ProgressDialog progress;
private Disposable getTokenDisposable, verifyDispose;
public PolyvCloudClassRNModule(ReactApplicationContext reactContext) {
super(reactContext);
}
private void initProgress() {
progress = new ProgressDialog(getCurrentActivity());
progress.setMessage(getReactApplicationContext().getResources().getString(R.string.login_waiting));
progress.setCanceledOnTouchOutside(false);
progress.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (getTokenDisposable != null) {
getTokenDisposable.dispose();
}
if (verifyDispose != null) {
verifyDispose.dispose();
}
}
});
}
@Override
public String getName() {
return TAG;
}
@ReactMethod
public void init(String vodKey, String decodeKey, String decodeIv, String viewerId, String nickName, Promise promise) {
initProgress();
int code = PolyvErrorCode.success;
if (TextUtils.isEmpty(viewerId)) {
code = PolyvErrorCode.noViewId;
} else if (TextUtils.isEmpty(vodKey)) {
code = PolyvErrorCode.noVodKey;
} else if (TextUtils.isEmpty(decodeKey)) {
code = PolyvErrorCode.noDecodeKey;
} else if (TextUtils.isEmpty(decodeIv)) {
code = PolyvErrorCode.noDecodeIv;
}
if (code == PolyvErrorCode.success) {
PolyvInitManager.init(vodKey, decodeKey, decodeIv, getReactApplicationContext());
WritableMap map = Arguments.createMap();
map.putInt("code", PolyvErrorCode.success);
promise.resolve(map);
} else {
getErrorMessage(promise, code);
}
}
private void getErrorMessage(Promise promise, int code) {
String errorCode = "" + code;
String errorDesc = PolyvErrorCode.getDesc(code);
Throwable throwable = new Throwable(errorDesc);
Log.e(TAG, "errorCode=" + errorCode + " errorDesc=" + errorDesc);
promise.reject(errorCode, errorDesc, throwable);
}
@ReactMethod
public void playbackLogin(int handler, String userId, String channelId, String vid, String appId, String appScrect, Promise promise) {
int code = PolyvErrorCode.success;
if (TextUtils.isEmpty(userId)) {
code = PolyvErrorCode.noUserId;
} else if (TextUtils.isEmpty(channelId)) {
code = PolyvErrorCode.noChannelId;
} else if (TextUtils.isEmpty(vid)) {
code = PolyvErrorCode.noVideoId;
} else if (TextUtils.isEmpty(appId)) {
code = PolyvErrorCode.noAppId;
} else if (TextUtils.isEmpty(appScrect)) {
code = PolyvErrorCode.noAppScrect;
}
if(code == PolyvErrorCode.success){
progress.show();
checkToken(userId,null,channelId,vid,appId,false);
}else {
getErrorMessage(promise, code);
}
}
@ReactMethod
public void liveLogin(int handler, String userId, String channelId, String appId, String appScrect, Promise promise) {
int code = PolyvErrorCode.success;
if (TextUtils.isEmpty(userId)) {
code = PolyvErrorCode.noUserId;
} else if (TextUtils.isEmpty(channelId)) {
code = PolyvErrorCode.noChannelId;
} else if (TextUtils.isEmpty(appId)) {
code = PolyvErrorCode.noAppId;
} else if (TextUtils.isEmpty(appScrect)) {
code = PolyvErrorCode.noAppScrect;
}
if(code == PolyvErrorCode.success){
progress.show();
checkToken(userId,appScrect,channelId,null,appId,true);
}else {
getErrorMessage(promise, code);
}
}
private void checkToken(String userId, String appSecret, String channel, String vid, String appId, boolean live) {
//请求token接口
getTokenDisposable = PolyvLoginManager.checkLoginToken(userId, appSecret, appId,
channel, vid,
new PolyvrResponseCallback<PolyvChatDomain>() {
@Override
public void onSuccess(PolyvChatDomain responseBean) {
PolyvLinkMicClient.getInstance().setAppIdSecret(appId, appSecret);
PolyvLiveSDKClient.getInstance().setAppIdSecret(appId, appSecret);
PolyvVodSDKClient.getInstance().initConfig(appId, appSecret);
if (!live) {
requestPlayBackStatus(userId,channel, vid);
return;
}
requestLiveStatus(userId, channel);
PolyvChatDomainManager.getInstance().setChatDomain(responseBean);
}
@Override
public void onFailure(PolyvResponseBean<PolyvChatDomain> responseBean) {
super.onFailure(responseBean);
failedStatus(responseBean.getMessage());
}
@Override
public void onError(Throwable e) {
super.onError(e);
errorStatus(e);
}
});
}
private void requestPlayBackStatus(String userId,String channelId, String vid) {
if (TextUtils.isEmpty(vid)) {
return;
}
verifyDispose = PolyvLoginManager.getPlayBackType(vid, new PolyvrResponseCallback<PolyvPlayBackVO>() {
@Override
public void onSuccess(PolyvPlayBackVO playBack) {
boolean isLivePlayBack = playBack.getLiveType() == 0;
startActivityForPlayback(vid,channelId,userId, isLivePlayBack);
}
@Override
public void onFailure(PolyvResponseBean<PolyvPlayBackVO> responseBean) {
super.onFailure(responseBean);
failedStatus(responseBean.getMessage());
}
@Override
public void onError(Throwable e) {
super.onError(e);
errorStatus(e);
}
});
}
public void failedStatus(String message) {
ToastUtils.showLong(message);
}
public void errorStatus(Throwable e) {
PolyvCommonLog.exception(e);
progress.dismiss();
if (e instanceof HttpException) {
try {
ToastUtils.showLong(((HttpException) e).response().errorBody().string());
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
ToastUtils.showLong(e.getMessage());
}
}
private void requestLiveStatus(String userId, String channelId) {
verifyDispose = PolyvResponseExcutor.excuteUndefinData(PolyvApiManager.getPolyvLiveStatusApi().geLiveStatusJson(channelId)
, new PolyvrResponseCallback<PolyvLiveStatusVO>() {
@Override
public void onSuccess(PolyvLiveStatusVO statusVO) {
String data = statusVO.getData();
String[] dataArr = data.split(",");
boolean isAlone = "alone".equals(dataArr[1]);//是否有ppt
startActivityForLive(userId, channelId, isAlone);
progress.dismiss();
}
@Override
public void onFailure(PolyvResponseBean<PolyvLiveStatusVO> responseBean) {
super.onFailure(responseBean);
failedStatus(responseBean.getMessage());
}
@Override
public void onError(Throwable e) {
super.onError(e);
errorStatus(e);
}
});
}
private String getTrim(String playbackUserId) {
return playbackUserId.trim();
}
// <editor-fold defaultstate="collapsed" desc="startActivity">
private void startActivityForLive(String userId, String channelId, boolean isAlone) {
PolyvCloudClassHomeActivity.startActivityForLive(getCurrentActivity(),
getTrim(channelId), userId, isAlone);
}
private void startActivityForPlayback(String playbackVideoId,String playbackChannelId,String playbackUserId, boolean isNormalLivePlayBack) {
progress.dismiss();
PolyvCloudClassHomeActivity.startActivityForPlayBack(getCurrentActivity(),
getTrim(playbackVideoId), getTrim(playbackChannelId), getTrim(playbackUserId), isNormalLivePlayBack);
}
// </editor-fold>
}
package com.easefun.polyvsdk.cloudclass.rn;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author df
* @create 2019/9/6
* @Describe
*/
public class PolyvCloudClassRNPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new PolyvCloudClassRNModule(reactContext));
return modules;
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
package com.easefun.polyvsdk.cloudclass.rn;
/**
* @author df
* @create 2019/1/26
* @Describe
*/
public class PolyvErrorCode {
/**
* code,返回码定义:
* 0 成功
* -1 AppId为空
* -2 AppSecret为空
* -3 viewerId为空
* -4 UserId为空
* -5 ChannelId为空
* -6 频道加载失败
*/
public final static int success = 0;
public final static int noAppId = -1;
public final static int noAppScrect = -2;
public final static int noViewId = -3;
public final static int noUserId = -4;
public final static int noChannelId = -5;
public final static int noVideoId = -6;
public final static int noVodKey = -7;
public final static int noDecodeKey = -8;
public final static int noDecodeIv = -9;
public final static int channleLoadFailed = -10;
public static String getDesc(int code) {
switch (code) {
case PolyvErrorCode.success:
return "成功";
case PolyvErrorCode.noAppId:
return "AppId为空";
case PolyvErrorCode.noAppScrect:
return "AppSecret为空";
case PolyvErrorCode.noViewId:
return "viewerId为空";
case PolyvErrorCode.noUserId:
return "UserId为空";
case PolyvErrorCode.noChannelId:
return "ChannelId为空";
case PolyvErrorCode.noVideoId:
return "videoId为空";
case PolyvErrorCode.channleLoadFailed:
return "频道加载失败";
case PolyvErrorCode.noVodKey:
return "VodKey为空";
case PolyvErrorCode.noDecodeKey:
return "DecodeKey为空";
case PolyvErrorCode.noDecodeIv:
return "DecodeIv为空";
default:
return "";
}
}
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext {
buildToolsVersion = "27.0.3"
minSdkVersion = 16
compileSdkVersion = 27
targetSdkVersion = 26
supportLibVersion = "27.1.1"