Skip to content

Commit 1f4c21e

Browse files
author
xianing
committed
fix 37200 iusses
1 parent e60f01d commit 1f4c21e

6 files changed

Lines changed: 117 additions & 215 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class ProcessAudioRawData extends BaseFragment implements View.OnClickLis
6464
private static final Integer SAMPLE_RATE = 44100;
6565
private static final Integer SAMPLE_NUM_OF_CHANNEL = 2;
6666
private static final Integer BIT_PER_SAMPLE = 8;
67-
private static final Integer SAMPLES = 441 * 10;
67+
private static final Integer SAMPLES = 1024;
6868
private static final String AUDIO_FILE = "output.raw";
6969
private InputStream inputStream;
7070
private SeekBar record;
@@ -313,7 +313,7 @@ private void joinChannel(String channelId) {
313313

314314
@Override
315315
public boolean onRecordAudioFrame(int audioFrameType, int samples, int bytesPerSample, int channels, int samplesPerSec, ByteBuffer byteBuffer, long renderTimeMs, int bufferLength) {
316-
Log.i(TAG, "onRecordAudioFrame " + isWriteBackAudio);
316+
// Log.i(TAG, "onRecordAudioFrame " + isWriteBackAudio);
317317
if(isWriteBackAudio){
318318
byte[] buffer = readBuffer();
319319
byte[] origin = new byte[byteBuffer.remaining()];

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

Lines changed: 21 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,22 @@ public class CustomAudioSource extends BaseFragment implements View.OnClickListe
5858
private int myUid;
5959
private boolean joined = false;
6060
public static RtcEngineEx engine;
61-
private static final Integer SAMPLE_RATE = 44100;
62-
private static final Integer SAMPLE_NUM_OF_CHANNEL = 1;
63-
private AudioPlayer mAudioPlayer;
64-
private Switch mic, pcm, loopback;
65-
public static RtcConnection rtcConnection2 = new RtcConnection();
61+
private Switch mic, pcm;
62+
private ChannelMediaOptions option = new ChannelMediaOptions();
6663

6764
@Override
6865
public void onCreate(@Nullable Bundle savedInstanceState)
6966
{
7067
super.onCreate(savedInstanceState);
7168
handler = new Handler();
69+
initMediaOption();
70+
}
71+
72+
private void initMediaOption() {
73+
option.autoSubscribeAudio = true;
74+
option.autoSubscribeVideo = true;
75+
option.publishAudioTrack = false;
76+
option.publishCustomAudioTrack = false;
7277
}
7378

7479
@Nullable
@@ -88,10 +93,8 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
8893
view.findViewById(R.id.btn_join).setOnClickListener(this);
8994
mic = view.findViewById(R.id.microphone);
9095
pcm = view.findViewById(R.id.localAudio);
91-
loopback = view.findViewById(R.id.playLocally);
9296
mic.setOnCheckedChangeListener(this);
9397
pcm.setOnCheckedChangeListener(this);
94-
loopback.setOnCheckedChangeListener(this);
9598
checkLocalAudio();
9699
}
97100

@@ -138,8 +141,6 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState)
138141
config.mEventHandler = iRtcEngineEventHandler;
139142
config.mAudioScenario = Constants.AudioScenario.getValue(Constants.AudioScenario.HIGH_DEFINITION);
140143
engine = (RtcEngineEx) RtcEngine.create(config);
141-
142-
mAudioPlayer = new AudioPlayer(AudioManager.STREAM_VOICE_CALL, SAMPLE_RATE, SAMPLE_NUM_OF_CHANNEL, AudioFormat.CHANNEL_OUT_MONO);
143144
}
144145
catch (Exception e)
145146
{
@@ -152,7 +153,6 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState)
152153
public void onDestroy()
153154
{
154155
super.onDestroy();
155-
stopAudioRecord();
156156
stopPCMRecord();
157157
/**leaveChannel and Destroy the RtcEngine instance*/
158158
if(engine != null)
@@ -161,7 +161,6 @@ public void onDestroy()
161161
}
162162
handler.post(RtcEngine::destroy);
163163
engine = null;
164-
mAudioPlayer.stopPlayer();
165164
}
166165

167166

@@ -170,39 +169,25 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
170169
if (compoundButton.getId() == R.id.microphone)
171170
{
172171
if(b){
173-
startAudioRecord();
172+
option.publishAudioTrack = true;
173+
engine.updateChannelMediaOptions(option);
174174
}
175175
else{
176-
stopAudioRecord();
176+
option.publishAudioTrack = false;
177+
engine.updateChannelMediaOptions(option);
177178
}
178179
}
179180
else if(compoundButton.getId() == R.id.localAudio)
180181
{
181-
// loopback.setVisibility(b?View.VISIBLE:View.INVISIBLE);
182-
loopback.setEnabled(b);
183182
if(b){
184-
ChannelMediaOptions mediaOptions = new ChannelMediaOptions();
185-
mediaOptions.autoSubscribeAudio = false;
186-
mediaOptions.autoSubscribeVideo = false;
187-
mediaOptions.publishScreenTrack = false;
188-
mediaOptions.publishCameraTrack = false;
189-
mediaOptions.publishCustomAudioTrack = true;
190-
mediaOptions.enableAudioRecordingOrPlayout = false;
191-
mediaOptions.clientRoleType = Constants.CLIENT_ROLE_BROADCASTER;
192-
mediaOptions.channelProfile = Constants.CHANNEL_PROFILE_LIVE_BROADCASTING;
193-
rtcConnection2.channelId = et_channel.getText().toString();
194-
rtcConnection2.localUid = new Random().nextInt(512)+512;
195-
engine.joinChannelEx(null,rtcConnection2, mediaOptions, iRtcEngineEventHandlerEx);
183+
option.publishCustomAudioTrack = true;
184+
engine.updateChannelMediaOptions(option);
196185
}
197186
else{
198-
engine.leaveChannelEx(rtcConnection2);
199-
stopPCMRecord();
187+
option.publishCustomAudioTrack = false;
188+
engine.updateChannelMediaOptions(option);
200189
}
201190
}
202-
else if(compoundButton.getId() == R.id.playLocally)
203-
{
204-
205-
}
206191
}
207192

208193

@@ -235,7 +220,6 @@ public void onClick(View v)
235220
else
236221
{
237222
joined = false;
238-
stopAudioRecord();
239223
stopPCMRecord();
240224
/**After joining a channel, the user must call the leaveChannel method to end the
241225
* call before joining another channel. This method returns 0 if the user leaves the
@@ -258,7 +242,6 @@ public void onClick(View v)
258242
join.setText(getString(R.string.join));
259243
mic.setEnabled(false);
260244
pcm.setEnabled(false);
261-
loopback.setEnabled(false);
262245
}
263246
}
264247
}
@@ -297,9 +280,6 @@ private void joinChannel(String channelId)
297280
/** Allows a user to join a channel.
298281
if you do not specify the uid, we will generate the uid for you*/
299282

300-
ChannelMediaOptions option = new ChannelMediaOptions();
301-
option.autoSubscribeAudio = true;
302-
option.autoSubscribeVideo = true;
303283
int res = engine.joinChannel(accessToken, channelId, 0, option);
304284
if (res != 0)
305285
{
@@ -314,18 +294,6 @@ private void joinChannel(String channelId)
314294
join.setEnabled(false);
315295
}
316296

317-
private void startAudioRecord()
318-
{
319-
Intent intent = new Intent(getContext(), MicrophoneRecordService.class);
320-
getActivity().startService(intent);
321-
}
322-
323-
private void stopAudioRecord()
324-
{
325-
Intent intent = new Intent(getContext(), MicrophoneRecordService.class);
326-
getActivity().stopService(intent);
327-
}
328-
329297
private void startPCMRecord()
330298
{
331299
Intent intent = new Intent(getContext(), PCMRecordService.class);
@@ -336,6 +304,8 @@ private void stopPCMRecord()
336304
{
337305
Intent intent = new Intent(getContext(), PCMRecordService.class);
338306
getActivity().stopService(intent);
307+
option.publishCustomAudioTrack = false;
308+
engine.updateChannelMediaOptions(option);
339309
}
340310

341311
/**IRtcEngineEventHandler is an abstract class providing default implementation.
@@ -372,6 +342,7 @@ public void onJoinChannelSuccess(String channel, int uid, int elapsed)
372342
showLongToast(String.format("onJoinChannelSuccess channel %s uid %d", channel, uid));
373343
myUid = uid;
374344
joined = true;
345+
startPCMRecord();
375346
handler.post(new Runnable()
376347
{
377348
@Override
@@ -386,41 +357,4 @@ public void run()
386357
}
387358

388359
};
389-
390-
/**IRtcEngineEventHandler is an abstract class providing default implementation.
391-
* The SDK uses this class to report to the app on SDK runtime events.*/
392-
private final IRtcEngineEventHandler iRtcEngineEventHandlerEx = new IRtcEngineEventHandler()
393-
{
394-
/**Reports a warning during SDK runtime.
395-
* Warning code: https://docs.agora.io/en/Voice/API%20Reference/java/classio_1_1agora_1_1rtc_1_1_i_rtc_engine_event_handler_1_1_warn_code.html*/
396-
@Override
397-
public void onWarning(int warn)
398-
{
399-
Log.w(TAG, String.format("onWarning code %d message %s", warn, RtcEngine.getErrorDescription(warn)));
400-
}
401-
402-
/**Reports an error during SDK runtime.
403-
* Error code: https://docs.agora.io/en/Voice/API%20Reference/java/classio_1_1agora_1_1rtc_1_1_i_rtc_engine_event_handler_1_1_error_code.html*/
404-
@Override
405-
public void onError(int err)
406-
{
407-
Log.e(TAG, String.format("onError code %d message %s", err, RtcEngine.getErrorDescription(err)));
408-
showAlert(String.format("onError code %d message %s", err, RtcEngine.getErrorDescription(err)));
409-
}
410-
411-
/**Occurs when the local user joins a specified channel.
412-
* The channel name assignment is based on channelName specified in the joinChannel method.
413-
* If the uid is not specified when joinChannel is called, the server automatically assigns a uid.
414-
* @param channel Channel name
415-
* @param uid User ID
416-
* @param elapsed Time elapsed (ms) from the user calling joinChannel until this callback is triggered*/
417-
@Override
418-
public void onJoinChannelSuccess(String channel, int uid, int elapsed)
419-
{
420-
Log.i(TAG, String.format("onJoinChannelSuccess channel %s uid %d", channel, uid));
421-
showLongToast(String.format("onJoinChannelSuccess channel %s uid %d", channel, uid));
422-
startPCMRecord();
423-
}
424-
425-
};
426360
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,8 @@ private void readFile(File file) {
143143
* @return
144144
* 0: Success.
145145
* < 0: Failure.*/
146-
// int ret = CustomAudioSource.engine.pushExternalAudioFrame(
147-
// ByteBuffer.wrap(buffer), 0, CustomAudioSource.rtcConnection2.localUid);
148146
int ret = CustomAudioSource.engine.pushExternalAudioFrame(buffer, len);
147+
Log.i(TAG, "pushExternalAudioFrame result "+ ret);
149148
} catch (Exception e) {
150149
e.printStackTrace();
151150
}

Android/APIExample/app/src/main/res/values-zh/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
<string name="joinchannelvideo">此示例演示了如何使用SDK加入频道进行音视频通话的功能。</string>
116116
<string name="adjustvolume">此示例演示了如何使用SDK在频道中调整通话音量的功能。</string>
117117
<string name="precalltest">此示例演示了如何使用SDK在进入频道前检测网络质量状况。</string>
118-
<string name="customaudio">此示例演示了在语音通话过程中如何使用CustomSource和CustomSink接口进行自采集音频帧和自渲染音频帧的功能。</string>
118+
<string name="customaudio">此示例演示了在语音通话过程中使用MediaOption控制发布麦克风和自采集音频流的功能。</string>
119119
<string name="customremoterender">此示例演示了在音视频通话过程中如何自定义远端视频流渲染器的功能。</string>
120120
<string name="processrawdata">此示例演示了在音视频通话过程中如何通过回调获取裸数据,以及在数据被处理后如何返回给SDK的功能。\nPS:裸数据包括视频帧和音频帧。</string>
121121
<string name="pushexternalvideo">此示例演示了在音视频通话过程中如何以主动Push的方式进行视频自采集的功能。</string>

Android/APIExample/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
<string name="joinchannelvideo">This example demonstrates how to use the SDK to join channels for audio and video calls.</string>
120120
<string name="adjustvolume">This example demonstrates how to use the SDK to adjust in cal volume.</string>
121121
<string name="precalltest">This example demonstrates how to use the SDK to check uplink network condition before joining the channel.</string>
122-
<string name="customaudio">This example demonstrates how to use custom audio source and custom audio sink API to do Audio capture and renderer. </string>
122+
<string name="customaudio">This example demonstrates how to use mediaOption API to publish microphone and custom audio source. </string>
123123
<string name="customremoterender">This example demonstrates how to customize the functions of the remote video stream renderer during audio and video calls.</string>
124124
<string name="processrawdata">This example demonstrates how to obtain raw data through callback during audio and video calls, and how to return the data to the SDK after processing.\nPS:Bare data includes video frames and audio frames</string>
125125
<string name="pushexternalvideo">This example demonstrates how to use the active push mode to collect video during audio and video calls.</string>

0 commit comments

Comments
 (0)