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,14 +88,48 @@ 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 );
9197 mixingVolBar = view .findViewById (R .id .mixingVolBar );
9298 mixingProgressBar = view .findViewById (R .id .mixingProgress );
99+ mixingProgressBar .setOnSeekBarChangeListener (this );
93100 mixingPlayoutVolBar .setOnSeekBarChangeListener (this );
94101 mixingPublishVolBar .setOnSeekBarChangeListener (this );
95102 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 );
96133 }
97134
98135 @ Override
@@ -176,7 +213,8 @@ public void onClick(View v)
176213 {
177214 if (!joined )
178215 {
179- CommonUtil .hideInputBoard (getActivity (), et_channel );
216+ et_channel .clearFocus ();
217+ CommonUtil .hideInputBoard (requireActivity (), et_channel );
180218 // call when join button hit
181219 String channelId = et_channel .getText ().toString ();
182220 // Check permission
@@ -227,15 +265,13 @@ public void onClick(View v)
227265 }
228266 else if (v .getId () == R .id .bgmStart )
229267 {
230- engine . startAudioMixing ( Constant . MIX_FILE_PATH , false , false , - 1 , 0 );
231- String timeString = new SimpleDateFormat ( "mm:ss" ). format ( engine . getAudioMixingDuration ( Constant . MIX_FILE_PATH ) );
232- progressText . setText ( timeString );
268+ sliderSpeed . setProgress ( 50 );
269+ engine . startAudioMixing ( currentMusic , false , false , - 1 , 0 );
270+ engine . getAudioFileInfo ( currentMusic );
233271 startProgressTimer ();
234272 }
235273 else if (v .getId () == R .id .bgmStop ){
236274 engine .stopAudioMixing ();
237- progressText .setText ("00:00" );
238- mixingProgressBar .setProgress (0 );
239275 stopProgressTimer ();
240276 }
241277 else if (v .getId () == R .id .bgmResume ){
@@ -257,44 +293,44 @@ else if (v.getId() == R.id.btn_effect)
257293 audioEffectManager .pauseAllEffects ();
258294 }
259295 }
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+ }
260323 }
261324
262325 private void stopProgressTimer () {
263326 handler .removeCallbacksAndMessages (null );
264- // if(!progressTimer.isCancelled()){
265- // progressTimer.cancel(true);
266- // }
267327 }
268328
269329 private void startProgressTimer () {
270- final int result = (int ) ((float ) engine .getAudioMixingCurrentPosition () / (float ) engine .getAudioMixingDuration (Constant .MIX_FILE_PATH ) * 100 );
330+ final int result = (int ) ((float ) engine .getAudioMixingCurrentPosition () / (float ) engine .getAudioFileInfo (Constant .URL_PLAY_AUDIO_FILES ) * 100 );
271331 mixingProgressBar .setProgress (Long .valueOf (result ).intValue ());
272332 handler .postDelayed (this ::startProgressTimer , 500 );
273- // if(!progressTimer.getStatus().equals(AsyncTask.Status.RUNNING)){
274- // progressTimer.execute();
275- // }
276333 }
277-
278- // private final AsyncTask progressTimer = new AsyncTask() {
279- // @Override
280- // protected Object doInBackground(Object[] objects) {
281- // while(true){
282- // try {
283- // handler.post(new Runnable() {
284- // @Override
285- // public void run() {
286- // final int result = (int) ((float) engine.getAudioMixingCurrentPosition() / (float) engine.getAudioMixingDuration(Constant.MIX_FILE_PATH) * 100);
287- // mixingProgressBar.setProgress(Long.valueOf(result).intValue());
288- // }
289- // });
290- // Thread.sleep(500);
291- // } catch (InterruptedException e) {
292- // Log.e(TAG, e.getMessage());
293- // }
294- // }
295- // }
296- // };
297-
298334 /**
299335 * @param channelId Specify the channel name that you want to join.
300336 * Users that input the same channel name join the same channel.*/
@@ -468,6 +504,12 @@ public void onUserOffline(int uid, int reason)
468504 Log .i (TAG , String .format ("user %d offline! reason:%d" , uid , reason ));
469505 showLongToast (String .format ("user %d offline! reason:%d" , uid , reason ));
470506 }
507+
508+ @ Override
509+ public void onRequestAudioFileInfo (AudioFileInfo info , int error ) {
510+ Log .d (TAG , "onRequestAudioFileInfo: " +info .durationMs );
511+ handler .post (()-> mixingProgressBar .setMax (info .durationMs ));
512+ }
471513 };
472514
473515 @ Override
@@ -493,16 +535,30 @@ else if(seekBar.getId() == R.id.mixingVolBar){
493535 * @param volume: Audio mixing volume. The value ranges between 0 and 100 (default).
494536 */
495537 engine .adjustAudioMixingVolume (progress );
538+ }else if (seekBar .getId () == R .id .mixingProgress ){
539+ String durationText = io .agora .api .example .utils .TextUtils .durationFormat ((long ) progress );
540+ progressText .setText (durationText );
541+ }else if (seekBar .getId () == R .id .slider_speed_fg_audio_file ){
542+ updateSpeedTitle ();
496543 }
544+
497545 }
498546
499547 @ Override
500548 public void onStartTrackingTouch (SeekBar seekBar ) {
501-
549+ // no need to update when user changes
550+ if (seekBar .getId () == R .id .mixingProgress )
551+ handler .removeCallbacksAndMessages (null );
502552 }
503553
504554 @ Override
505555 public void onStopTrackingTouch (SeekBar seekBar ) {
506-
556+ if (seekBar .getId () == R .id .mixingProgress ) {
557+ engine .setAudioMixingPosition (seekBar .getProgress ());
558+ handler .postDelayed (this ::startProgressTimer , 1000 );
559+ } else if (seekBar .getId () == R .id .slider_speed_fg_audio_file ){
560+ if (sliderSpeed .isPressed ())
561+ engine .setAudioMixingPlaybackSpeed (seekBar .getProgress ()+50 );
562+ }
507563 }
508564}
0 commit comments