|
26 | 26 | import io.agora.api.example.common.BaseFragment; |
27 | 27 | import io.agora.api.example.examples.advanced.customaudio.AudioPlayer; |
28 | 28 | import io.agora.api.example.utils.CommonUtil; |
| 29 | +import io.agora.rtc.AudioFrame; |
29 | 30 | import io.agora.rtc.Constants; |
30 | 31 | import io.agora.rtc.IAudioFrameObserver; |
31 | 32 | import io.agora.rtc.IRtcEngineEventHandler; |
32 | 33 | import io.agora.rtc.RtcEngine; |
| 34 | +import io.agora.rtc.audio.AudioParams; |
33 | 35 | import io.agora.rtc.models.ChannelMediaOptions; |
34 | 36 |
|
35 | 37 | import static io.agora.api.example.common.model.Examples.ADVANCED; |
36 | | -import static io.agora.api.example.common.model.Examples.BASIC; |
| 38 | +import static io.agora.rtc.IRtcEngineEventHandler.ClientRole.CLIENT_ROLE_BROADCASTER; |
37 | 39 |
|
38 | 40 | /** |
39 | 41 | * This demo demonstrates how to make a one-to-one voice call |
@@ -112,133 +114,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
112 | 114 | * - 0: Success. |
113 | 115 | * - < 0: Failure. |
114 | 116 | */ |
115 | | - engine.registerAudioFrameObserver(new IAudioFrameObserver() { |
116 | | - /** Occurs when the recorded audio frame is received. |
117 | | - * |
118 | | - * @param samples Sample data of the frame. |
119 | | - * @param numOfSamples Number of samples. |
120 | | - * @param bytesPerSample Number of bytes per audio sample. For example, each PCM audio sample usually takes up 16 bits (2 bytes). |
121 | | - * @param channels Number of audio channels. If the channel uses stereo, the data is interleaved. |
122 | | - * <ul> |
123 | | - * <li>1: Mono. |
124 | | - * <li>2: Stereo. |
125 | | - * </ul> |
126 | | - * @param samplesPerSec The number of samples per channel per second in the audio frame. |
127 | | - * @return |
128 | | - * <ul> |
129 | | - * <li>true: The recorded audio frame is valid and is encoded and sent.</li> |
130 | | - * <li>false: The recorded audio frame is invalid and is not encoded or sent.</li> |
131 | | - * </ul> |
132 | | - */ |
133 | | - @Override |
134 | | - public boolean onRecordFrame(byte[] samples, int numOfSamples, int bytesPerSample, int channels, int samplesPerSec) { |
135 | | - if(isEnableLoopBack){ |
136 | | - mAudioPlayer.play(samples, 0, numOfSamples * bytesPerSample); |
137 | | - } |
138 | | - return false; |
139 | | - } |
140 | | - /** Occurs when the playback audio frame is received. |
141 | | - * |
142 | | - * @param samples Sample data of the frame. |
143 | | - * @param numOfSamples Number of samples. |
144 | | - * @param bytesPerSample Number of bytes per audio sample. For example, each PCM audio sample usually takes up 16 bits (2 bytes). |
145 | | - * @param channels Number of audio channels. If the channel uses stereo, the data is interleaved. |
146 | | - * <ul> |
147 | | - * <li>1: Mono. |
148 | | - * <li>2: Stereo. |
149 | | - * </ul> |
150 | | - * @param samplesPerSec The number of samples per channel per second in the audio frame. |
151 | | - * @return |
152 | | - * <ul> |
153 | | - * <li>true: The playback audio frame is valid and is encoded and sent.</li> |
154 | | - * <li>false: The playback audio frame is invalid and is not encoded or sent.</li> |
155 | | - * </ul> |
156 | | - */ |
157 | | - @Override |
158 | | - public boolean onPlaybackFrame(byte[] samples, int numOfSamples, int bytesPerSample, int channels, int samplesPerSec) { |
159 | | - return false; |
160 | | - } |
161 | | - |
162 | | - /** Occurs when the audio frame of a specified user before mixing. |
163 | | - * |
164 | | - * @note This callback only returns the single-channel data. |
165 | | - * |
166 | | - * @param samples Sample data of the frame. |
167 | | - * @param numOfSamples Number of samples. |
168 | | - * @param bytesPerSample Number of bytes per audio sample. For example, each PCM audio sample usually takes up 16 bits (2 bytes). |
169 | | - * @param channels Number of audio channels. If the channel uses stereo, the data is interleaved. |
170 | | - * <ul> |
171 | | - * <li>1: Mono. |
172 | | - * <li>2: Stereo. |
173 | | - * </ul> |
174 | | - * @param samplesPerSec The number of samples per channel per second in the audio frame. |
175 | | - * @param uid The User ID. |
176 | | - * @return |
177 | | - * <ul> |
178 | | - * <li>true: The playback audio frame is valid and the mixed recorded and playback audio frame is encoded and sent.</li> |
179 | | - * <li>false: The playback audio frame is invalid and the mixed recorded and playback audio frame is not encoded or sent.</li> |
180 | | - * </ul> |
181 | | - */ |
182 | | - @Override |
183 | | - public boolean onPlaybackFrameBeforeMixing(byte[] samples, int numOfSamples, int bytesPerSample, int channels, int samplesPerSec, int uid) { |
184 | | - return false; |
185 | | - } |
186 | | - |
187 | | - /** Occurs when the mixed recorded and playback audio frame. |
188 | | - * |
189 | | - * @param samples Sample data of the frame. |
190 | | - * @param numOfSamples Number of samples. |
191 | | - * @param bytesPerSample Number of bytes per audio sample. For example, each PCM audio sample usually takes up 16 bits (2 bytes). |
192 | | - * @param channels Number of audio channels. If the channel uses stereo, the data is interleaved. |
193 | | - * <ul> |
194 | | - * <li>1: Mono. |
195 | | - * <li>2: Stereo. |
196 | | - * </ul> |
197 | | - * @param samplesPerSec The number of samples per channel per second in the audio frame. |
198 | | - * @return |
199 | | - * <ul> |
200 | | - * <li>true: The playback audio frame is valid and the mixed recorded and playback audio frame is encoded and sent.</li> |
201 | | - * <li>false: The playback audio frame is invalid and the mixed recorded and playback audio frame is not encoded or sent.</li> |
202 | | - * </ul> |
203 | | - */ |
204 | | - @Override |
205 | | - public boolean onMixedFrame(byte[] samples, int numOfSamples, int bytesPerSample, int channels, int samplesPerSec) { |
206 | | - return false; |
207 | | - } |
208 | | - |
209 | | - /** |
210 | | - * |
211 | | - * @return |
212 | | - * <ul> |
213 | | - * <li>true: The playback audio frame is valid and the mixed recorded and playback audio frame is encoded and sent.</li> |
214 | | - * <li>false: The playback audio frame is invalid and the mixed recorded and playback audio frame is not encoded or sent.</li> |
215 | | - * </ul> |
216 | | - */ |
217 | | - @Override |
218 | | - public boolean isMultipleChannelFrameWanted() { |
219 | | - return false; |
220 | | - } |
221 | | - |
222 | | - /** |
223 | | - * Occurs when the playback audio frame is received. |
224 | | - * @param samples Sample data of the frame. |
225 | | - * @param numOfSamples Number of samples. |
226 | | - * @param bytesPerSample Number of bytes per audio sample. For example, each PCM audio sample usually takes up 16 bits (2 bytes). |
227 | | - * @param channels Number of audio channels. If the channel uses stereo, the data is interleaved. |
228 | | - * @param samplesPerSec The number of samples per channel per second in the audio frame. |
229 | | - * @param uid The User ID. |
230 | | - * @param channelId The Channel ID. |
231 | | - * @return |
232 | | - * <ul> |
233 | | - * <li>true: The playback audio frame is valid and the mixed recorded and playback audio frame is encoded and sent.</li> |
234 | | - * <li>false: The playback audio frame is invalid and the mixed recorded and playback audio frame is not encoded or sent.</li> |
235 | | - * </ul> |
236 | | - */ |
237 | | - @Override |
238 | | - public boolean onPlaybackFrameBeforeMixingEx(byte[] samples, int numOfSamples, int bytesPerSample, int channels, int samplesPerSec, int uid, String channelId) { |
239 | | - return false; |
240 | | - } |
241 | | - }); |
| 117 | + engine.registerAudioFrameObserver(audioFrameObserver); |
242 | 118 | engine.setRecordingAudioFrameParameters(SAMPLE_RATE, SAMPLE_NUM_OF_CHANNEL, Constants.RAW_AUDIO_FRAME_OP_MODE_READ_ONLY, SAMPLES_PER_CALL); |
243 | 119 | engine.setMixedAudioFrameParameters(SAMPLE_RATE, SAMPLES_PER_CALL); |
244 | 120 | engine.setPlaybackAudioFrameParameters(SAMPLE_RATE, SAMPLE_NUM_OF_CHANNEL, Constants.RAW_AUDIO_FRAME_OP_MODE_READ_ONLY, SAMPLES_PER_CALL); |
@@ -335,7 +211,7 @@ private void joinChannel(String channelId) { |
335 | 211 | an audience can only receive streams.*/ |
336 | 212 | engine.setChannelProfile(Constants.CHANNEL_PROFILE_LIVE_BROADCASTING); |
337 | 213 | /**In the demo, the default is to enter as the anchor.*/ |
338 | | - engine.setClientRole(IRtcEngineEventHandler.ClientRole.CLIENT_ROLE_BROADCASTER); |
| 214 | + engine.setClientRole(CLIENT_ROLE_BROADCASTER); |
339 | 215 | /**Please configure accessToken in the string_config file. |
340 | 216 | * A temporary token generated in Console. A temporary token is valid for 24 hours. For details, see |
341 | 217 | * https://docs.agora.io/en/Agora%20Platform/token?platform=All%20Platforms#get-a-temporary-token |
@@ -373,6 +249,7 @@ private void joinChannel(String channelId) { |
373 | 249 | * The SDK uses this class to report to the app on SDK runtime events. |
374 | 250 | */ |
375 | 251 | private final IRtcEngineEventHandler iRtcEngineEventHandler = new IRtcEngineEventHandler() { |
| 252 | + |
376 | 253 | /**Reports a warning during SDK runtime. |
377 | 254 | * 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*/ |
378 | 255 | @Override |
@@ -495,6 +372,58 @@ public void onActiveSpeaker(int uid) { |
495 | 372 | } |
496 | 373 | }; |
497 | 374 |
|
| 375 | + private final IAudioFrameObserver audioFrameObserver = new IAudioFrameObserver() { |
| 376 | + @Override |
| 377 | + public boolean onRecordFrame(AudioFrame audioFrame) { |
| 378 | + return false; |
| 379 | + } |
| 380 | + |
| 381 | + @Override |
| 382 | + public boolean onPlaybackFrame(AudioFrame audioFrame) { |
| 383 | + return false; |
| 384 | + } |
| 385 | + |
| 386 | + @Override |
| 387 | + public boolean onPlaybackFrameBeforeMixing(AudioFrame audioFrame, int uid) { |
| 388 | + return false; |
| 389 | + } |
| 390 | + |
| 391 | + @Override |
| 392 | + public boolean onMixedFrame(AudioFrame audioFrame) { |
| 393 | + return false; |
| 394 | + } |
| 395 | + |
| 396 | + @Override |
| 397 | + public boolean isMultipleChannelFrameWanted() { |
| 398 | + return false; |
| 399 | + } |
| 400 | + |
| 401 | + @Override |
| 402 | + public boolean onPlaybackFrameBeforeMixingEx(AudioFrame audioFrame, int uid, String channelId) { |
| 403 | + return false; |
| 404 | + } |
| 405 | + |
| 406 | + @Override |
| 407 | + public int getObservedFramePosition() { |
| 408 | + return 0; |
| 409 | + } |
| 410 | + |
| 411 | + @Override |
| 412 | + public AudioParams getRecordAudioParams() { |
| 413 | + return null; |
| 414 | + } |
| 415 | + |
| 416 | + @Override |
| 417 | + public AudioParams getPlaybackAudioParams() { |
| 418 | + return null; |
| 419 | + } |
| 420 | + |
| 421 | + @Override |
| 422 | + public AudioParams getMixedAudioParams() { |
| 423 | + return null; |
| 424 | + } |
| 425 | + }; |
| 426 | + |
498 | 427 | @Override |
499 | 428 | public void onCheckedChanged(CompoundButton compoundButton, boolean b) { |
500 | 429 | isEnableLoopBack = b; |
|
0 commit comments