Skip to content

Commit

Permalink
update text view on handle
Browse files Browse the repository at this point in the history
  • Loading branch information
litongjava committed Oct 24, 2023
1 parent 644c2d5 commit 0743926
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;

Expand All @@ -15,6 +17,7 @@
import com.litongjava.android.view.inject.annotation.OnClick;
import com.litongjava.android.view.inject.utils.ViewInjectUtils;
import com.litongjava.jfinal.aop.Aop;
import com.litongjava.jfinal.aop.AopManager;
import com.litongjava.whisper.android.java.services.WhisperService;
import com.litongjava.whisper.android.java.task.LoadModelTask;
import com.litongjava.whisper.android.java.task.TranscriptionTask;
Expand Down Expand Up @@ -42,15 +45,20 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
ViewInjectUtils.injectActivity(this, this);
initAopBean();
showSystemInfo();
}

private void initAopBean() {
Handler mainHandler = new Handler(Looper.getMainLooper());
AopManager.me().addSingletonObject(mainHandler);
}

@RequiresApi(api = Build.VERSION_CODES.O)
@OnClick(R.id.loadModelBtn)
public void loadModelBtn_OnClick(View v) {
Context context = getBaseContext();
ThreadUtils.executeByIo(new LoadModelTask(context,tv));

ThreadUtils.executeByIo(new LoadModelTask(tv));
}

@OnClick(R.id.transcriptSampleBtn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.litongjava.android.utils.dialog.AlertDialogUtils;
import com.litongjava.jfinal.aop.Aop;
import com.litongjava.whisper.android.java.bean.WhisperSegment;
import com.litongjava.whisper.android.java.single.LocalWhisper;
import com.litongjava.whisper.android.java.utils.WaveEncoder;
Expand All @@ -26,7 +30,7 @@ public class WhisperService {
private final Object lock = new Object();

@RequiresApi(api = Build.VERSION_CODES.O)
public void loadModel(Context context, TextView tv) {
public void loadModel(TextView tv) {
String modelFilePath = LocalWhisper.modelFilePath;
String msg = "load model from :" + modelFilePath + "\n";
outputMsg(tv, msg);
Expand Down Expand Up @@ -71,7 +75,7 @@ public void transcribeSample(TextView tv, File sampleFile) {
end = System.currentTimeMillis();
if(transcription!=null){
ToastUtils.showLong(transcription.toString());

AlertDialogUtils.showWaringDialog(Utils.getApp(),msg);
msg = "Transcript successful:" + (end - start) + "ms";
outputMsg(tv, msg);

Expand All @@ -86,11 +90,10 @@ public void transcribeSample(TextView tv, File sampleFile) {
}

private void outputMsg(TextView tv, String msg) {
log.info(msg);
if(tv!=null){
tv.append(msg + "\n");
Aop.get(Handler.class).post(()->{ tv.append(msg + "\n");});
}
log.info(msg);

}

@RequiresApi(api = Build.VERSION_CODES.O)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import android.app.Application;
import android.os.Build;
import android.os.Handler;

import androidx.annotation.RequiresApi;

import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.litongjava.jfinal.aop.Aop;
import com.litongjava.whisper.android.java.bean.WhisperSegment;
import com.litongjava.whisper.android.java.utils.AssetUtils;
import com.whispercpp.java.whisper.WhisperContext;
Expand All @@ -32,11 +35,27 @@ public enum LocalWhisper {
}

public synchronized String transcribeData(float[] data) throws ExecutionException, InterruptedException {
return whisperContext.transcribeData(data);
if(whisperContext==null){
toastModelLoading();
return null;
}else{
return whisperContext.transcribeData(data);
}
}

public List<WhisperSegment> transcribeDataWithTime(float[] audioData) throws ExecutionException, InterruptedException {
return whisperContext.transcribeDataWithTime(audioData);
private static void toastModelLoading() {
Aop.get(Handler.class).post(()->{
ToastUtils.showShort("please wait for model loading");
});
}

public List<WhisperSegment> transcribeDataWithTime(float[] audioData) throws ExecutionException, InterruptedException {
if(whisperContext==null){
toastModelLoading();
return null;
}else{
return whisperContext.transcribeDataWithTime(audioData);
}
}

public void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.widget.TextView;

import com.blankj.utilcode.util.ThreadUtils;
Expand All @@ -11,19 +12,20 @@
import java.io.File;

public class LoadModelTask extends ThreadUtils.Task<Object> {
private final Context context;
private final TextView tv;
public LoadModelTask(Context context,TextView tv) {
public LoadModelTask(TextView tv) {
this.tv = tv;
this.context=context;
}

@Override
public Object doInBackground() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Aop.get(WhisperService.class).loadModel(context,tv);
Aop.get(WhisperService.class).loadModel(tv);
}else{
tv.append("not supported android devices");
Aop.get(Handler.class).post(()->{
tv.append("not supported android devices");
});

}
return null;
}
Expand Down

0 comments on commit 0743926

Please sign in to comment.