腾讯云SDK介入以及使用

腾讯云SDK介入以及使用


腾讯云直播官网
https://cloud.tencent.com/product/mlvb
腾讯云SDK介入以及使用_第1张图片
腾讯云SDK介入以及使用_第2张图片
清单




    
    
    
    
    
    
    
    
    
    

    
    

    
        

            
                

                
            

        
        

        
    


bulid.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.1"


    defaultConfig {
        applicationId "com.example.app3"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        ndk {
            abiFilters "armeabi", "armeabi-v7a", "arm64-v8a"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.tencent.liteavsdk:LiteAVSDK_Smart:latest.release'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

全局类

package com.example.app3;

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/2cbbf736ba4744239e7e7b18115ceab7/TXLiveSDK.licence"; // 获取到的 licence url
        String licenceKey = "eb8fb2132870df152531f9461c62b228"; // 获取到的 licence key
        TXLiveBase.getInstance().setLicence(this, licenceURL, licenceKey);
    }
}

推流

package com.example.app3;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

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";
    TXLivePusher mLivePusher;
    TXCloudVideoView mPusherView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        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();
        mLivePusher = new TXLivePusher(this);

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

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

    public void open(View view) {
        String rtmpURL = "rtmp://59312.livepush.myqcloud.com/live/MyStream?txSecret=4e5e8424ad6bfd4e414e841be2912279&txTime=5D5C18FF"; //此处填写您的 rtmp 推流地址
        int ret = mLivePusher.startPusher(rtmpURL.trim());
        if (ret == -5) {
            Log.i(TAG, "startRTMPPush: license 校验失败");
        }else {
            Log.i(TAG, "open: 校验成功");
        }
    }

    public void close(View view) {
        mLivePusher.stopPusher();
        mLivePusher.stopCameraPreview(true); //如果已经启动了摄像头预览,请在结束推流时将其关闭。
    }
}

拉流

package com.example.app3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

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

public class PullActivity extends AppCompatActivity {

    TXCloudVideoView mView;
    TXLivePlayer mLivePlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull);

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

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

        String flvUrl = "https://59312.liveplay.myqcloud.com/live/123.flv";
        mLivePlayer.startPlay(flvUrl, TXLivePlayer.PLAY_TYPE_LIVE_FLV); //推荐 FLV

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

    }

    public void start(View view) {
        //关键 player 对象与界面 view
        mLivePlayer.setPlayerView(mView);
    }

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

你可能感兴趣的:(次世代数据储存思维与技术)