huangxinbao

delete VideoPublisherActivity.java useless for this project,because major push v…

…ideo function  move to  VideoPlayActivity.java.
package com.mang.xdy.demo.activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.eventhandle.SmartEventCallback;
import com.google.gson.Gson;
import com.mang.xdy.common.Constants;
import com.mang.xdy.core.XdySdk;
import com.mang.xdy.demo.R;
import com.mang.xdy.demo.adapter.SimpleFragmentPagerAdapter;
import com.mang.xdy.demo.bean.AudioPlayBean;
import com.mang.xdy.demo.bean.ChatBean;
import com.mang.xdy.demo.bean.DocEntity;
import com.mang.xdy.demo.bean.ErrorEntity;
import com.mang.xdy.demo.bean.InitClassSuccessEntity;
import com.mang.xdy.demo.bean.JoinClass;
import com.mang.xdy.demo.bean.LiveBean;
import com.mang.xdy.demo.bean.PublisherEntity;
import com.mang.xdy.demo.bean.PublisherVideoExitBean;
import com.mang.xdy.demo.bean.PublisherVideoReturnBean;
import com.mang.xdy.demo.bean.VideoPlayBean;
import com.mang.xdy.demo.utils.JsonUtil;
import com.mang.xdy.demo.utils.ToastUtil;
import com.mang.xdy.demo.widget.dialog.LoginDialog;
import com.mang.xdy.listener.ObserverListener;
import com.mang.xdy.utils.UIUtils;
import com.mang.xdy.utils.XdyLogUtil;
import com.mang.xdy.utils.XdyStringUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class VideoPublisherActivity extends AppCompatActivity implements ObserverListener{
private final static String TAG = "VideoPlayActivity";
@BindView(R.id.btn_videoPlay_pubsherVideo)
Button btnVideoPlayPubsherVideo;
@BindView(R.id.btn_videoPlay_chat)
Button btnVideoPlayChat;
@BindView(R.id.tv_videoPlay_url)
TextView tv_videoPlay_url;
@BindView(R.id.surfaceview_pubisherVideo)
SurfaceView surfaceview_pubisherVideo;
@BindView(R.id.btn_videoPlay_publisherAudio)
Button btn_videoPlay_publisherAudio;
// @BindView(R.id.img_playVideo_novideo)
// ImageView img_playVideo_novideo;
private String username="";
private String userpwd="";
private PersonDialog personDialog;
private SimpleFragmentPagerAdapter pagerAdapter;
private XdySdk xdySdk;
private ViewPager viewPager;
//默认推送视频
private boolean isPusherAudio=false;
/*默认是可以退出的,如果进行推流则需要主动退出*/
private boolean isAbleback=false;
private TabLayout tabLayout;
@BindView(R.id.surfaceview_playVideo)
SurfaceView surfaceviewPlayVideo;
@BindView(R.id.btn_videoPlay_stop)
Button btn_videoPlay_stop;
private String initClass = "";
private String publisherSuccess="";
/*视频成功返回值*/
private PublisherVideoReturnBean publisherVideoReturnBean;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case -2:
//加入课堂输入用户名,密码
joinClass("", (Boolean) msg.obj);
break;
case -1:
//退出
// xdySdk.api("leaveClass", "");
break;
case 0:
parseJoinClass((String) msg.obj,true);
break;
case 7:
break;
case 8:
btnVideoPlayPubsherVideo.setEnabled(true);
btn_videoPlay_publisherAudio.setEnabled(true);
break;
}
}
};
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); //屏幕常亮
setContentView(R.layout.activity_video_publisher);
ButterKnife.bind(this);
btn_videoPlay_publisherAudio.setVisibility(View.GONE);
// setTablayout();
xdySdk=XdySdk.getXdyInstance();
xdySdk.add(this);
init();
XdyLogUtil.e(TAG,"当前线程 main:"+android.os.Process.myPid()+"线程:"+Thread.currentThread().getId()+"");
xdySdk.setOnXdyAsyncMessageLitener(new XdySdk.OnXdyAsyncMessageLitener() {
@Override
public void onXdyAsyncMessageReceiver(String type, String response) {
switch (type) {
case "class_init_success":
ToastUtil.showToastshort("初始化课堂成功", VideoPublisherActivity.this);
Message message = Message.obtain();
message.obj = response;
message.what = 0;
handler.sendMessage(message);
break;
case "class_join_success":
//加入课堂成功
UIUtils.closeDialog(mLoginDialog);
ToastUtil.showToastshort("加入课堂成功", VideoPublisherActivity.this);
//只有加入课堂成功才可以点击推流的按钮
handler.sendEmptyMessage(8);
break;
case "video_stop":
// handler.sendEmptyMessage(7);
break;
case "audio_stop":
break;
}
XdyLogUtil.e(TAG,"type:"+type+" response:"+response);
}
});
}
public void init() {
initClass = getIntent().getStringExtra("init");
xdySdk.api("init", initClass);
}
private LoginDialog mLoginDialog;
//判断解析是否有密码
public void parseJoinClass(String rsponse,boolean isShowPwd){
InitClassSuccessEntity initClassSuccessEntity=JsonUtil.parseJsonToBean(rsponse,InitClassSuccessEntity.class);
if(initClassSuccessEntity!=null) {
if (initClassSuccessEntity.isPasswordRequired()) {
isShowPwd=true;
} else {
isShowPwd=false;
userpwd = "123321";
}
}
if (mLoginDialog == null) {
mLoginDialog = new LoginDialog(this, isShowPwd, null);
mLoginDialog.show();
mLoginDialog.setCanceledOnTouchOutside(false);
final boolean finalIsShowPwd = isShowPwd;
mLoginDialog.setBtnClick(new View.OnClickListener() {
@Override
public void onClick(View view) {
String[] nameAndPwd = mLoginDialog.getNameAndPwd();
joinClass(nameAndPwd[0], nameAndPwd[1], finalIsShowPwd);
}
});
}else if (mLoginDialog != null) {
UIUtils.closeDialog(mLoginDialog);
}
}
//加入课堂
public void joinClass(String username,String userpwd,boolean isShowPwd) {
JoinClass joinClass=new JoinClass();
joinClass.setHasCamera(false);
joinClass.setHasMicrophone(false);
joinClass.setUserName(username);
if(!isShowPwd){
joinClass.setPassword("123321");
}else{
joinClass.setPassword(userpwd);
}
String temp=new Gson().toJson(joinClass);
String jsonParmp = XdyStringUtils.stringToJson(temp, true);
XdyLogUtil.e("加入课堂", jsonParmp);
xdySdk.api("joinClass", jsonParmp);
}
//加入课堂
public void joinClass(String rsponse,boolean isShowPwd) {
// String temp = "{\n" +
// " \"userName\":\"base\",\n" +
// " \"password\":\"123321\",\n" +
// " \"hasCamera\":false,\n" +
// " \"hasMicrophone\":false}";
JoinClass joinClass=new JoinClass();
joinClass.setHasCamera(false);
joinClass.setHasMicrophone(false);
joinClass.setUserName(username);
if(!isShowPwd){
joinClass.setPassword("123321");
}else{
joinClass.setPassword(userpwd);
}
String temp=new Gson().toJson(joinClass);
String jsonParmp = XdyStringUtils.stringToJson(temp, true);
XdyLogUtil.e("加入课堂", jsonParmp);
xdySdk.api("joinClass", jsonParmp);
}
public void sendMeg() {
String temp = "{\n" +
" \"message\": \"固定发送测试 hello\",\n" +
" \"to\": 0\n" +
"}";
String jsonParmp = XdyStringUtils.stringToJson(temp, true);
XdyLogUtil.e(TAG, "聊天" + jsonParmp);
xdySdk.api("sendChatMsg", jsonParmp);
}
/**
* 获取封装的推流地址
*/
public void publisherVideoText(){
xdySdk.api("publishVideo", "", surfaceview_pubisherVideo, VideoPublisherActivity.this);
// xdySdk.api(Constants.PUBLISH_AUDIO, "", surfaceview_pubisherVideo, VideoPublisherActivity.this);
}
@Override
protected void onPause() {
super.onPause();
xdySdk.onPublisherPause();
}
@Override
protected void onResume() {
super.onResume();
xdySdk.onPublisherResume();
}
@Override
protected void onDestroy() {
xdySdk.onPublisherStop();
super.onDestroy();
}
@OnClick({R.id.btn_videoPlay_chat, R.id.btn_videoPlay_pubsherVideo,R.id.btn_videoPlay_publisherAudio,R.id.btn_videoPlay_stop})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_videoPlay_chat:
handler.sendEmptyMessage(4);
break;
case R.id.btn_videoPlay_pubsherVideo:
// getPublish();
publisherVideoText();
// publisherAudioText();
ToastUtil.showToastshort("推送视频",VideoPublisherActivity.this);
btnVideoPlayPubsherVideo.setClickable(false);
break;
case R.id.btn_videoPlay_stop:
ToastUtil.showToastshort("正在退出,请稍等",this);
xdySdk.api("unPublishVideo","");
// finish();
break;
}
}
@Override
public void observerUpData(final String type, final String parameter) {
handler.post(new Runnable() {
@Override
public void run() {
handError(type,parameter);
}
});
}
public void handError(String errorId, String errorMsg) {
ErrorEntity errorEntity = JsonUtil.parseJsonToBean(errorMsg, ErrorEntity.class);
if (errorEntity == null) {
return;
}
switch (errorEntity.getCode() + "") {
case "100":
case "101":
case "102":
case "103":
case "104":
case "105":
case "106":
case "107":
case "200":
case "201":
case "202":
case "203":
case "204":
case "205":
case "206":
case "207":
case "208":
ToastUtil.showToastshort(errorEntity.getReson(), this);
exit();
break;
case "300":
case "301":
break;
case "10000":
ToastUtil.showToastshort(errorEntity.getReson(), this);
break;
case "10001":
ToastUtil.showToastshort(errorEntity.getReson(), this);
break;
case "20000":
isDefaultExit=false;
showErrorDialog();
ToastUtil.showToastshort(errorEntity.getReson(), this);
break;
}
}
class EventHande implements SmartEventCallback {
@Override
public void onCallback(int code, long param1, long param2, String param3, String param4, Object param5) {
switch (code) {
case EVENTID.EVENT_DANIULIVE_ERC_PLAYER_STARTED:
Log.i(TAG, "开始。。");
break;
case EVENTID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTING:
Log.i(TAG, "连接中。。");
break;
case EVENTID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTION_FAILED:
Log.i(TAG, "连接失败。。");
break;
case EVENTID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTED:
Log.i(TAG, "连接成功。。");
// PublisherEntity publisherEntity=new PublisherEntity();
// if(TextUtils.isEmpty(publisherSuccess)) {
// publisherEntity.setPublishUrl(publisherSuccess);
// XdySdk.setAsyncApi("publishVideo", new Gson().toJson(publisherEntity));
// }
//TODO
break;
case EVENTID.EVENT_DANIULIVE_ERC_PLAYER_DISCONNECTED:
Log.i(TAG, "连接断开。。");
break;
case EVENTID.EVENT_DANIULIVE_ERC_PLAYER_STOP:
Log.i(TAG, "关闭。。");
break;
case EVENTID.EVENT_DANIULIVE_ERC_PLAYER_RESOLUTION_INFO:
Log.i(TAG, "分辨率信息: width: " + param1 + ", height: " + param2);
break;
case EVENTID.EVENT_DANIULIVE_ERC_PLAYER_NO_MEDIADATA_RECEIVED:
Log.i(TAG, "收不到媒体数据,可能是url错误。。");
}
}
}
/*mcu断开或者网络原因为false*/
private boolean isDefaultExit=true;
private android.support.v7.app.AlertDialog mErrorDialog;
public void showErrorDialog() {
// if(isDefaultExit){
// return;
// }
if (mErrorDialog != null)
return;
mErrorDialog = new android.support.v7.app.AlertDialog.Builder(this)
.setTitle("退出")
.setMessage("您设备的网络属于断开状态,请重新进入")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
exit();
}
})
.create();
mErrorDialog.setCanceledOnTouchOutside(false);
mErrorDialog.show();
}
/**
* 错误情况退出
*/
public void exit() {
UIUtils.closeDialog(mLoginDialog);
// xdySdk.onPublisherStop();
xdySdk.remove(this);
handler.removeCallbacksAndMessages(null);
this.finish();
}
class PersonDialog extends Dialog {
public PersonDialog(Context context) {
this(context, false);
}
public PersonDialog(Context context, boolean showPwd) {
this(context, R.style.bottom_dialog, showPwd);
}
public PersonDialog(Context context, int themeResId, boolean showPwd) {
super(context, themeResId);
init(context, showPwd);
}
public void init(Context context, boolean showPwd) {
View view = View.inflate(context, R.layout.dialog_item, null);
setContentView(view);
initView(view,showPwd);
setCanceledOnTouchOutside(false);
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = dp2px(context, 220);
window.setAttributes(params);
window.setGravity(Gravity.CENTER);
}
public int dp2px(Context context, float dp) {
return (int) Math.ceil(context.getResources().getDisplayMetrics().density * dp);
}
public void initView(View view, final boolean showPwd){
final EditText edt_uername= (EditText) view.findViewById(R.id.dialog_edt_username);
final EditText edt_userpwd= (EditText) view.findViewById(R.id.dialog_edt_userpwd);
if(showPwd){
edt_userpwd.setVisibility(View.VISIBLE);
}else{
edt_userpwd.setVisibility(View.GONE);
}
Button btn_enter= (Button) view.findViewById(R.id.dialog_btn_enter);
btn_enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
username=edt_uername.getText().toString();
userpwd=edt_userpwd.getText().toString();
if(TextUtils.isEmpty(username)){
ToastUtil.showToastshort("用户名不能为空",VideoPublisherActivity.this);
return;
}
if(showPwd&&TextUtils.isEmpty(userpwd)){
ToastUtil.showToastshort("密码不能为空",VideoPublisherActivity.this);
return;
}
dismiss();
Message message=Message.obtain();
message.what=-2;
message.obj=showPwd;
handler.sendMessage(message);
}
});
}
}
}