Threeivity.java 11.6 KB
package xdycom.abao.myapplication;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;

import com.eventhandle.SmartEventCallback;
import com.google.gson.Gson;
import com.mang.xdy.core.XdySdk;
import com.mang.xdy.utils.XdyLogUtil;
import com.mang.xdy.utils.XdyStringUtils;

import xdycom.abao.myapplication.entity.LiveBean;
import xdycom.abao.myapplication.utisl.StringUtils;

import static xdycom.abao.myapplication.MainActivity.stringToJson;

public class Threeivity extends AppCompatActivity implements View.OnClickListener{
   private Button bt_three,bt_video
    ,bt_paudio,bt_stopaudiopub,bt_back,bt_stopReco;
    private WebView webView;
    private final static String URL="http://h5.test.3mang.com/web/";
    private SurfaceView surface;
    private Button bt_stop;
    private XdySdk xdySdk;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_threeivity);
        xdySdk=XdySdk.getXdyInstance();
        bt_three= (Button) findViewById(R.id.bt_three);
        bt_video= (Button) findViewById(R.id.bt_video);
        surface= (SurfaceView) findViewById(R.id.surface);
        bt_stop= (Button) findViewById(R.id.bt_stop);
        bt_paudio= (Button) findViewById(R.id.bt_paudio);
        bt_paudio.setOnClickListener(this);
        bt_stopaudiopub= (Button) findViewById(R.id.bt_stopaudiopub);
        bt_back= (Button) findViewById(R.id.bt_back);
        bt_back.setOnClickListener(this);
        bt_stopaudiopub.setOnClickListener(this);
        bt_stopReco= (Button) findViewById(R.id.bt_stopReco);
        bt_stopReco.setOnClickListener(this);
        bt_three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id="init";
                String jsonParmps="{\n" +
                        "    \"classId\": 1184939098,\n" +
                        "    \"portal\": \"112.126.80.182:90\",\n" +
                        "    \"userRole\": \"normal\",\n" +
                        "    \"userName\": \"\",\n" +
                        "    \"userId\": 0\n" +
                        "}";
                String jsonParmp= StringUtils.stringToJson(jsonParmps,true);
                getSdySdk(id,jsonParmp);
            }
        });


        bt_video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               String id = "joinClass";
                String temp = "{\n" +
                        "      \"userName\":\"base\",\n" +
                        "       \"password\":\"123321\",\n" +
                        "       \"hasCamera\":false,\n" +
                        "       \"hasMicrophone\":false}";

//                        "}";
                String jsonParmp = stringToJson(temp, true);
                getSdySdk(id,jsonParmp);
//                SmartPlayerNoLayout smartPlayerNoLayout=new SmartPlayerNoLayout(surface, Threeivity.this);
//                startActivity(new Intent(Threeivity.this,SmartPlayer.class));
                String url=  "rtmp://123.56.205.116:6000/live/h5dev_1999957388_980_983041_1491813919";;
////                XdyPlayer xdyPlayer=new XdyPlayer(url,surface,Threeivity.this);
////                xdyPlayer.playVideo(url,surface);
//                XdySdk.setAsyncApi("playAudio",url,null,Threeivity.this);
//                XdySdk.setAsyncApi("playVideo",url,surface,Threeivity.this,new EventHande());
                xdySdk.api("publishVideo",url,surface,Threeivity.this);
//                xdyPlayer.playVideo("",surface);

//                XdyPublisher xdyPublisherCore=new XdyPublisher(Threeivity.this);
//                xdyPublisherCore.publish("",surface);
            }
        });

        bt_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                xdySdk.onPlayStop();
//                XdySdk.onPublisherStop();;

                xdySdk.api("leaveClass","");
            }
        });
        init();
    }
    public void init(){
        webView=new WebView(this);
        WebSettings settings=webView.getSettings();
        settings.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new MyJsH(),"xdyAndroid");
        webView.loadUrl(URL);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_paudio:
                //开始推送音频
                String url=  "rtmp://player.daniulive.com:1935/hls/stream";;
                xdySdk.api("publishAudio",url,surface,Threeivity.this);
                break;
            case R.id.bt_stopaudiopub:
                //停止
//                XdySdk.setAsyncApi("unPublishAudio","");
                                String id="initRecordPlayback";
//                String id="startRecordPlayback";
                String param="{\n" +
                        "    \"classId\": 479270427 ,\n" +
                        "    \"portal\": \"112.126.80.182:90\",\n" +
                        "    \"userRole\": \"normal\",\n" +
                        "    \"userName\": \"\",\n" +
                        "    \"userId\": 0\n" +
                        "}";
                String ss=StringUtils.stringToJson(param,true);
                xdySdk.api(id,ss);
                break;
            case R.id.bt_back:
//                String id="initRecordPlayback";
                String ids="startRecordPlayback";
//                String param="{\n" +
//                        "    \"classId\": 1184939098,\n" +
//                        "    \"portal\": \"112.126.80.182:90\",\n" +
//                        "    \"userRole\": \"normal\",\n" +
//                        "    \"userName\": \"\",\n" +
//                        "    \"userId\": 0\n" +
//                        "}";
//                String ss=StringUtils.stringToJson(param,true);
                xdySdk.api(ids,"");
                break;
            case R.id.bt_stopReco:
//                String isd="stopRecordPlayback";
//                XdySdk.setAsyncApi(isd,"");
//                String ids_new="seekRecordPlayback";
//                String tem="{\n" +
//                        "    \"time\": 50\n" +
//                        "}";
//               String s= StringUtils.stringToJson(tem,true);
//                XdySdk.setAsyncApi(ids_new,s);

        String ids_new="getVideoPublishPath";
                String tem="{ \"type\": \"live\"}";
               String s= StringUtils.stringToJson(tem,true);
                xdySdk.api(ids_new,s);


                break;
        }
    }

    class MyJsH{
        @JavascriptInterface
        public String _js2native(String id,String parmp){
          /*  _native2js("接口名称","参数为JOSN字符串")*/
            Log.e("s3sjs2native","id:"+id+"  parmp:"+parmp);
            return parmp;
        }

    }

    public  void getSdySdk(String id,String parameter){
//        XdyCore core=XdyCore.getInstance(MyApplicaiton.getInstance());
//       core.native2js(id,parameter);
//        core.setXdyAsyncMessageListener(new XdyCore.XdyAsyncMessageListener() {
//            @Override
//            public void getSdyAsyncMessageListener(String s, String s1) {
//                LogUtil.e("java_native",s+":"+s1);
//            }
//        });
        xdySdk.api(id,parameter);
//        XdySdk.getXdyInstance().setOnXdyAsyncMessageLitener(new XdySdk.OnXdyAsyncMessageLitener() {
//            @Override
//            public void onXdyAsyncMessageReceiver(String type, String response) {
//                XdyLogUtil.e("学点云",type+":"+response);
//            }
//        });

//        XdySdk.setOnXdyAsyncMessageLitener(new XdySdk.OnXdyAsyncMessageLitener() {
//            @Override
//            public void onXdyAsyncMessageReceiver(String type, String response) {
//                XdyLogUtil.e("学点云static", type + ":" + response);
////                if ("video_play".equals(type)) {
////                    MessageBean messageBean = new Gson().fromJson(response, MessageBean.class);
////                    if (messageBean != null) {
//////                        Intent intent = new Intent(Threeivity.this, SmartPlayer.class);
//////                        intent.putExtra("url", messageBean.getRtmpUrl() + "");
//////                        startActivity(intent);
////                        Toast.makeText(Threeivity.this, messageBean.getRtmpUrl(), Toast.LENGTH_SHORT).show();
//////                        SmartPlayerNoLayout smartPlayerNoLayout=new SmartPlayerNoLayout(surface,Threeivity.this);
////                    }
////                }
//                if("live".equals(type)){
////                    String urls=  "rtmp://player.daniulive.com:1935/hls/stream";;
//                    String url = XdyStringUtils.stringToJson(response);
//                    XdyLogUtil.e("调用任务了播视频了url:",""+url);
//                    String us=response.substring(1,response.length()-1);
//                    XdyLogUtil.e("调用任务了播视频了ursfdfl:",""+us);
//                    try {
//
//                        LiveBean liveBean = new Gson().fromJson(url, LiveBean.class);
//
//                        if (liveBean != null) {
//                            XdySdk.setAsyncApi("publishVideo", liveBean.getPublishUrl(), surface, Threeivity.this);
//                            XdyLogUtil.e("调用任务了播视频了","哈哈哈哈");
//                        }
//                    }catch (Exception e){
//                        XdyLogUtil.e("调用任务了播视频了e",""+e.getMessage());
//                        XdyLogUtil.e("调用任务了播视频了url:",""+url);
//                    }
//                }
//            }
//        });
    }
   public void playMedia(){
       Intent intent = new Intent(Intent.ACTION_VIEW);
       String bpath = "http://123.56.73.119:6001/live/h5dev_1184939098_980_983041_1491533378/total.m3u8";
       intent.setDataAndType(Uri.parse(bpath), "video/*");
       startActivity(intent);
   }


}

class EventHande implements SmartEventCallback {
    private static String TAG="THREURL";

    @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, "连接成功。。");
                //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错误。。");
        }

    }
}