Skip to content

Commit 34e4b08

Browse files
authored
Merge pull request #221 from oOJohn6Oo/feature-351
[FEAT] NMS-714 complete.
2 parents 825f9d9 + 36b5c2e commit 34e4b08

6 files changed

Lines changed: 213 additions & 34 deletions

File tree

-1.85 MB
Binary file not shown.

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

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

3+
import static io.agora.api.example.common.model.Examples.ADVANCED;
4+
35
import android.content.Context;
4-
import android.os.AsyncTask;
56
import android.os.Bundle;
67
import android.os.Handler;
78
import android.text.TextUtils;
@@ -12,6 +13,7 @@
1213
import android.widget.Button;
1314
import android.widget.EditText;
1415
import android.widget.SeekBar;
16+
import android.widget.TextView;
1517

1618
import androidx.annotation.NonNull;
1719
import androidx.annotation.Nullable;
@@ -20,8 +22,6 @@
2022
import com.yanzhenjie.permission.AndPermission;
2123
import com.yanzhenjie.permission.runtime.Permission;
2224

23-
import java.text.SimpleDateFormat;
24-
2525
import io.agora.api.component.Constant;
2626
import io.agora.api.example.R;
2727
import io.agora.api.example.annotation.Example;
@@ -33,8 +33,6 @@
3333
import io.agora.rtc.RtcEngine;
3434
import io.agora.rtc.models.ChannelMediaOptions;
3535

36-
import static io.agora.api.example.common.model.Examples.ADVANCED;
37-
3836
@Example(
3937
index = 14,
4038
group = ADVANCED,
@@ -47,12 +45,18 @@ public class PlayAudioFiles extends BaseFragment implements View.OnClickListener
4745
private EditText et_channel;
4846
private AppCompatTextView progressText;
4947
private Button join, bgm_start, bgm_resume, bgm_pause, bgm_stop, effect;
50-
private SeekBar mixingPublishVolBar, mixingPlayoutVolBar, mixingVolBar, mixingProgressBar;
48+
private SeekBar mixingPublishVolBar, mixingPlayoutVolBar, mixingVolBar, mixingProgressBar, sliderSpeed;
49+
private TextView titleSpeed;
50+
private TextView titleTrack;
5151
private RtcEngine engine;
5252
private int myUid;
5353
private boolean joined = false;
5454
private IAudioEffectManager audioEffectManager;
5555

56+
private String currentMusic = Constant.URL_PLAY_AUDIO_FILES;
57+
private int currentTrackIndex;
58+
59+
5660
@Override
5761
public void onCreate(@Nullable Bundle savedInstanceState)
5862
{
@@ -64,8 +68,7 @@ public void onCreate(@Nullable Bundle savedInstanceState)
6468
@Override
6569
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
6670
{
67-
View view = inflater.inflate(R.layout.fragment_play_audio_files, container, false);
68-
return view;
71+
return inflater.inflate(R.layout.fragment_play_audio_files, container, false);
6972
}
7073

7174
@Override
@@ -85,6 +88,9 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
8588
bgm_stop = view.findViewById(R.id.bgmStop);
8689
bgm_stop.setOnClickListener(this);
8790
effect = view.findViewById(R.id.btn_effect);
91+
sliderSpeed = view.findViewById(R.id.slider_speed_fg_audio_file);
92+
titleSpeed = view.findViewById(R.id.title_speed_fg_audio_file);
93+
titleTrack = view.findViewById(R.id.title_track_fg_audio_file);
8894
effect.setOnClickListener(this);
8995
mixingPublishVolBar = view.findViewById(R.id.mixingPublishVolBar);
9096
mixingPlayoutVolBar = view.findViewById(R.id.mixingPlayoutVolBar);
@@ -94,6 +100,36 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
94100
mixingPlayoutVolBar.setOnSeekBarChangeListener(this);
95101
mixingPublishVolBar.setOnSeekBarChangeListener(this);
96102
mixingVolBar.setOnSeekBarChangeListener(this);
103+
sliderSpeed.setOnSeekBarChangeListener(this);
104+
105+
view.findViewById(R.id.btn_to_last_track_fg_audio_file).setOnClickListener(this);
106+
view.findViewById(R.id.btn_to_next_track_fg_audio_file).setOnClickListener(this);
107+
108+
view.findViewById(R.id.btn_channel_left_fg_audio_file).setOnClickListener(this);
109+
view.findViewById(R.id.btn_channel_right_fg_audio_file).setOnClickListener(this);
110+
view.findViewById(R.id.btn_channel_stereo_fg_audio_file).setOnClickListener(this);
111+
view.findViewById(R.id.btn_channel_mix_fg_audio_file).setOnClickListener(this);
112+
113+
updateSpeedTitle();
114+
updateTrackTitle();
115+
}
116+
117+
/**
118+
* 声道切换
119+
*/
120+
private void updateChannel(int index){
121+
int value = Constants.AudioMixingDualMonoMode.getValue(Constants.AudioMixingDualMonoMode.values()[index]);
122+
engine.setAudioMixingDualMonoMode(value);
123+
}
124+
125+
private void updateSpeedTitle(){
126+
titleSpeed.setText(getString(R.string.play_speed,(sliderSpeed.getProgress()+50)/100f));
127+
}
128+
private void updateTrackTitle(){
129+
int count = 0;
130+
if(engine!=null) count = engine.getAudioTrackCount();
131+
String desc = getString(R.string.track_change,count);
132+
titleTrack.setText(desc);
97133
}
98134

99135
@Override
@@ -177,7 +213,8 @@ public void onClick(View v)
177213
{
178214
if (!joined)
179215
{
180-
CommonUtil.hideInputBoard(getActivity(), et_channel);
216+
et_channel.clearFocus();
217+
CommonUtil.hideInputBoard(requireActivity(), et_channel);
181218
// call when join button hit
182219
String channelId = et_channel.getText().toString();
183220
// Check permission
@@ -228,7 +265,9 @@ public void onClick(View v)
228265
}
229266
else if(v.getId() == R.id.bgmStart)
230267
{
231-
engine.startAudioMixing(Constant.MIX_FILE_PATH, false, false, -1, 0);
268+
sliderSpeed.setProgress(50);
269+
engine.startAudioMixing(currentMusic, false, false, -1, 0);
270+
engine.getAudioFileInfo(currentMusic);
232271
startProgressTimer();
233272
}
234273
else if(v.getId() == R.id.bgmStop){
@@ -254,6 +293,33 @@ else if (v.getId() == R.id.btn_effect)
254293
audioEffectManager.pauseAllEffects();
255294
}
256295
}
296+
else{
297+
switch (v.getId()){
298+
case R.id.btn_to_last_track_fg_audio_file:{
299+
updateTrackTitle();
300+
if(currentTrackIndex==0)
301+
currentTrackIndex = engine.getAudioTrackCount() - 1;
302+
else currentTrackIndex--;
303+
engine.selectAudioTrack(currentTrackIndex);
304+
break;
305+
}
306+
case R.id.btn_to_next_track_fg_audio_file:{
307+
updateTrackTitle();
308+
currentTrackIndex = (++currentTrackIndex) % engine.getAudioTrackCount();
309+
engine.selectAudioTrack(currentTrackIndex);
310+
break;
311+
}
312+
313+
case R.id.btn_channel_left_fg_audio_file:updateChannel(1);
314+
break;
315+
case R.id.btn_channel_right_fg_audio_file:updateChannel(2);
316+
break;
317+
case R.id.btn_channel_stereo_fg_audio_file:updateChannel(0);
318+
break;
319+
case R.id.btn_channel_mix_fg_audio_file:updateChannel(3);
320+
break;
321+
}
322+
}
257323
}
258324

259325
private void stopProgressTimer() {
@@ -262,10 +328,12 @@ private void stopProgressTimer() {
262328
}
263329

264330
private void startProgressTimer() {
265-
int currentPosition = engine.getAudioMixingCurrentPosition();
266-
if(mixingProgressBar.getMax()!=0 && !mixingProgressBar.isPressed())
267-
mixingProgressBar.setProgress(currentPosition);
268-
handler.postDelayed(this::startProgressTimer, 1000);
331+
if(engine != null) {
332+
int currentPosition = engine.getAudioMixingCurrentPosition();
333+
if (mixingProgressBar.getMax() != 0 && !mixingProgressBar.isPressed())
334+
mixingProgressBar.setProgress(currentPosition);
335+
handler.postDelayed(this::startProgressTimer, 1000);
336+
}
269337
}
270338

271339
/**
@@ -370,7 +438,6 @@ public void run()
370438
bgm_pause.setEnabled(true);
371439
bgm_stop.setEnabled(true);
372440
effect.setEnabled(true);
373-
engine.getAudioFileInfo(Constant.MIX_FILE_PATH);
374441
}
375442
});
376443
}
@@ -445,7 +512,7 @@ public void onUserOffline(int uid, int reason)
445512

446513
@Override
447514
public void onRequestAudioFileInfo(AudioFileInfo info, int error) {
448-
Log.d(TAG, "onRequestAudioFileInfo: "+info.filePath);
515+
Log.d(TAG, "onRequestAudioFileInfo: "+info.durationMs);
449516
handler.post(()-> mixingProgressBar.setMax(info.durationMs));
450517
}
451518
};
@@ -476,6 +543,8 @@ else if(seekBar.getId() == R.id.mixingVolBar){
476543
}else if(seekBar.getId() == R.id.mixingProgress){
477544
String durationText = io.agora.api.example.utils.TextUtils.durationFormat((long) progress);
478545
progressText.setText(durationText);
546+
}else if(seekBar.getId() == R.id.slider_speed_fg_audio_file){
547+
updateSpeedTitle();
479548
}
480549

481550
}
@@ -492,7 +561,9 @@ public void onStopTrackingTouch(SeekBar seekBar) {
492561
if(seekBar.getId() == R.id.mixingProgress) {
493562
engine.setAudioMixingPosition(seekBar.getProgress());
494563
handler.postDelayed(this::startProgressTimer, 1000);
564+
} else if(seekBar.getId() == R.id.slider_speed_fg_audio_file){
565+
if(sliderSpeed.isPressed())
566+
engine.setAudioMixingPlaybackSpeed(seekBar.getProgress()+50);
495567
}
496568
}
497-
498569
}

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

Lines changed: 105 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
3+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
34
xmlns:tools="http://schemas.android.com/tools"
5+
xmlns:app="http://schemas.android.com/apk/res-auto"
6+
tools:context=".examples.advanced.PlayAudioFiles"
47
android:layout_width="match_parent"
5-
android:layout_height="match_parent"
6-
tools:context=".examples.advanced.PlayAudioFiles">
7-
8+
android:layout_height="match_parent">
9+
<ScrollView
10+
android:fillViewport="true"
11+
app:layout_constraintTop_toTopOf="parent"
12+
app:layout_constraintBottom_toTopOf="@id/ll_join"
13+
android:layout_width="match_parent"
14+
android:layout_height="0dp">
815
<LinearLayout
9-
android:id="@+id/layout1"
1016
android:layout_width="match_parent"
11-
android:layout_height="match_parent"
17+
android:layout_height="wrap_content"
1218
android:orientation="vertical">
1319

1420

1521
<TextView
1622
android:id="@+id/lblMixing"
17-
android:layout_marginTop="40dp"
23+
android:layout_marginTop="24dp"
1824
android:layout_marginStart="16dp"
1925
android:layout_width="wrap_content"
2026
android:layout_height="24dp"
@@ -23,10 +29,8 @@
2329

2430

2531
<LinearLayout
26-
android:id="@+id/progressBar"
2732
android:layout_width="match_parent"
2833
android:layout_height="wrap_content"
29-
android:layout_marginBottom="16dp"
3034
android:gravity="center_vertical"
3135
android:orientation="horizontal">
3236

@@ -173,12 +177,95 @@
173177
</LinearLayout>
174178

175179

180+
<LinearLayout
181+
android:layout_width="match_parent"
182+
android:layout_height="wrap_content"
183+
android:layout_marginHorizontal="16dp"
184+
android:gravity="center_vertical"
185+
android:orientation="horizontal">
186+
187+
<TextView
188+
android:id="@+id/title_speed_fg_audio_file"
189+
android:layout_width="wrap_content"
190+
android:layout_height="wrap_content"
191+
android:text="@string/play_speed" />
192+
193+
<SeekBar
194+
android:id="@+id/slider_speed_fg_audio_file"
195+
android:layout_marginStart="16dp"
196+
android:max="150"
197+
android:progress="50"
198+
android:layout_width="match_parent"
199+
android:layout_height="wrap_content" />
200+
201+
</LinearLayout>
202+
203+
<TextView
204+
android:id="@+id/title_track_fg_audio_file"
205+
android:text="@string/track_change"
206+
android:paddingVertical="12dp"
207+
android:layout_width="wrap_content"
208+
android:layout_height="wrap_content"/>
209+
<LinearLayout
210+
android:layout_height="wrap_content"
211+
android:orientation="horizontal"
212+
android:layout_width="match_parent">
213+
214+
<Button
215+
android:id="@+id/btn_to_last_track_fg_audio_file"
216+
android:text="@string/track_last"
217+
android:layout_weight="1"
218+
android:layout_width="0dp"
219+
android:layout_height="wrap_content" />
220+
<Button
221+
android:id="@+id/btn_to_next_track_fg_audio_file"
222+
android:text="@string/track_next"
223+
android:layout_weight="1"
224+
android:layout_width="0dp"
225+
android:layout_height="wrap_content" />
226+
</LinearLayout>
227+
<TextView
228+
android:text="@string/channel_change"
229+
android:paddingVertical="12dp"
230+
android:layout_width="wrap_content"
231+
android:layout_height="wrap_content"/>
232+
<LinearLayout
233+
android:layout_height="wrap_content"
234+
android:orientation="horizontal"
235+
android:layout_width="match_parent">
236+
237+
<Button
238+
android:id="@+id/btn_channel_left_fg_audio_file"
239+
android:text="@string/channel_left"
240+
android:layout_weight="1"
241+
android:layout_width="0dp"
242+
android:layout_height="wrap_content" />
243+
<Button
244+
android:id="@+id/btn_channel_right_fg_audio_file"
245+
android:text="@string/channel_right"
246+
android:layout_weight="1"
247+
android:layout_width="0dp"
248+
android:layout_height="wrap_content" />
249+
<Button
250+
android:id="@+id/btn_channel_stereo_fg_audio_file"
251+
android:text="@string/channel_auto"
252+
android:layout_weight="1"
253+
android:layout_width="0dp"
254+
android:layout_height="wrap_content" />
255+
<Button
256+
android:id="@+id/btn_channel_mix_fg_audio_file"
257+
android:text="@string/channel_mix"
258+
android:layout_weight="1"
259+
android:layout_width="0dp"
260+
android:layout_height="wrap_content" />
261+
</LinearLayout>
262+
176263
<TextView
177264
android:id="@+id/lblEffect"
178-
android:layout_marginStart="16dp"
265+
android:layout_margin="16dp"
179266
android:layout_width="wrap_content"
180267
android:layout_height="24dp"
181-
android:textSize="20dp"
268+
android:textSize="20sp"
182269
android:text="@string/effect_control" />
183270

184271

@@ -215,15 +302,18 @@
215302
android:layout_height="wrap_content" />
216303

217304
</LinearLayout>
218-
</LinearLayout>
219305

306+
</LinearLayout>
220307

308+
</ScrollView>
221309
<LinearLayout
222310
android:id="@+id/ll_join"
223311
android:layout_width="match_parent"
224312
android:layout_height="wrap_content"
225-
android:layout_alignParentBottom="true"
226-
android:gravity="center_vertical"
313+
android:background="@android:color/white"
314+
android:padding="12dp"
315+
android:clipToPadding="false"
316+
app:layout_constraintBottom_toBottomOf="parent"
227317
android:orientation="horizontal">
228318

229319
<androidx.appcompat.widget.AppCompatEditText
@@ -242,4 +332,4 @@
242332

243333
</LinearLayout>
244334

245-
</RelativeLayout>
335+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)