腾讯云直播功能

分享到:

腾讯云的使用

腾讯云也是第三方提供的接口让我们调用和导入依赖使用的。可以完成直播的功能。

腾讯云推流

就是开启直播的一方,比如可以通过拉流查看。代码

package com.example.zhibo_01;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.tencent.rtmp.TXLivePushConfig;
import com.tencent.rtmp.TXLivePusher;
import com.tencent.rtmp.ui.TXCloudVideoView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Button startButton;
    private Button stopButton;
    private TXCloudVideoView pusherTxCloudView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startButton = (Button) findViewById(R.id.start_button);
        stopButton = (Button) findViewById(R.id.stop_button);
        pusherTxCloudView = (TXCloudVideoView) findViewById(R.id.pusher_tx_cloud_view);

        String[] strings = new String[]{
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.MODIFY_AUDIO_SETTINGS,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.CAMERA,
                Manifest.permission.READ_PHONE_STATE
        };
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            requestPermissions(strings,100);
        }

        TXLivePushConfig mLivePushConfig  = new TXLivePushConfig();
        final TXLivePusher mLivePusher = new TXLivePusher(this);

        // 一般情况下不需要修改 config 的默认配置
        mLivePusher.setConfig(mLivePushConfig);

        //启动本地摄像头预览
        TXCloudVideoView mPusherView = (TXCloudVideoView) findViewById(R.id.pusher_tx_cloud_view);
        mLivePusher.startCameraPreview(mPusherView);

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String rtmpURL = "rtmp://59307.livepush.myqcloud.com/live/hh?txSecret=974fe943d78a6959f37ddd4c3431048f&txTime=5D5C18FF"; //此处填写您的 rtmp 推流地址
                int ret = mLivePusher.startPusher(rtmpURL.trim());
                if (ret == -5) {
                    Log.i(TAG, "startRTMPPush: license 校验失败");
                }
            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLivePusher.stopPusher();
                mLivePusher.stopCameraPreview(true); //如果已经启动了摄像头预览,请在结束推流时将其关闭。
            }
        });

    }
}

腾讯云拉流

就是连接直播的功能。代码

package com.example.zhibo_01;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.TXLivePlayer;
import com.tencent.rtmp.ui.TXCloudVideoView;

public class Main2Activity extends AppCompatActivity {

    private Button laliuButton;
    private TXLivePlayer mLivePlayer;
    private  TXCloudVideoView mView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        getSupportActionBar().hide();
        laliuButton = (Button) findViewById(R.id.laliu_button);

        //mPlayerView 即 step1 中添加的界面 view
        mView = (TXCloudVideoView) findViewById(R.id.video_view);

//创建 player 对象
        mLivePlayer = new TXLivePlayer(this);

        // 设置填充模式
        mLivePlayer.setRenderMode(TXLiveConstants.RENDER_MODE_ADJUST_RESOLUTION);
// 设置画面渲染方向
        mLivePlayer.setRenderRotation(TXLiveConstants.RENDER_ROTATION_LANDSCAPE);

        laliuButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关键 player 对象与界面 view
                mLivePlayer.setPlayerView(mView);
                String flvUrl = "https://59307.liveplay.myqcloud.com/live/hh.flv";
                mLivePlayer.startPlay(flvUrl, TXLivePlayer.PLAY_TYPE_LIVE_FLV); //推荐 FLV
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mLivePlayer.stopPlay(true); // true 代表清除最后一帧画面
        mView.onDestroy();
    }
}

APP

package com.example.zhibo_01;

import android.app.Application;

import com.tencent.rtmp.TXLiveBase;

public class APP extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        String licenceURL = "http://license.vod2.myqcloud.com/license/v1/1201e13c6ab2c534856ecff8fd78523f/TXLiveSDK.licence"; // 获取到的 licence url
        String licenceKey = "74edb2ab922b230e9b9bd20eeb53420d"; // 获取到的 licence key
        TXLiveBase.getInstance().setLicence(this, licenceURL, licenceKey);
    }
}


相关文章