Skip to content

Commit 18154ab

Browse files
author
xianing
committed
fix gradle dependency, add sample code for raw audio data
1 parent 3f1f1a9 commit 18154ab

File tree

9 files changed

+79
-22
lines changed

9 files changed

+79
-22
lines changed

Android/APIExample/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ dependencies {
5050
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
5151
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
5252

53-
implementation 'io.github.luizgrp.sectionedrecyclerviewadapter:sectionedrecyclerviewadapter:1.2.0'
54-
implementation 'com.yanzhenjie:permission:2.0.3'
53+
implementation 'com.github.luizgrp:SectionedRecyclerViewAdapter:v3.2.0'
54+
implementation 'com.yanzhenjie:permission:2.0.2'
5555
implementation 'de.javagl:obj:0.2.1'
5656
implementation 'com.google.ar:core:1.0.0'
5757

8.39 MB
Binary file not shown.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ public void onPlayBufferUpdated(long l) {
202202

203203
}
204204

205+
@Override
206+
public void onPreloadEvent(String s, Constants.MediaPlayerPreloadEvent mediaPlayerPreloadEvent) {
207+
208+
}
209+
205210
@Override
206211
public void onPlayerEvent(Constants.MediaPlayerEvent eventCode) {
207212
LogUtil.i("agoraMediaPlayerKit1 onEvent:" + eventCode);

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

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

33
import android.content.Context;
4-
import android.media.AudioFormat;
5-
import android.media.AudioManager;
64
import android.os.Bundle;
75
import android.os.Handler;
86
import android.text.TextUtils;
@@ -21,10 +19,13 @@
2119
import com.yanzhenjie.permission.AndPermission;
2220
import com.yanzhenjie.permission.runtime.Permission;
2321

22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.nio.ByteBuffer;
25+
2426
import io.agora.api.example.R;
2527
import io.agora.api.example.annotation.Example;
2628
import io.agora.api.example.common.BaseFragment;
27-
import io.agora.api.example.examples.advanced.customaudio.AudioPlayer;
2829
import io.agora.api.example.utils.CommonUtil;
2930
import io.agora.rtc.AudioFrame;
3031
import io.agora.rtc.Constants;
@@ -53,15 +54,17 @@ public class ProcessAudioRawData extends BaseFragment implements View.OnClickLis
5354
private static final String TAG = ProcessAudioRawData.class.getSimpleName();
5455
private EditText et_channel;
5556
private Button mute, join, speaker;
56-
private Switch loopback;
57+
private Switch writeBackAudio;
5758
private RtcEngine engine;
5859
private int myUid;
5960
private boolean joined = false;
60-
private boolean isEnableLoopBack = false;
61-
private AudioPlayer mAudioPlayer;
61+
private boolean isWriteBackAudio = false;
6262
private static final Integer SAMPLE_RATE = 44100;
63+
private static final Integer SAMPLE_NUM_OF_CHANNEL = 2;
64+
private static final Integer BIT_PER_SAMPLE = 16;
6365
private static final Integer SAMPLES_PER_CALL = 4410;
64-
private static final Integer SAMPLE_NUM_OF_CHANNEL = 1;
66+
private static final String AUDIO_FILE = "output.raw";
67+
private InputStream inputStream;
6568

6669
@Override
6770
public void onCreate(@Nullable Bundle savedInstanceState) {
@@ -86,8 +89,8 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
8689
mute.setOnClickListener(this);
8790
speaker = view.findViewById(R.id.btn_speaker);
8891
speaker.setOnClickListener(this);
89-
loopback = view.findViewById(R.id.loopback);
90-
loopback.setOnCheckedChangeListener(this);
92+
writeBackAudio = view.findViewById(R.id.audioWriteBack);
93+
writeBackAudio.setOnCheckedChangeListener(this);
9194
}
9295

9396
@Override
@@ -115,7 +118,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
115118
* - < 0: Failure.
116119
*/
117120
engine.registerAudioFrameObserver(audioFrameObserver);
118-
mAudioPlayer = new AudioPlayer(AudioManager.STREAM_VOICE_CALL, SAMPLE_RATE, SAMPLE_NUM_OF_CHANNEL, AudioFormat.CHANNEL_OUT_MONO);
121+
openAudioFile();
119122
}
120123
catch (Exception e) {
121124
e.printStackTrace();
@@ -132,7 +135,45 @@ public void onDestroy() {
132135
}
133136
handler.post(RtcEngine::destroy);
134137
engine = null;
135-
mAudioPlayer.stopPlayer();
138+
closeAudioFile();
139+
}
140+
141+
private void openAudioFile(){
142+
try {
143+
inputStream = this.getResources().getAssets().open(AUDIO_FILE);
144+
} catch (IOException e) {
145+
e.printStackTrace();
146+
}
147+
}
148+
149+
private void closeAudioFile(){
150+
try {
151+
inputStream.close();
152+
} catch (IOException e) {
153+
e.printStackTrace();
154+
}
155+
}
156+
157+
private byte[] readBuffer(){
158+
int byteSize = SAMPLES_PER_CALL * BIT_PER_SAMPLE / 8 * SAMPLE_NUM_OF_CHANNEL;
159+
byte[] buffer = new byte[byteSize];
160+
try {
161+
if(inputStream.read(buffer) < 0){
162+
inputStream.reset();
163+
return readBuffer();
164+
}
165+
} catch (IOException e) {
166+
e.printStackTrace();
167+
}
168+
return buffer;
169+
}
170+
171+
private byte[] audioAggregate(byte[] origin, byte[] buffer) {
172+
byte[] output = new byte[origin.length];
173+
for (int i = 0; i < origin.length; i++) {
174+
output[i] = (byte) ((int) origin[i] + (int) buffer[i] / 2);
175+
}
176+
return output;
136177
}
137178

138179
@Override
@@ -282,7 +323,6 @@ public void onLeaveChannel(RtcStats stats) {
282323
public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
283324
Log.i(TAG, String.format("onJoinChannelSuccess channel %s uid %d", channel, uid));
284325
showLongToast(String.format("onJoinChannelSuccess channel %s uid %d", channel, uid));
285-
mAudioPlayer.startPlayer();
286326
myUid = uid;
287327
joined = true;
288328
handler.post(new Runnable() {
@@ -292,7 +332,7 @@ public void run() {
292332
mute.setEnabled(true);
293333
join.setEnabled(true);
294334
join.setText(getString(R.string.leave));
295-
loopback.setEnabled(true);
335+
writeBackAudio.setEnabled(true);
296336
}
297337
});
298338
}
@@ -372,7 +412,16 @@ public void onActiveSpeaker(int uid) {
372412
private final IAudioFrameObserver audioFrameObserver = new IAudioFrameObserver() {
373413
@Override
374414
public boolean onRecordFrame(AudioFrame audioFrame) {
375-
return false;
415+
Log.i(TAG, "onRecordAudioFrame " + isWriteBackAudio);
416+
if(isWriteBackAudio){
417+
ByteBuffer byteBuffer = audioFrame.samples;
418+
byte[] buffer = readBuffer();
419+
byte[] origin = new byte[byteBuffer.remaining()];
420+
byteBuffer.get(origin);
421+
byteBuffer.flip();
422+
byteBuffer.put(audioAggregate(origin, buffer), 0, byteBuffer.remaining());
423+
}
424+
return true;
376425
}
377426

378427
@Override
@@ -402,12 +451,12 @@ public boolean onPlaybackFrameBeforeMixingEx(AudioFrame audioFrame, int uid, Str
402451

403452
@Override
404453
public int getObservedAudioFramePosition() {
405-
return 0;
454+
return IAudioFrameObserver.POSITION_RECORD;
406455
}
407456

408457
@Override
409458
public AudioParams getRecordAudioParams() {
410-
return new AudioParams(SAMPLE_RATE, SAMPLE_NUM_OF_CHANNEL, Constants.RAW_AUDIO_FRAME_OP_MODE_READ_ONLY, SAMPLES_PER_CALL);
459+
return new AudioParams(SAMPLE_RATE, SAMPLE_NUM_OF_CHANNEL, Constants.RAW_AUDIO_FRAME_OP_MODE_READ_WRITE, SAMPLES_PER_CALL);
411460
}
412461

413462
@Override
@@ -423,6 +472,6 @@ public AudioParams getMixedAudioParams() {
423472

424473
@Override
425474
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
426-
isEnableLoopBack = b;
475+
isWriteBackAudio = b;
427476
}
428477
}

Android/APIExample/app/src/main/res/layout/fragment_raw_audio.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
tools:context=".examples.basic.JoinChannelAudio">
88

99
<Switch
10-
android:id="@+id/loopback"
10+
android:id="@+id/audioWriteBack"
1111
android:layout_width="wrap_content"
1212
android:layout_height="wrap_content"
1313
android:layout_alignParentEnd="true"
1414
android:layout_alignParentBottom="true"
1515
android:layout_marginEnd="16dp"
1616
android:layout_marginBottom="200dp"
1717
android:enabled="false"
18-
android:text="@string/loopback" />
18+
android:text="@string/mixing_audio" />
1919

2020
<androidx.appcompat.widget.AppCompatButton
2121
android:id="@+id/btn_mute"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,5 @@
139139
<string name="mixing_playout_vol">混音播放音量</string>
140140
<string name="effect_control">音效控制</string>
141141
<string name="audio_effects_vol">音效音量</string>
142+
<string name="mixing_audio">混音</string>
142143
</resources>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,5 @@
143143
<string name="mixing_playout_vol">Mixing Playout Vol</string>
144144
<string name="effect_control">Effect Control</string>
145145
<string name="audio_effects_vol">Audio Effects Vol</string>
146+
<string name="mixing_audio">Audio Mixing</string>
146147
</resources>

Android/APIExample/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ allprojects {
2020
repositories {
2121
google()
2222
mavenCentral()
23+
maven { url 'https://jitpack.io' }
2324
}
2425
}
2526

Android/APIExample/lib-component/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ dependencies {
3131
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
3232

3333
api 'io.agora.rtc:full-sdk:3.5.0'
34-
api 'io.agora:agoraplayer:1.2.4'
34+
api 'io.agora:player:1.3.0'
3535

3636
}

0 commit comments

Comments
 (0)