|
|
@@ -2,15 +2,22 @@ package com.atmob.voiceai.module.voiceai;
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
import android.graphics.Rect;
|
|
|
+import android.net.Uri;
|
|
|
import android.os.Bundle;
|
|
|
import android.view.View;
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
import androidx.annotation.Nullable;
|
|
|
+import androidx.media3.common.MediaItem;
|
|
|
+import androidx.media3.common.PlaybackException;
|
|
|
+import androidx.media3.common.Player;
|
|
|
+import androidx.media3.exoplayer.ExoPlaybackException;
|
|
|
+import androidx.media3.exoplayer.ExoPlayer;
|
|
|
import androidx.recyclerview.widget.GridLayoutManager;
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
import com.atmob.app.lib.base.BaseFragment;
|
|
|
+import com.atmob.common.logging.AtmobLog;
|
|
|
import com.atmob.voiceai.R;
|
|
|
import com.atmob.voiceai.data.api.bean.TypeListBean;
|
|
|
import com.atmob.voiceai.data.api.bean.VoiceListBean;
|
|
|
@@ -19,6 +26,7 @@ import com.atmob.voiceai.databinding.ItemVoiceAiTabBinding;
|
|
|
import com.atmob.voiceai.module.clonevoice.CloneVoiceFragment;
|
|
|
import com.atmob.voiceai.module.main.MainActivity;
|
|
|
import com.atmob.voiceai.utils.GridLayoutItemDecoration;
|
|
|
+import com.atmob.voiceai.utils.ToastUtil;
|
|
|
import com.google.android.material.tabs.TabLayout;
|
|
|
import com.gyf.immersionbar.ImmersionBar;
|
|
|
|
|
|
@@ -34,6 +42,8 @@ public class VoiceAIFragment extends BaseFragment<FragmentVoiceAiBinding> {
|
|
|
private VoiceAIViewModel voiceViewModel;
|
|
|
private VoiceAIListAdapter voiceAIListAdapter;
|
|
|
|
|
|
+ private ExoPlayer player;
|
|
|
+
|
|
|
@Override
|
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
|
super.onViewCreated(view, savedInstanceState);
|
|
|
@@ -64,6 +74,7 @@ public class VoiceAIFragment extends BaseFragment<FragmentVoiceAiBinding> {
|
|
|
binding.voiceTab.addTab(tab);
|
|
|
itemBinding.getRoot().setOnClickListener(v -> {
|
|
|
tab.select();
|
|
|
+
|
|
|
voiceViewModel.refreshVoiceList(typeListBean.getId());
|
|
|
});
|
|
|
if (i == 0) {
|
|
|
@@ -74,10 +85,15 @@ public class VoiceAIFragment extends BaseFragment<FragmentVoiceAiBinding> {
|
|
|
}
|
|
|
|
|
|
private void initView() {
|
|
|
+ initExoPlayer();
|
|
|
initTabLayout();
|
|
|
initVoiceList();
|
|
|
}
|
|
|
|
|
|
+ private void initExoPlayer() {
|
|
|
+ player = new ExoPlayer.Builder(requireContext()).build();
|
|
|
+ }
|
|
|
+
|
|
|
private void initVoiceList() {
|
|
|
voiceAIListAdapter = new VoiceAIListAdapter(getViewLifecycleOwner());
|
|
|
voiceAIListAdapter.setActionHandler(new VoiceAIListAdapter.ActionHandler() {
|
|
|
@@ -87,8 +103,9 @@ public class VoiceAIFragment extends BaseFragment<FragmentVoiceAiBinding> {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void choiceItemClick(VoiceListBean voiceListBean) {
|
|
|
+ public void choiceItemClick(@NonNull VoiceListBean voiceListBean) {
|
|
|
voiceViewModel.setChoiceVoiceBean(voiceListBean);
|
|
|
+ playExampleVoice(voiceListBean);
|
|
|
}
|
|
|
});
|
|
|
binding.ryVoiceView.setAdapter(voiceAIListAdapter);
|
|
|
@@ -107,6 +124,35 @@ public class VoiceAIFragment extends BaseFragment<FragmentVoiceAiBinding> {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ private void playExampleVoice(@NonNull VoiceListBean voiceListBean) {
|
|
|
+ if (player.isPlaying() || player.isLoading()) {
|
|
|
+ player.stop();
|
|
|
+ }
|
|
|
+ Uri videoUri = Uri.parse(voiceListBean.getVoiceUrl());
|
|
|
+ MediaItem mediaItem = MediaItem.fromUri(videoUri);
|
|
|
+ player.setMediaItem(mediaItem);
|
|
|
+ player.prepare();
|
|
|
+ player.setPlayWhenReady(true);
|
|
|
+ voiceListBean.setVoicePlayState(VoiceListBean.VoicePlayState.LOADING);
|
|
|
+ player.addListener(new Player.Listener() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPlayerError(PlaybackException error) {
|
|
|
+ AtmobLog.d("zk", "onPlayerError: " + error.getMessage());
|
|
|
+ voiceListBean.setVoicePlayState(VoiceListBean.VoicePlayState.INITIAL);
|
|
|
+ ToastUtil.show(R.string.voice_play_error, ToastUtil.LENGTH_SHORT);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPlaybackStateChanged(int state) {
|
|
|
+ AtmobLog.d("zk", "onPlaybackStateChanged: " + state + " " + voiceListBean.getName() + " " + voiceListBean.getVoiceUrl());
|
|
|
+ if (Player.STATE_ENDED == state) {
|
|
|
+ voiceViewModel.setVoicePlayEnd();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
private void initTabLayout() {
|
|
|
binding.voiceTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
|
|
|
@SuppressLint("UseCompatLoadingForDrawables")
|
|
|
@@ -165,4 +211,14 @@ public class VoiceAIFragment extends BaseFragment<FragmentVoiceAiBinding> {
|
|
|
super.configImmersion(immersionBar);
|
|
|
immersionBar.statusBarDarkFont(false);
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ if (player != null) {
|
|
|
+ player.stop();
|
|
|
+ player.release();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|