Agora-SDK-在Android中的使用(在线视频通话),安卓rxjava获取网络时间
作者:互联网
|—x86
|—PLACEHOLDER
|—res
|—drawable-xxxhdpi
|—btn_end_call.png
|—btn_mute.png
|—btn_switch_camera.png
|—btn_video.png
|—btn_voice.png
|—ic_launcher.png
|—layout
|—activity_video_chat_view.xml
|—values
|—colors.xml
|—dimens.xml
|—strings.xml
|—styles.xml
|—build.gradle
|—gradle
|—wrapper
|—gradle-wrapper.jar
|—gradle-wrapper.properties
|—gradle.properties
|—gradlew
|—gradlew.bat
|—images
|—ActivityViewChat.png
|—LICENSE.md
|—README.md
|—README.zh.md
|—settings.gradle
2.查看最项目的settings.gradle
和build.gradle
(最外层)
如果你想导入AS中查看,可以看一下
com.android.tools.build:gradle
的版本修改一下
---->[settings.gradle]----------------看一下项目包含的模块------------
include ‘:app’
---->[build.gradle]----------------看一下项目的一些信息------------
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath ‘com.android.tools.build:gradle:3.1.4’
}
}
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
d
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
elete rootProject.buildDir
}
3.查看模块下的build.gradle
---->[app/build.gradle]----------------看一下项目的具体信息------------
apply plugin: ‘com.android.application’
android {
compileSdkVersion 26
defaultConfig {
applicationId “io.agora.tutorials1v1vcall”
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName “1.0”
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
}
}
sourceSets {//这里是jniLibs的目录
main {
jniLibs.srcDirs = [’…/…/…/libs’]
}
}
}
dependencies {//这里是依赖
implementation fileTree(dir: ‘…/…/…/libs’, include: [’*.jar’]) // DO NOT CHANGE, CI may needs it when packaging
implementation ‘com.android.support:appcompat-v7:26.1.0’
}
4.查看AndroidManifest.xml
,得到入口Activity
可见示例的入口是
VideoChatViewActivity
,并看一下权限
|— 权限 ------------
三、创建IChat项目
1、配置项目
项目的配置如图,将依赖包以及.so文件放在对应位置
为了方便些,将res文件夹的资源拷贝一下
2、配置APP ID
3.视频通话Activity的分析
一共也就200多行,还包括一大坨权限申请的代码,这里权限申请的代码单独拎出来,就当复习一下。
3.1:权限申请(非要点,可忽略)
---->[成员变量]----------------------------------
private static final int PERMISSION_REQ_ID = 22;
//WRITE_EXTERNAL_STORAGE 权限只是为了保存日志到SD卡
private static final String[] REQUESTED_PERMISSIONS = {
Manifest.permission.RECORD_AUDIO,//录音权限
Manifest.permission.CAMERA,//相机权限
Manifest.permission.WRITE_EXTERNAL_STORAGE//SD卡写权限
};
if (checkSelfPermission(REQUESTED_PERMISSIONS[0], PERMISSION_REQ_ID) &&
checkSelfPermission(REQUESTED_PERMISSIONS[1], PERMISSION_REQ_ID) &&
checkSelfPermission(REQUESTED_PERMISSIONS[2], PERMISSION_REQ_ID)) {
//执行到此处说明已有权限成功
initAgoraEngineAndJoinChannel();
}
/**
- 检查权限的方法
- @param permission 权限
- @param requestCode 请求码
- @return 是否拥有权限
*/
public boolean checkSelfPermission(String permission, int requestCode) {
Log.i(LOG_TAG, "checkSelfPermission " + permission + " " + requestCode);
if (ContextCompat.checkSelfPermission(this, permission)
!= PackageManager.PERMISSION_GRANTED) {
//发送权限请求
ActivityCompat.requestPermissions(this, REQUESTED_PERMISSIONS, requestCode);
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Log.i(LOG_TAG, "onRequestPermissionsResult " + grantResults[0] + " " + requestCode);
switch (requestCode) {
case PERMISSION_REQ_ID: {//请求码
if (grantResults[0] != PackageManager.PERMISSION_GRANTED ||
grantResults[1] != PackageManager.PERMISSION_GRANTED ||
grantResults[2] != PackageManager.PERMISSION_GRANTED) {
//三个权限有任意的未被允许,弹吐司,退出
showLongToast("Need permissions " +
Manifest.permission.RECORD_AUDIO + “/” +
Manifest.permission.CAMERA + “/” +
Manifest.permission.WRITE_EXTERNAL_STORAGE);
finish();
break;
}
//执行到此处说明用户已允许权限
initAgoraEngineAndJoinChannel();
break;
}
}
}
4.初始化Agora引擎和连接频道
/**
- 初始化Agora引擎和连接频道
*/
private void initAgoraEngineAndJoinChannel() {
initializeAgoraEngine();//初始化Agora引擎
setupVideoProfile();//设置视频信息
setupLocalVideo();//设置本地的视频窗
joinChannel();//连接频道
}
/**
- 初始化Agora引擎
*/
private void initializeAgoraEngine() {
try {
mRtcEngine = RtcEngine.create(//实例化Rtc引擎
getBaseContext(),//传入Context
getString(R.string.agora_app_id), //传入APP ID
mRtcEventHandler);//RTC事件处理器
} catch (Exception e) {//发生异常时捕获异常
Log.e(LOG_TAG, Log.getStackTraceString(e));
throw new RuntimeException(“NEED TO check rtc sdk init fatal error\n” + Log.getStackTraceString(e));
}
}
/**
- 设置视频信息
*/
private void setupVideoProfile() {
mRtcEngine.enableVideo();//启用视屏
mRtcEngine.setVideoEncoderConfiguration(//视频解码配置
new VideoEncoderConfiguration(//实例化对象
VideoEncoderConfiguration.VD_120x120,//尺寸
VideoEncoderConfiguration.FRAME_RATE.FRAME_RATE_FPS_15,//帧率
VideoEncoderConfiguration.STANDARD_BITRATE,//比特率
VideoEncoderConfiguration.ORIENTATION_MODE.ORIENTATION_MODE_FIXED_PORTRAIT));//旋转模式
}
|—关于VideoEncoderConfiguration对象---->[VideoEncoderConfiguration构造方法]------------------
public VideoEncoderConfiguration(
VideoEncoderConfiguration.VideoDimensions dimensions, //尺寸
VideoEncoderConfiguration.FRAME_RATE frameRate,//帧率
int bitrate, //比特率
VideoEncoderConfiguration.ORIENTATION_MODE orientationMode)//旋转模式
/**
- 设置本地视频窗
*/
private void setupLocalVideo() {
FrameLayout container = findViewById(R.id.local_video_view_container);//FrameLayout视图
SurfaceView surfaceView = RtcEngine.CreateRendererView(getBaseContext());//创建SurfaceView
surfaceView.setZOrderMediaOverlay(true);
container.addView(surfaceView);
mRtcEngine.setupLocalVideo(new VideoCanvas(surfaceView, VideoCanvas.RENDER_MODE_FIT, 0));
}
/**
- 连接到频道
*/
private void joinChannel() {
mRtcEngine.joinChannel(null, “demoChannel1”, “Extra Optional Data”, 0);
// 如果你不指定 uid(第四参), 我们会为你生成一个 uid
}
5.RTC事件处理器:IRtcEngineEventHandler
IRtcEngineEventHandler是一个抽象类,定义了非常多的抽象方法还有一些静态内部类
private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() {
@Override//已完成远端视频首帧解码回调。
public void onFirstRemoteVideoDecoded(final int uid, int width, int height, int elapsed) {
runOnUiThread(() -> setupRemoteVideo(uid));
}
@Override//远端用户(通信模式)/主播(直播模式)离开当前频道回调。
public void onUserOffline(int uid, int reason) {
runOnUiThread(() -> onRemoteUserLeft());
}
@Override//远端用户静音回调。
public void onUserMuteVideo(final int uid, final boolean muted) {
runOnUiThread(() -> onRemoteUserVideoMuted(uid, muted));
}
};
/**
- 根据uid设置远端视频
- @param uid 唯一标识符
*/
private void setupRemoteVideo(int uid) {
FrameLayout container = findViewById(R.id.remote_video_view_container);
if (container.getChildCount() >= 1) {
return;
}
SurfaceView surfaceView = RtcEngine.CreateRendererView(getBaseContext());
container.addView(surfaceView);
mRtcEngine.setupRemoteVideo(new VideoCanvas(surfaceView, VideoCanvas.RENDER_MODE_FIT, u
surfaceView.setTag(uid); // 用uid为surfaceView打标签
View tipMsg = findViewById(R.id.quick_tips_when_use_agora_sdk); // 隐藏文字UI
tipMsg.setVisibility(View.GONE);
}
/**
- 远端用户挂断
*/
private void onRemoteUserLeft() {
FrameLayout container = findViewById(R.id.remote_video_view_container);
container.removeAllViews();
View tipMsg = findViewById(R.id.quick_tips_when_use_agora_sdk); // 显示文字UI
tipMsg.setVisibility(View.VISIBLE);
}
/**
- 远端用户静音
- @param uid 标识符
隐藏文字UI
tipMsg.setVisibility(View.GONE);
}
/**
- 远端用户挂断
*/
private void onRemoteUserLeft() {
FrameLayout container = findViewById(R.id.remote_video_view_container);
container.removeAllViews();
View tipMsg = findViewById(R.id.quick_tips_when_use_agora_sdk); // 显示文字UI
tipMsg.setVisibility(View.VISIBLE);
}
/**
- 远端用户静音
- @param uid 标识符
标签:container,uid,int,安卓,在线视频,gradle,Agora,权限,void 来源: https://blog.csdn.net/m0_64319298/article/details/121789065