Skip to content

Commit 57e31e6

Browse files
committed
[Android]fix arcore problem, jira: MS-198734
1 parent e110273 commit 57e31e6

2 files changed

Lines changed: 38 additions & 33 deletions

File tree

Android/APIExample/app/src/main/java/io/agora/api/example/ExampleActivity.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import androidx.appcompat.app.AppCompatActivity;
1111
import androidx.fragment.app.Fragment;
1212

13+
import com.yanzhenjie.permission.AndPermission;
14+
import com.yanzhenjie.permission.runtime.Permission;
15+
1316
import io.agora.api.component.Constant;
1417
import io.agora.api.example.common.model.ExampleBean;
1518
import io.agora.api.example.examples.advanced.ARCore;
@@ -70,7 +73,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
7073
actionBar.setDisplayHomeAsUpEnabled(true);
7174
}
7275

73-
Fragment fragment;
76+
Fragment fragment = null;
7477
switch (exampleBean.getActionId()) {
7578
case R.id.action_mainFragment_to_joinChannelAudio:
7679
fragment = new JoinChannelAudio();
@@ -145,7 +148,15 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
145148
fragment = new LiveStreaming();
146149
break;
147150
case R.id.action_mainFragment_arcore:
148-
fragment = new ARCore();
151+
AndPermission.with(this).runtime().permission(
152+
Permission.Group.STORAGE,
153+
Permission.Group.CAMERA,
154+
Permission.Group.MICROPHONE
155+
).onGranted(data -> {
156+
getSupportFragmentManager().beginTransaction()
157+
.replace(R.id.fragment_Layout, new ARCore())
158+
.commit();
159+
}).onDenied(data -> finish()).start();
149160
break;
150161
case R.id.action_mainFragment_senddatastream:
151162
fragment = new SendDataStream();
@@ -166,9 +177,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
166177
fragment = new JoinChannelAudio();
167178
break;
168179
}
169-
getSupportFragmentManager().beginTransaction()
170-
.replace(R.id.fragment_Layout, fragment)
171-
.commit();
180+
if(fragment != null){
181+
getSupportFragmentManager().beginTransaction()
182+
.replace(R.id.fragment_Layout, fragment)
183+
.commit();
184+
}
172185
}
173186

174187
@Override

Android/APIExample/app/src/main/java/io/agora/api/example/examples/advanced/ARCore.java

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package io.agora.api.example.examples.advanced;
22

3+
import static io.agora.api.example.common.model.Examples.ADVANCED;
4+
import static io.agora.rtc.video.VideoEncoderConfiguration.STANDARD_BITRATE;
5+
36
import android.annotation.TargetApi;
47
import android.content.Context;
58
import android.graphics.Bitmap;
@@ -15,12 +18,10 @@
1518
import android.view.LayoutInflater;
1619
import android.view.MotionEvent;
1720
import android.view.PixelCopy;
18-
import android.view.SurfaceView;
1921
import android.view.View;
2022
import android.view.ViewGroup;
2123
import android.widget.Button;
2224
import android.widget.EditText;
23-
import android.widget.FrameLayout;
2425
import android.widget.Toast;
2526

2627
import androidx.annotation.NonNull;
@@ -35,7 +36,6 @@
3536
import com.google.ar.core.HitResult;
3637
import com.google.ar.core.Plane;
3738
import com.google.ar.core.Point;
38-
import com.google.ar.core.PointCloud;
3939
import com.google.ar.core.Session;
4040
import com.google.ar.core.Trackable;
4141
import com.google.ar.core.TrackingState;
@@ -44,8 +44,6 @@
4444
import com.google.ar.core.exceptions.UnavailableArcoreNotInstalledException;
4545
import com.google.ar.core.exceptions.UnavailableSdkTooOldException;
4646
import com.google.ar.core.exceptions.UnavailableUserDeclinedInstallationException;
47-
import com.yanzhenjie.permission.AndPermission;
48-
import com.yanzhenjie.permission.runtime.Permission;
4947

5048
import java.io.IOException;
5149
import java.nio.ByteBuffer;
@@ -74,13 +72,8 @@
7472
import io.agora.rtc.RtcEngine;
7573
import io.agora.rtc.mediaio.MediaIO;
7674
import io.agora.rtc.models.ChannelMediaOptions;
77-
import io.agora.rtc.video.VideoCanvas;
7875
import io.agora.rtc.video.VideoEncoderConfiguration;
7976

80-
import static io.agora.api.example.common.model.Examples.ADVANCED;
81-
import static io.agora.rtc.video.VideoCanvas.RENDER_MODE_HIDDEN;
82-
import static io.agora.rtc.video.VideoEncoderConfiguration.STANDARD_BITRATE;
83-
8477
/**This demo demonstrates how to make a one-to-one video call*/
8578
@Example(
8679
index = 24,
@@ -166,7 +159,6 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
166159
join = view.findViewById(R.id.btn_join);
167160
et_channel = view.findViewById(R.id.et_channel);
168161
et_channel.setText("arcoreDemo");
169-
joinChannel("arcoreDemo");
170162
view.findViewById(R.id.btn_join).setOnClickListener(this);
171163
mSurfaceView = view.findViewById(R.id.fl_local);
172164
mDisplayRotationHelper = new DisplayRotationHelper(getContext());
@@ -216,6 +208,10 @@ public void onDestroy()
216208
{
217209
super.onDestroy();
218210
/**leaveChannel and Destroy the RtcEngine instance*/
211+
if(mSession != null){
212+
mSession.close();
213+
mSession = null;
214+
}
219215
if(engine != null)
220216
{
221217
engine.leaveChannel();
@@ -228,7 +224,10 @@ public void onDestroy()
228224
//mRtcEngine.setRemoteVideoRenderer(render.getPeer().uid, null);
229225
}
230226
mRemoteRenders.clear();
231-
mSenderHandler.getLooper().quit();
227+
if(mSenderHandler != null){
228+
mSenderHandler.getLooper().quit();
229+
mSenderHandler = null;
230+
}
232231
}
233232

234233
@Override
@@ -242,21 +241,7 @@ public void onClick(View v)
242241
// call when join button hit
243242
String channelId = et_channel.getText().toString();
244243
// Check permission
245-
if (AndPermission.hasPermissions(this, Permission.Group.STORAGE, Permission.Group.MICROPHONE, Permission.Group.CAMERA))
246-
{
247-
joinChannel(channelId);
248-
return;
249-
}
250-
// Request permission
251-
AndPermission.with(this).runtime().permission(
252-
Permission.Group.STORAGE,
253-
Permission.Group.MICROPHONE,
254-
Permission.Group.CAMERA
255-
).onGranted(permissions ->
256-
{
257-
// Permissions Granted
258-
joinChannel(channelId);
259-
}).start();
244+
joinChannel(channelId);
260245
}
261246
else
262247
{
@@ -280,6 +265,10 @@ public void onClick(View v)
280265
* triggers the removeInjectStreamUrl method.*/
281266
engine.leaveChannel();
282267
join.setText(getString(R.string.join));
268+
if (mSenderHandler != null) {
269+
mSenderHandler.getLooper().quit();
270+
mSenderHandler = null;
271+
}
283272
}
284273
}
285274
}
@@ -584,14 +573,17 @@ public void onResume() {
584573

585574
@RequiresApi(api = Build.VERSION_CODES.N)
586575
private void sendARViewMessage() {
576+
if (mSenderHandler == null) {
577+
return;
578+
}
587579
final Bitmap outBitmap = Bitmap.createBitmap(mSurfaceView.getWidth(), mSurfaceView.getHeight(), Bitmap.Config.ARGB_8888);
588580
PixelCopy.request(mSurfaceView, outBitmap, new PixelCopy.OnPixelCopyFinishedListener() {
589581
@Override
590582
public void onPixelCopyFinished(int copyResult) {
591583
if (copyResult == PixelCopy.SUCCESS) {
592584
sendARView(outBitmap);
593585
} else {
594-
Toast.makeText(getContext(), "Pixel Copy Failed", Toast.LENGTH_SHORT);
586+
handler.post(() -> Toast.makeText(getContext(), "Pixel Copy Failed", Toast.LENGTH_SHORT).show());
595587
}
596588
}
597589
}, mSenderHandler);

0 commit comments

Comments
 (0)