|
|
@@ -27,15 +27,22 @@ import com.atmob.voiceai.data.consts.ErrorCode;
|
|
|
import com.atmob.voiceai.handlers.EventHandler;
|
|
|
import com.atmob.voiceai.sdk.firebase.FirebaseHelper;
|
|
|
import com.atmob.voiceai.utils.BoxingUtil;
|
|
|
+import com.atmob.voiceai.utils.DownloadUtils;
|
|
|
import com.atmob.voiceai.utils.ThreePair;
|
|
|
+import com.atmob.voiceai.utils.VoiceFileUtil;
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
import javax.inject.Singleton;
|
|
|
|
|
|
+import atmob.okhttp3.OkHttpClient;
|
|
|
+import atmob.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
|
|
import atmob.reactivex.rxjava3.annotations.NonNull;
|
|
|
import atmob.reactivex.rxjava3.core.Single;
|
|
|
import atmob.reactivex.rxjava3.core.SingleObserver;
|
|
|
+import atmob.reactivex.rxjava3.core.SingleSource;
|
|
|
import atmob.reactivex.rxjava3.disposables.Disposable;
|
|
|
+import atmob.reactivex.rxjava3.functions.Function;
|
|
|
+import atmob.reactivex.rxjava3.schedulers.Schedulers;
|
|
|
import atmob.rxjava.utils.RxJavaUtil;
|
|
|
import dagger.hilt.EntryPoint;
|
|
|
import dagger.hilt.InstallIn;
|
|
|
@@ -51,6 +58,7 @@ public class VoiceAIRepository {
|
|
|
private final MemberRepository memberRepository;
|
|
|
|
|
|
private final SingleLiveEvent<?> cloneExecuteSuccess = new SingleLiveEvent<>();
|
|
|
+ private final OkHttpClient okHttpClient;
|
|
|
private boolean textToSpeechRequest;
|
|
|
private final MutableLiveData<Integer> adFreeGenerateNumber = new MutableLiveData<>();
|
|
|
|
|
|
@@ -73,9 +81,10 @@ public class VoiceAIRepository {
|
|
|
}
|
|
|
|
|
|
@Inject
|
|
|
- public VoiceAIRepository(AtmobApi atmobApi, MemberRepository memberRepository, HistoryRepository historyRepository) {
|
|
|
+ public VoiceAIRepository(AtmobApi atmobApi, MemberRepository memberRepository, HistoryRepository historyRepository, OkHttpClient okHttpClient) {
|
|
|
this.atmobApi = atmobApi;
|
|
|
this.memberRepository = memberRepository;
|
|
|
+ this.okHttpClient = okHttpClient;
|
|
|
this.historyRepository = historyRepository;
|
|
|
}
|
|
|
|
|
|
@@ -178,33 +187,43 @@ public class VoiceAIRepository {
|
|
|
// userVoiceBean.setVoiceUrl("http://cdn.atmob.com/upload/project_voice/text_to_speech/20240510/986334800900366336.mp3");
|
|
|
// textToSpeechState.setValue(new ThreePair<>(TextToSpeechState.GENERATED, null, userVoiceBean));
|
|
|
// /********仅测试使用******/
|
|
|
- textToSpeech(bean.getId(), bean.getVoiceType(), content).subscribe(new SingleObserver<TextToSpeechResponse>() {
|
|
|
- @Override
|
|
|
- public void onSubscribe(@NonNull Disposable d) {
|
|
|
- textToSpeechRequest = true;
|
|
|
- textToSpeechState.setValue(new ThreePair<>(TextToSpeechState.GENERATING, null, null));
|
|
|
- }
|
|
|
+ textToSpeech(bean.getId(), bean.getVoiceType(), content)
|
|
|
+ .flatMap((Function<TextToSpeechResponse, SingleSource<UserVoiceBean>>) textToSpeechResponse -> {
|
|
|
+ UserVoiceBean userVoice = textToSpeechResponse.getUserVoice();
|
|
|
+ if (userVoice == null || userVoice.getVoiceUrl() == null) {
|
|
|
+ throw new Exception("cloneVoice is null");
|
|
|
+ }
|
|
|
+ return VoiceFileUtil.getVoiceFile(okHttpClient, userVoice.getVoiceUrl())
|
|
|
+ .map(file -> userVoice);
|
|
|
+ })
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
+ .subscribe(new SingleObserver<UserVoiceBean>() {
|
|
|
+ @Override
|
|
|
+ public void onSubscribe(@NonNull Disposable d) {
|
|
|
+ textToSpeechRequest = true;
|
|
|
+ textToSpeechState.setValue(new ThreePair<>(TextToSpeechState.GENERATING, null, null));
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void onSuccess(@NonNull TextToSpeechResponse textToSpeechResponse) {
|
|
|
- textToSpeechRequest = false;
|
|
|
- textToSpeechState.setValue(new ThreePair<>(TextToSpeechState.GENERATED, null, textToSpeechResponse.getUserVoice()));
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public void onSuccess(@NonNull UserVoiceBean userVoiceBean) {
|
|
|
+ textToSpeechRequest = false;
|
|
|
+ textToSpeechState.setValue(new ThreePair<>(TextToSpeechState.GENERATED, null, userVoiceBean));
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void onError(@NonNull Throwable throwable) {
|
|
|
- textToSpeechRequest = false;
|
|
|
- if (throwable instanceof RxHttpHandler.ServerErrorException) {
|
|
|
- RxHttpHandler.ServerErrorException serverErrorException = (RxHttpHandler.ServerErrorException) throwable;
|
|
|
- textToSpeechState.setValue(new ThreePair<>(TextToSpeechState.ERROR, serverErrorException.getMsg(), null));
|
|
|
- if (serverErrorException.getCode() == ErrorCode.ERROR_NOT_SUBSCRIBED) {
|
|
|
- memberRepository.refreshUserData();
|
|
|
+ @Override
|
|
|
+ public void onError(@NonNull Throwable throwable) {
|
|
|
+ textToSpeechRequest = false;
|
|
|
+ if (throwable instanceof RxHttpHandler.ServerErrorException) {
|
|
|
+ RxHttpHandler.ServerErrorException serverErrorException = (RxHttpHandler.ServerErrorException) throwable;
|
|
|
+ textToSpeechState.setValue(new ThreePair<>(TextToSpeechState.ERROR, serverErrorException.getMsg(), null));
|
|
|
+ if (serverErrorException.getCode() == ErrorCode.ERROR_NOT_SUBSCRIBED) {
|
|
|
+ memberRepository.refreshUserData();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ textToSpeechState.setValue(new ThreePair<>(TextToSpeechState.ERROR, ContextUtil.getContext().getString(R.string.generate_error), null));
|
|
|
+ }
|
|
|
}
|
|
|
- } else {
|
|
|
- textToSpeechState.setValue(new ThreePair<>(TextToSpeechState.ERROR, ContextUtil.getContext().getString(R.string.generate_error), null));
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
private Single<TextToSpeechResponse> textToSpeech(int id, int voiceType, String content) {
|