Skip to content

Commit 96a89de

Browse files
author
zhaoyongqiang
committed
Merge branch 'dev/4.0.0-GA' of github.com:AgoraIO/API-Examples into dev/4.0.0-GA
2 parents 397e91f + aa002d9 commit 96a89de

12 files changed

Lines changed: 95 additions & 56 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,15 @@ protected final void runOnUIThread(Runnable runnable){
5959
}
6060

6161
protected final void runOnUIThread(Runnable runnable, long delay){
62-
if(handler != null && runnable != null){
62+
if(handler != null && runnable != null && getContext() != null){
6363
if (delay <= 0 && handler.getLooper().getThread() == Thread.currentThread()) {
6464
runnable.run();
6565
}else{
66-
handler.postDelayed(runnable, delay);
66+
handler.postDelayed(() -> {
67+
if(getContext() != null){
68+
runnable.run();
69+
}
70+
}, delay);
6771
}
6872
}
6973
}

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
286286
user.height = canvas_height;
287287
user.uid = localUid;
288288
liveTranscoding.addUser(user);
289-
engine.setLiveTranscoding(liveTranscoding);
289+
engine.updateRtmpTranscoding(liveTranscoding);
290290
}
291291

292292

@@ -360,32 +360,32 @@ public void run() {
360360
public void onRtmpStreamingStateChanged(String url, int state, int errCode) {
361361
super.onRtmpStreamingStateChanged(url, state, errCode);
362362
showLongToast(String.format("onRtmpStreamingStateChanged state %s errCode %s", state, errCode));
363+
if(state == Constants.RTMP_STREAM_PUBLISH_STATE_IDLE){
364+
if (cdnStreaming) {
365+
runOnUIThread(() -> {
366+
LeaveChannelOptions leaveChannelOptions = new LeaveChannelOptions();
367+
leaveChannelOptions.stopMicrophoneRecording = false;
368+
engine.leaveChannel(leaveChannelOptions);
369+
fl_remote.removeAllViews();
370+
fl_remote_2.removeAllViews();
371+
fl_remote_3.removeAllViews();
372+
remoteViews.clear();
373+
engine.startPreview();
374+
engine.setDirectCdnStreamingVideoConfiguration(videoEncoderConfiguration);
375+
int ret = startCdnStreaming();
376+
if (ret != 0) {
377+
showLongToast(String.format("startCdnStreaming failed! error code: %d", ret));
378+
stopStreaming();
379+
}
380+
});
381+
}
382+
}
363383
}
364384

365385
@Override
366386
public void onTranscodingUpdated() {
367387
showLongToast("RTMP transcoding updated successfully!");
368388
}
369-
370-
@Override
371-
public void onStreamUnpublished(String url) {
372-
if(cdnStreaming){
373-
LeaveChannelOptions leaveChannelOptions = new LeaveChannelOptions();
374-
leaveChannelOptions.stopMicrophoneRecording = false;
375-
engine.leaveChannel(leaveChannelOptions);
376-
fl_remote.removeAllViews();
377-
fl_remote_2.removeAllViews();
378-
fl_remote_3.removeAllViews();
379-
remoteViews.clear();
380-
engine.startPreview();
381-
engine.setDirectCdnStreamingVideoConfiguration(videoEncoderConfiguration);
382-
int ret = startCdnStreaming();
383-
if(ret != 0){
384-
showLongToast(String.format("startCdnStreaming failed! error code: %d", ret));
385-
stopStreaming();
386-
}
387-
}
388-
}
389389
};
390390

391391
private void updateTranscodeLayout() {
@@ -431,14 +431,14 @@ private void updateTranscodeLayout() {
431431
Log.i(TAG, "ignored user as only 2x2 video layout supported in this demo. uid:" + uid);
432432
}
433433
}
434-
engine.setLiveTranscoding(liveTranscoding);
434+
engine.updateRtmpTranscoding(liveTranscoding);
435435
}
436436

437437
private final IDirectCdnStreamingEventHandler iDirectCdnStreamingEventHandler = new IDirectCdnStreamingEventHandler() {
438438

439439
@Override
440440
public void onDirectCdnStreamingStateChanged(DirectCdnStreamingState directCdnStreamingState, DirectCdnStreamingError directCdnStreamingError, String s) {
441-
handler.post(new Runnable() {
441+
runOnUIThread(new Runnable() {
442442
@Override
443443
public void run() {
444444
switch (directCdnStreamingState) {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ public void onLeaveChannel(RtcStats stats) {
287287
Log.i(TAG, String.format("local user %d leaveChannel!", myUid));
288288
showLongToast(String.format("local user %d leaveChannel!", myUid));
289289
lastI420Frame = null;
290-
mSurfaceView.requestRender();
290+
if(mSurfaceView != null){
291+
mSurfaceView.requestRender();
292+
}
291293
}
292294

293295
/**Occurs when the local user joins a specified channel.
@@ -439,7 +441,9 @@ public void onUserJoined(int uid, int elapsed)
439441
public void onUserOffline(int uid, int reason) {
440442
Log.i(TAG, String.format("user %d offline! reason:%d", uid, reason));
441443
showLongToast(String.format("user %d offline! reason:%d", uid, reason));
442-
mSurfaceView.requestRender();
444+
if(mSurfaceView != null){
445+
mSurfaceView.requestRender();
446+
}
443447
remoteUid = 0;
444448
}
445449
};

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ public void onClick(View v) {
174174
* the onLeaveChannel callback.
175175
* 2:If you call the leaveChannel method during CDN live streaming, the SDK
176176
* triggers the removeInjectStreamUrl method.*/
177+
engine.stopPreview();
177178
engine.leaveChannel();
178179
join.setText(getString(R.string.join));
180+
remoteUid = 0;
179181
}
180182
} else if (v.getId() == R.id.btn_publish) {
181183
isHost = !isHost;
@@ -235,7 +237,10 @@ public void onClick(View v) {
235237
}
236238
} else if (v.getId() == R.id.btn_take_shot) {
237239
if (remoteUid != 0) {
238-
engine.takeSnapshot(remoteUid, "/sdcard/APIExample_snapshot_" + et_channel.getText().toString() + "_" + remoteUid + ".png");
240+
int ret = engine.takeSnapshot(remoteUid, "/sdcard/APIExample_snapshot_" + et_channel.getText().toString() + "_" + remoteUid + ".png");
241+
if(ret != Constants.ERR_OK){
242+
showLongToast("takeSnapshot error code=" + ret + ",msg=" + RtcEngine.getErrorDescription(ret));
243+
}
239244
} else {
240245
showLongToast(getString(R.string.remote_screenshot_tip));
241246
}
@@ -453,16 +458,18 @@ public void onUserJoined(int uid, int elapsed) {
453458
public void onUserOffline(int uid, int reason) {
454459
Log.i(TAG, String.format("user %d offline! reason:%d", uid, reason));
455460
showLongToast(String.format("user %d offline! reason:%d", uid, reason));
456-
if(uid == remoteUid)
457-
handler.post(new Runnable() {
458-
@Override
459-
public void run() {
460-
/**Clear render view
461-
Note: The video will stay at its last frame, to completely remove it you will need to
462-
remove the SurfaceView from its parent*/
463-
engine.setupRemoteVideo(new VideoCanvas(null, RENDER_MODE_HIDDEN, uid));
464-
}
465-
});
461+
if(uid == remoteUid) {
462+
remoteUid = 0;
463+
runOnUIThread(new Runnable() {
464+
@Override
465+
public void run() {
466+
/**Clear render view
467+
Note: The video will stay at its last frame, to completely remove it you will need to
468+
remove the SurfaceView from its parent*/
469+
engine.setupRemoteVideo(new VideoCanvas(null, RENDER_MODE_HIDDEN, uid));
470+
}
471+
});
472+
}
466473
}
467474

468475
/**

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
128128
publish = view.findViewById(R.id.publish);
129129

130130
progressBar = view.findViewById(R.id.ctrl_progress_bar);
131+
progressBar.setMax(100);
131132
progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
132133
@Override
133134
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
@@ -204,8 +205,6 @@ public void onClick(View v) {
204205
engine.stopPreview();
205206
join.setText(getString(R.string.join));
206207
mediaPlayer.stop();
207-
mediaPlayer.destroy();
208-
mediaPlayer.unRegisterPlayerObserver(this);
209208
open.setEnabled(false);
210209
setMediaPlayerViewEnable(false);
211210
}
@@ -514,7 +513,7 @@ public void run() {
514513

515514
@Override
516515
public void onPlayerStateChanged(io.agora.mediaplayer.Constants.MediaPlayerState mediaPlayerState, io.agora.mediaplayer.Constants.MediaPlayerError mediaPlayerError) {
517-
Log.e(TAG, "onPlayerStateChanged mediaPlayerState " + mediaPlayerState);
516+
Log.e(TAG, "onPlayerStateChanged mediaPlayerState " + mediaPlayerState + ", error=" + mediaPlayerError);
518517
if (mediaPlayerState.equals(PLAYER_STATE_OPEN_COMPLETED)) {
519518
setMediaPlayerViewEnable(true);
520519
} else if (mediaPlayerState.equals(PLAYER_STATE_IDLE) || mediaPlayerState.equals(PLAYER_STATE_STOPPED) || mediaPlayerState.equals(PLAYER_STATE_PLAYBACK_COMPLETED) ) {

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
)
4242
public class PlayAudioFiles extends BaseFragment implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {
4343
private static final String TAG = PlayAudioFiles.class.getSimpleName();
44+
private static final int EFFECT_SOUND_ID = 0;
4445
private EditText et_channel;
4546
private Button mute, join, speaker, bgm, effect;
4647
private SeekBar mixingPublishVolBar, mixingPlayoutVolBar, mixingVolBar;
@@ -143,24 +144,31 @@ private void preloadAudioEffect(){
143144
// Only mp3, aac, m4a, 3gp, and wav files are supported.
144145
// You may need to record the sound IDs and their file paths.
145146
int id = 0;
146-
int preloadRet = audioEffectManager.preloadEffect(id++, Constant.EFFECT_FILE_PATH);
147+
audioEffectManager.preloadEffect(id++, Constant.EFFECT_FILE_PATH);
148+
}
149+
150+
private void playEffect() {
147151
/** Plays an audio effect file.
148152
* Returns
149153
* 0: Success.
150154
* < 0: Failure.
151155
*/
152156
int playRet = audioEffectManager.playEffect(
153-
0, // The sound ID of the audio effect file to be played.
157+
EFFECT_SOUND_ID, // The sound ID of the audio effect file to be played.
154158
Constant.EFFECT_FILE_PATH, // The file path of the audio effect file.
155159
-1, // The number of playback loops. -1 means an infinite loop.
156160
1, // pitch The pitch of the audio effect. The value ranges between 0.5 and 2. The default value is 1 (no change to the pitch). The lower the value, the lower the pitch.
157161
0.0, // Sets the spatial position of the effect. 0 means the effect shows ahead.
158162
100, // Sets the volume. The value ranges between 0 and 100. 100 is the original volume.
159163
true // Sets whether to publish the audio effect.
160164
);
161-
// Pauses all audio effects.
162-
int pauseRet = audioEffectManager.pauseAllEffects();
163-
Log.i(TAG, "result preloadRet:" + preloadRet + ", playRet:"+ playRet + ", pauseRet:"+ pauseRet);
165+
Log.i(TAG, "result playRet:"+ playRet);
166+
}
167+
168+
private void stopEffect() {
169+
audioEffectManager.stopEffect(
170+
EFFECT_SOUND_ID // The sound ID of the audio effect file to be played.
171+
);
164172
}
165173

166174
@Override
@@ -246,7 +254,7 @@ else if (v.getId() == R.id.btn_speaker)
246254
speaker.setActivated(!speaker.isActivated());
247255
speaker.setText(getString(speaker.isActivated() ? R.string.speaker : R.string.earpiece));
248256
/**Turn off / on the speaker and change the audio playback route.*/
249-
engine.setDefaultAudioRoutetoSpeakerphone(speaker.isActivated());
257+
engine.setEnableSpeakerphone(speaker.isActivated());
250258
}
251259
else if(v.getId() == R.id.btn_bgm)
252260
{
@@ -266,11 +274,11 @@ else if (v.getId() == R.id.btn_effect)
266274
effect.setText(!effect.isActivated() ? getString(R.string.effect_on): getString(R.string.effect_off));
267275
if(effect.isActivated()){
268276
// Resumes playing all audio effects.
269-
audioEffectManager.resumeAllEffects();
277+
playEffect();
270278
}
271279
else {
272280
// Pauses all audio effects.
273-
audioEffectManager.pauseAllEffects();
281+
stopEffect();
274282
}
275283
}
276284
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ public void onDestroy() {
137137
engine.leaveChannel();
138138
engine.stopPreview();
139139
}
140-
handler.post(RtcEngine::destroy);
141140
engine = null;
142141
super.onDestroy();
142+
handler.post(RtcEngine::destroy);
143143
}
144144

145145
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ public void onDestroy() {
192192
textureBufferHelper = null;
193193
}
194194
}
195-
handler.post(RtcEngine::destroy);
196195
engine = null;
197196
super.onDestroy();
197+
handler.post(RtcEngine::destroy);
198198
}
199199

200200
@Override

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ public void onDestroy()
161161
engine.leaveChannel();
162162
engine.stopPreview();
163163
}
164+
if(retryTask != null){
165+
retryTask.cancel(true);
166+
}
164167
handler.post(RtcEngine::destroy);
165168
engine = null;
166169
}
@@ -541,6 +544,9 @@ public void onRemoteVideoStateChanged(int uid, int state, int reason, int elapse
541544
public void onRtmpStreamingStateChanged(String url, int state, int errCode) {
542545
super.onRtmpStreamingStateChanged(url, state, errCode);
543546
Log.i(TAG, "onRtmpStreamingStateChanged->" + url + ", state->" + state + ", errCode->" + errCode);
547+
if(retryTask == null){
548+
return;
549+
}
544550
if(state == RTMP_STREAM_PUBLISH_STATE.RTMP_STREAM_PUBLISH_STATE_RUNNING.getValue())
545551
{
546552
/**After confirming the successful push, make changes to the UI.*/
@@ -631,7 +637,7 @@ public void onUserJoined(int uid, int elapsed)
631637
/**The transcoding images are arranged vertically according to the adding order*/
632638
LiveTranscoding.TranscodingUser transcodingUser = new LiveTranscoding.TranscodingUser();
633639
transcodingUser.x = 0;
634-
transcodingUser.y = localTranscodingUser.height;
640+
transcodingUser.y = transcoding.height / MAXUserCount;
635641
transcodingUser.width = transcoding.width;
636642
transcodingUser.height = transcoding.height / MAXUserCount;
637643
transcodingUser.uid = uid;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ public void onDestroy() {
182182
engine.leaveChannel();
183183
engine.stopPreview();
184184
}
185-
handler.post(RtcEngine::destroy);
186185
engine = null;
187186
super.onDestroy();
187+
handler.post(RtcEngine::destroy);
188188
}
189189

190190

0 commit comments

Comments
 (0)