-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mute.java
251 lines (194 loc) · 8.65 KB
/
Mute.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package com.example.myapplication;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import io.agora.rtc.Constants;
import io.agora.rtc.IRtcEngineEventHandler;
import io.agora.rtc.RtcEngine;
import io.agora.rtc.video.VideoEncoderConfiguration;
import io.agora.rtc.video.VideoCanvas;
public class Mute extends AppCompatActivity{
private RtcEngine mRtcEngine;
SeekBar seekBar;
// Permissions
private static final int PERMISSION_REQ_ID = 22;
private static final String[] REQUESTED_PERMISSIONS = {Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA};
private static final String LOG_TAG = MainActivity.class.getSimpleName();
// Handle SDK Events
private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() {
@Override
public void onUserJoined(final int uid, int elapsed) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// set first remote user to the main bg video container
setupRemoteVideoStream(uid);
}
});
}
// remote user has left channel
@Override
public void onUserOffline(int uid, int reason) { // Tutorial Step 7
runOnUiThread(new Runnable() {
@Override
public void run() {
onRemoteUserLeft();
}
});
}
// remote user has toggled their video
@Override
public void onRemoteVideoStateChanged(final int uid, final int state, int reason, int elapsed) {
runOnUiThread(new Runnable() {
@Override
public void run() {
onRemoteUserVideoToggle(uid, state);
}
});
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mute);
if (checkSelfPermission(REQUESTED_PERMISSIONS[0], PERMISSION_REQ_ID) &&
checkSelfPermission(REQUESTED_PERMISSIONS[1], PERMISSION_REQ_ID)) {
initAgoraEngine();
}
seekBar=(SeekBar)findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
mRtcEngine.adjustAudioMixingVolume(progress);
mRtcEngine.adjustPlaybackSignalVolume(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(),"seekbar touch started!", Toast.LENGTH_SHORT).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(),"seekbar touch stopped!", Toast.LENGTH_SHORT).show();
}
});
}
private void initAgoraEngine() {
try {
mRtcEngine = RtcEngine.create(getBaseContext(), getString(R.string.agora_app_id), mRtcEventHandler);
} 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));
}
setupSession();
}
private void setupSession() {
mRtcEngine.setChannelProfile(Constants.CHANNEL_PROFILE_COMMUNICATION);
mRtcEngine.enableVideo();
mRtcEngine.setVideoEncoderConfiguration(new VideoEncoderConfiguration(VideoEncoderConfiguration.VD_640x480, VideoEncoderConfiguration.FRAME_RATE.FRAME_RATE_FPS_30,
VideoEncoderConfiguration.STANDARD_BITRATE,
VideoEncoderConfiguration.ORIENTATION_MODE.ORIENTATION_MODE_FIXED_PORTRAIT));
}
private void setupLocalVideoFeed() {
// setup the container for the local user
FrameLayout videoContainer = findViewById(R.id.floating_video_container);
SurfaceView videoSurface = RtcEngine.CreateRendererView(getBaseContext());
videoSurface.setZOrderMediaOverlay(true);
videoContainer.addView(videoSurface);
mRtcEngine.setupLocalVideo(new VideoCanvas(videoSurface, VideoCanvas.RENDER_MODE_FIT, 0));
}
private void setupRemoteVideoStream(int uid) {
// setup ui element for the remote stream
FrameLayout videoContainer = findViewById(R.id.bg_video_container);
// ignore any new streams that join the session
if (videoContainer.getChildCount() >= 1) {
return;
}
SurfaceView videoSurface = RtcEngine.CreateRendererView(getBaseContext());
videoContainer.addView(videoSurface);
mRtcEngine.setupRemoteVideo(new VideoCanvas(videoSurface, VideoCanvas.RENDER_MODE_FIT, uid));
mRtcEngine.setRemoteSubscribeFallbackOption(Constants.STREAM_FALLBACK_OPTION_AUDIO_ONLY);
}
// join the channel when user clicks UI button
public void onjoinChannelClicked(View view) {
mRtcEngine.joinChannel(null, "test-channel", "Extra Optional Data", 0); // if you do not specify the uid, Agora will assign one.
setupLocalVideoFeed();
findViewById(R.id.joinBtn).setVisibility(View.GONE); // set the join button hidden
}
private void leaveChannel() {
mRtcEngine.leaveChannel();
}
private void removeVideo(int containerID) {
FrameLayout videoContainer = findViewById(containerID);
videoContainer.removeAllViews();
}
private void onRemoteUserVideoToggle(int uid, int state) {
FrameLayout videoContainer = findViewById(R.id.bg_video_container);
SurfaceView videoSurface = (SurfaceView) videoContainer.getChildAt(0);
videoSurface.setVisibility(state == 0 ? View.GONE : View.VISIBLE);
// add an icon to let the other user know remote video has been disabled
if(state == 0){
ImageView noCamera = new ImageView(this);
noCamera.setImageResource(R.drawable.video_disabled);
videoContainer.addView(noCamera);
} else {
ImageView noCamera = (ImageView) videoContainer.getChildAt(1);
if(noCamera != null) {
videoContainer.removeView(noCamera);
}
}
}
private void onRemoteUserLeft() {
removeVideo(R.id.bg_video_container);
}
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) {
super.onRequestPermissionsResult(requestCode, permissions, 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) {
Log.i(LOG_TAG, "Need permissions " + Manifest.permission.RECORD_AUDIO + "/" + Manifest.permission.CAMERA);
break;
}
initAgoraEngine();
break;
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
leaveChannel();
RtcEngine.destroy();
mRtcEngine = null;
}
public void muteAudio(View view) {
mRtcEngine.adjustAudioMixingVolume(0);
mRtcEngine.adjustPlaybackSignalVolume(0);
}
}