11package io .agora .api .example .examples .advanced ;
22
3+ import static io .agora .api .example .common .model .Examples .ADVANCED ;
4+
35import android .content .Context ;
4- import android .os .AsyncTask ;
56import android .os .Bundle ;
67import android .os .Handler ;
78import android .text .TextUtils ;
1213import android .widget .Button ;
1314import android .widget .EditText ;
1415import android .widget .SeekBar ;
16+ import android .widget .TextView ;
1517
1618import androidx .annotation .NonNull ;
1719import androidx .annotation .Nullable ;
2022import com .yanzhenjie .permission .AndPermission ;
2123import com .yanzhenjie .permission .runtime .Permission ;
2224
23- import java .text .SimpleDateFormat ;
24-
2525import io .agora .api .component .Constant ;
2626import io .agora .api .example .R ;
2727import io .agora .api .example .annotation .Example ;
3333import io .agora .rtc .RtcEngine ;
3434import 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}
0 commit comments