Ucloud直播云SDK的简单集成

        近段时间在做有关直播的项目,对ucloud的云直播sdk也有所了解,下面就来说说集成。

推流:

1.首先去ucloud官网下载最新的sdk,注意还有lib下面.so文件也要放到自己的项目中去。(目前只有armeabi-v7a的包,官方说很快会有其他包。注意:若项目中还用到其他.so,使用时lib下面的文件夹都要有相应的.so文件,否则会出现崩溃)。


2.布局文件:

<com.ucloud.live.widget.UAspectFrameLayout
    android:id="@+id/container"
    android:layout_gravity="center"
    android:background="@color/color_progress"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

com.ucloud.live.widget.UAspectFrameLayout>

3.java代码:

官方Demo中有不少类,这里我只讲重点部分

①初始化

protected EasyStreaming mEasyStreaming;

protected UStreamingProfile mStreamingProfile;

//设置推流地址

第一个参数为推流地址的 host, publish3.cdn.ucloud.com.cn, 第二个参数为 id,整个推流即:rtmp://publish3.cdn.ucloud.com.cn/id

UStreamingProfile.Stream stream = new UStreamingProfile.Stream(rtmpPushStreamDomain, "ucloud/" + mSettings.getPusblishStreamId());

mPreviewContainer.setShowMode(UAspectFrameLayout.Mode.FULL);

//设置 上下文对象、显示的控件Id、视频编码类型、摄像头(前)、视频编码比特率,帧率等等
mStreamingProfile = new UStreamingProfile.Builder()
        .setContext(this)
        .setPreviewContainerLayout(mPreviewContainer)
        .setEncodeType(mSettings.getEncoderType())
        .setCameraId(whichCamera)
        .setResolution(UStreamingProfile.Resolution.RATIO_AUTO)
        .setVideoBitrate(mSettings.getVideoBitRate())
        .setVideoFrameRate(mSettings.getVideoFrameRate())
        .setAudioBitrate(UStreamingProfile.AUDIO_BITRATE_NORMAL)
        .setVideoCaptureOrientation(mSettings.getVideoCaptureOrientation())//UStreamingProfile.ORIENTATION_LANDSCAPE or UStreamingProfile.ORIENTATION_PORTRAIT
        .setStream(stream).build();

handleShowStreamingInfo(mStreamingProfile.getEncodeType());
mEasyStreaming = UEasyStreaming.Factory.newInstance(mStreamingProfile);

mEasyStreaming.addListener(this);

②开始推流
if (mEasyStreaming != null) {
    mEasyStreaming.startRecording();
}

注意:OnPause(),OnResum(),OnDestory()中要重写:
@Override
protected void onPause() {
    super.onPause();
    Log.e(TAG, "lifecycle->demo->activity->onPause");
    //if UEasyStermaing no inited in activity onCreate method , you need call this method after inited -> function as camera stopPreivew()
    mEasyStreaming.onPause();
}

@Override
protected void onResume() {
    super.onResume();
    Log.e(TAG, "lifecycle->demo->activity->onResume");
    //if UEasyStermaing no inited in activity onCreate method, you need call this method after inited -> function as camera startPreivew()
    mEasyStreaming.onResume();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if(mSettings.isOpenLogRecoder()) {
        Log2FileUtil.getInstance().stopLog();
    }
    Log.e(TAG, "lifecycle->demo->activity->onDestroy");
    mEasyStreaming.onDestroy();
}

③停止推流
if (mEasyStreaming != null) {
    mEasyStreaming.stopRecording();
}

④其他设置
切换摄像头:
if (mEasyStreaming != null) {
    mEasyStreaming.switchCamera();
}

打开、关闭闪光灯
 if (mEasyStreaming != null) {
    mEasyStreaming.toggleFlashMode();
}

注意:本文从官方Demo中抽出重点,集成时请结合官方Demo和文档。

你可能感兴趣的:(Android)