上一篇博客只是简单的说了一下如何搭建和怎么样推流,但是直播还有好多功能没有实现
在这篇博客我将会一一实现。
1、切换摄像头
2、自动对焦设置
3、美颜美白设置
4、打开闪光灯
5、对码率的设置
1、切换摄像头:
在initView(view)中找到切换摄像头的Button
//初始化摄像头
mBtnChangeCam = (Button) view.findViewById(R.id.btnCameraChange);
//是否是前置摄像头
private boolean mFrontCam;
在对应的监听事件中做点击操作:
//点击后切换摄像头
mBtnChangeCam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mFrontCam = !mFrontCam;
//判断是否正在推流,如果正在推流的直接切换摄像头,如果不是的话修改摄像头配置
if (mLivePusher.isPushing()) {
mLivePusher.switchCamera();
} else {
mTxLivePushConfig.setFrontCamera(mFrontCam);
}
//根据相机的摄像头的改变切换图片
mBtnChangeCam.setBackgroundResource(mFrontCam ? R.drawable.camera_change : R.drawable.camera_change2);
}
});
2、自动对焦设置
//对摄像头的对焦处理
mBtnTouchFoucs = (Button) view.findViewById(R.id.btnTouchFoucs);
//设置默认对焦
private boolean mTouchFocus = true;
//设置自动对焦的处理操作
mBtnTouchFoucs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mFrontCam) {
return;
}
mTouchFocus = !mTouchFocus;
//更换对焦方式
mTxLivePushConfig.setTouchFocus(mTouchFocus);
mLivePusher.setConfig(mTxLivePushConfig);
mBtnTouchFoucs.setBackgroundResource(mTouchFocus ? R.drawable.manual : R.drawable.automatic);
//判断如果处于推送中,对摄像头进行语言效果处理:
//关闭之前的摄像头,打开当前的摄像头
if (mLivePusher.isPushing()) {
mLivePusher.stopCameraPreview(false);
mLivePusher.startCameraPreview(mCaptureView);
}
}
});
3、美颜美白设置
//美颜布局、
layoutFaceBeauty = (LinearLayout) view.findViewById(R.id.layoutFaceBeauty);
beauty_seekbar = (SeekBar) view.findViewById(R.id.beauty_seekbar);
whitening_seekbar = (SeekBar) view.findViewById(R.id.whitening_seekbar);
btnFaceBeauty = (Button) view.findViewById(R.id.btnFaceBeauty);
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//隐藏美颜布局
layoutFaceBeauty.setVisibility(View.GONE);
}
});
init(view);
}
//美白美颜效果
btnFaceBeauty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layoutFaceBeauty.setVisibility(View.VISIBLE);
}
});
beauty_seekbar.setOnSeekBarChangeListener(this);
whitening_seekbar.setOnSeekBarChangeListener(this);
//美颜
private int mBeautyLevel;
//美白
private int mWhitenLevel;
/**
* 美颜美白强度设置
* @param seekBar
* @param progress
* @param b
*/
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
switch (seekBar.getId()) {
case R.id.beauty_seekbar:
mBeautyLevel = progress;
break;
case R.id.whitening_seekbar:
mWhitenLevel = progress;
break;
}
if(mLivePusher!=null){
boolean isBeauty = mLivePusher.setBeautyFilter(mBeautyLevel, mWhitenLevel);
if(!isBeauty){
Toast.makeText(getActivity(), "当前手机不支持美颜", Toast.LENGTH_SHORT).show();
}
}
}
4、打开闪光灯
//开启闪光灯
mBtnFlash = (Button) view.findViewById(R.id.btnFlash);
//是否开启闪光灯
private boolean mFlashTrunOn;
//开启闪光灯
mBtnFlash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mFlashTrunOn = !mFlashTrunOn;
//闪光灯开启失败的了原因
//1、前置摄像头不支持 2、没有开启摄像头
if (!mLivePusher.turnOnFlashLight(mFlashTrunOn)) {
Toast.makeText(getActivity(), "1、前置摄像头不支持 2、没有开启摄像头", Toast.LENGTH_SHORT).show();
}
mBtnFlash.setBackgroundResource(mFlashTrunOn ? R.drawable.flash_on : R.drawable.flash_off);
}
});
5、码率设置
//修改码率
btnBitrate = (Button) view.findViewById(R.id.btnBitrate);
layoutBitrate = (LinearLayout) view.findViewById(R.id.layoutBitrate);
radioGroup = (RadioGroup) view.findViewById(R.id.resolutionRadioGroup);
//修改码率
btnBitrate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layoutBitrate.setVisibility(View.VISIBLE);
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
fitOrAdjustBitrate(checkId);
layoutBitrate.setVisibility(View.GONE);
}
});
/**
* 设置码率的方法
* @param checkId
*/
private void fitOrAdjustBitrate(int checkId) {
switch (checkId){
case R.id.radio_btn_fix_720p:
if (mLivePusher != null) {
mTxLivePushConfig.setVideoResolution(TXLiveConstants.VIDEO_RESOLUTION_TYPE_720_1280);
mTxLivePushConfig.setAutoAdjustBitrate(false);
mTxLivePushConfig.setVideoBitrate(1500);
mLivePusher.setConfig(mTxLivePushConfig);
}
btnBitrate.setBackgroundResource(R.drawable.fix_bitrate);
break;
case R.id.radio_btn_fix_540p:
if (mLivePusher != null) {
mTxLivePushConfig.setVideoResolution(TXLiveConstants.VIDEO_RESOLUTION_TYPE_540_960);
mTxLivePushConfig.setAutoAdjustBitrate(false);
mTxLivePushConfig.setVideoBitrate(1000);
mLivePusher.setConfig(mTxLivePushConfig);
}
btnBitrate.setBackgroundResource(R.drawable.fix_bitrate);
break;
case R.id.radio_btn_fix_360p:
if (mLivePusher != null) {
mTxLivePushConfig.setVideoResolution(TXLiveConstants.VIDEO_RESOLUTION_TYPE_360_640);
mTxLivePushConfig.setAutoAdjustBitrate(false);
mTxLivePushConfig.setVideoBitrate(700);
mLivePusher.setConfig(mTxLivePushConfig);
}
btnBitrate.setBackgroundResource(R.drawable.fix_bitrate);
break;
case R.id.radio_btn_auto:
if (mLivePusher != null) {
//设置界面画质
mTxLivePushConfig.setVideoResolution(TXLiveConstants.VIDEO_RESOLUTION_TYPE_360_640);
//设置开启自适应码率
mTxLivePushConfig.setAutoAdjustBitrate(true);
//设置最大码率
mTxLivePushConfig.setMaxVideoBitrate(1000);
//设置最小码率
mTxLivePushConfig.setMinVideoBitrate(500);
//设置常规码率
mTxLivePushConfig.setVideoBitrate(700);
mLivePusher.setConfig(mTxLivePushConfig);
}
btnBitrate.setBackgroundResource(R.drawable.auto_bitrate);
break;
}
}
上述的这些方法就是推流时候的一些效果功能,在最后当然也要对生命周期进行一些处理
6、生命周期
@Override
public void onResume() {
super.onResume();
if(mCaptureView!=null){
mCaptureView.onResume();
}
if(mVideoPublish&&mLivePusher.isPushing()){
mLivePusher.startCameraPreview(mCaptureView);
}
mLivePusher.resumePusher(); // 通知 SDK 重回前台推流
}
@Override
public void onStop() {
super.onStop();
if (mCaptureView!=null){
mCaptureView.onPause();
}
mLivePusher.pausePusher(); // 通知 SDK 进入“后台推流模式”了
mLivePusher.stopCameraPreview(false);
}
@Override
public void onDestroy() {
super.onDestroy();
stopPulishRtmp();
if (mCaptureView != null) {
mCaptureView.onDestroy();
}
}