Android 悬浮窗语音识别功能开发详解

笔者是一个普通不能再普通的程序员,本着出处兴趣,花时间研究了一下,想实现手机的悬浮窗语音识别功能,这样不影响自己其它操作的,语音识别技术是用百度云语音sdk,应该不难实现,很难实现就是核心语音识别技术了,这是大数据上运用来的没错,有空再去研究一下大数据。

切入正题,先弄一个页面activity_main.xml,布局如下图,需要一个按钮用来开启或关闭悬浮框,其中的输入框是输入密码用的,类似解锁使用功能

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Test" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Test Voice" />
    </LinearLayout>

    <TextView
        android:id="@+id/showState"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="TextView" />

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/showState">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/showMsg"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Hello World!" />

            <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPassword" />
        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

接下来,就是MainActivity.class页面的交互逻辑实现,贴上代码

package com.example.voiceapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Handler mHandler = null;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mHandler = new Handler();

        this.editText = (EditText) findViewById(R.id.editText);

        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                mHandler.postDelayed(new Runnable() {

                    @Override
                    public void run() {

//                        create2();
                    }
                }, 1000 * 3);

            }
        });

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String pwd = editText.getText().toString();
                if (!pwd.equals("wx zs1026")) {
                    Toast.makeText(MainActivity.this, "密码错误!", Toast.LENGTH_SHORT).show();
                    return;
                }

                //TODO: 此方案在模拟器上正常,在真机上会闪退
                //create3();

                //TODO: 此方案比上个好多了,可以提示获取权限
                create4();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        WindowUtils.hidePopupWindow();
    }


    /**
     * 详解Android 全局弹出对话框SYSTEM_ALERT_WINDOW权限
     * 发布时:2019/7/7 22:08:19
     * http://www.zyiz.net/tech/detail-63251.html
     * https://www.cnblogs.com/mengdd/p/3824782.html
     * */
    void create3() {
        WindowUtils.showPopupWindow(MainActivity.this);
    }
    
    /**
     *  Android 8.0完美适配全局dialog 悬浮窗弹出
     *  https://www.jianshu.com/p/78953f3c07d5
     * */
    void create4() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.canDrawOverlays(MainActivity.this)) {
                //TODO: 没有说明MainService.class  具体实现方式,据说是 Service 上 创建 一个全局dialog?
//                Intent intent = new Intent(MainActivity.this, MainService.class);
//                startService(intent);
                create3();
//                finish();
            } else {
                //若没有权限,提示获取.
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                Toast.makeText(MainActivity.this,"需要取得权限以使用悬浮窗",Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }

        }else {
            //SDK在23以下,不用管.
//            Intent intent = new Intent(MainActivity.this, MainService.class);
//            startService(intent);
            create3();
//            finish();
        }

    }
}

然后,弄一个悬浮框的页面popupwindow.xml,布局如下图,只有一个标题和展示消息的,精简就好,不需要添加过多,因为悬浮框不能过大,过大容易遮挡后面的操作的

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_gravity="center"
    android:alpha="0.7"
    android:background="#0E0E0E"
    android:clickable="false"
    android:gravity="center"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/popup_window"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/footer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingLeft="@dimen/dialog_content_padding_side"
                android:paddingRight="@dimen/dialog_content_padding_side"
                android:paddingBottom="@dimen/dialog_content_padding_bottom">


                <TextView
                    android:id="@+id/title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/default_title"
                    android:textColor="@color/dialog_title_text_color"
                    android:textSize="@dimen/dialog_title_text_size" />

            </LinearLayout>

            <View
                android:id="@+id/title_divider"
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@color/black" />
        </LinearLayout>

        <ScrollView
            android:id="@+id/miniScrolView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/header">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/content"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="@dimen/dialog_content_padding_side"
                    android:text="@string/default_content"
                    android:textColor="@color/dialog_content_text_color"
                    android:textSize="@dimen/dialog_content_text_size" />
            </LinearLayout>
        </ScrollView>

    </RelativeLayout>

</FrameLayout>

接着,实现悬浮框的逻辑,文件是WindowUtils.class,这个就稍微复杂一点儿,贴上代码,这个只要悬浮框一出现,就会自动开启语音识别对吧,一旦关闭,语音识别也就关闭了,需要的就是长语音识别功能

package com.example.voiceapplication;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.ScrollView;
import android.widget.TextView;

public class WindowUtils {
    private static final String LOG_TAG = "WindowUtils";
    private static View mView = null;
    private static WindowManager mWindowManager = null;
    private static Context mContext = null;
    public static Boolean isShown = false;
    private static VoiceMode voice = null;

    /**
     * 显示弹出框
     *
     * @param context
     */
    public static void showPopupWindow(final Context context) {
        if (isShown) {
            LogUtil.i(LOG_TAG, "return cause already shown");
            hidePopupWindow();
            return;
        }

        isShown = true;
        LogUtil.i(LOG_TAG, "showPopupWindow");

        // 获取应用的Context
        mContext = context.getApplicationContext();

        voice = new VoiceMode(mContext, new VoiceMode.Listener() {
            @Override
            public void state(String text) {
                updateTitle(text);
            }

            @Override
            public void append(String text) {

            }

            @Override
            public void update(String text) {
                updateContent(text,true);
            }


        });

        // 获取WindowManager
        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);

        mView = setUpView(context);

        final WindowManager.LayoutParams params = new WindowManager.LayoutParams();

        // 类型
//        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        //设置弹出全局对话框,但是这句话并不能解决在android的其他手机上能弹出来(例如用户华为p10 就无法弹框)
        // WindowManager.LayoutParams.TYPE_SYSTEM_ALERT

        //只有这样才能弹框
        if (Build.VERSION.SDK_INT>=26) {//8.0新特性
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }else{
            params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }

        // 设置flag
//        int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
//        int flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        int flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;

        // FLAG_NOT_TOUCH_MODAL不阻塞事件传递到后面的窗口
        // 设置 FLAG_NOT_FOCUSABLE 悬浮窗口较小时,后面的应用图标由不可长按变为可长按
        // 不设置这个flag的话,home页的划屏会有问题
        // 如果设置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,弹出的View收不到Back键的事件
        params.flags = flags;
        // 不设置这个弹出框的透明遮罩显示为黑色
        params.format = PixelFormat.TRANSLUCENT;

        Point p = new Point();
        //获取窗口管理器
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getSize(p);
        int screenWidth = p.x; // 屏幕宽度
        int screenHeight = p.y;
        params.width = (int) Math.round(screenWidth*0.5);
        params.height = (int) Math.round(screenHeight*0.4);

//        params.width = WindowManager.LayoutParams.MATCH_PARENT;
//        params.height = WindowManager.LayoutParams.MATCH_PARENT;

//        params.x = 10;
//        params.y = 10;

        params.gravity = Gravity.TOP;

        mWindowManager.addView(mView, params);

        LogUtil.i(LOG_TAG, "add view");
        voice.start();

    }

    /**
     * 隐藏弹出框
     */
    public static void hidePopupWindow() {
        LogUtil.i(LOG_TAG, "hide " + isShown + ", " + mView);
        if (isShown && null != mView) {
            LogUtil.i(LOG_TAG, "hidePopupWindow");
            mWindowManager.removeView(mView);
            isShown = false;
            voice.destory();
        }

    }

    private static TextView showContent=null;
    private static TextView showTitle=null;

    public static void updateTitle(String title) {
        if (showTitle==null) return;

        showTitle.setText(title);
    }

    public static  void updateContent(String content, Boolean isReset) {
        if (showContent==null) return;

        if (isReset==true) showContent.setText("");

        showContent.append(content);
    }

    private static View setUpView(final Context context) {

        LogUtil.i(LOG_TAG, "setUp view");

        View view = LayoutInflater.from(context).inflate(R.layout.popupwindow, null);

        showContent = (TextView) view.findViewById(R.id.content);
        showTitle = (TextView) view.findViewById(R.id.title);

        final ScrollView scrollView = (ScrollView) view.findViewById(R.id.miniScrolView);

        //scrollview自动滚动到底部,
        //转载 https://blog.csdn.net/weixin_39753616/article/details/117314708?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-0.no_search_link&spm=1001.2101.3001.4242.1
        scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                scrollView.post(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.fullScroll(View.FOCUS_DOWN);
                    }
                });
            }
        });


        // 点击窗口外部区域可消除
        // 这点的实现主要将悬浮窗设置为全屏大小,外层有个透明背景,中间一部分视为内容区域
        // 所以点击内容区域外部视为点击悬浮窗外部
//        final View popupWindowView = view.findViewById(R.id.popup_window);// 非透明的内容区域

        view.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                LogUtil.i(LOG_TAG, "onTouch");
                int x = (int) event.getX();
                int y = (int) event.getY();
                Rect rect = new Rect();
//                popupWindowView.getGlobalVisibleRect(rect);
//                if (!rect.contains(x, y)) {
//                    WindowUtils.hidePopupWindow();
//                }

                LogUtil.i(LOG_TAG, "onTouch : " + x + ", " + y + ", rect: " + rect);
//                ((Activity) mContext).dispatchTouchEvent(event);
                return false;
            }
        });

        return view;

    }
}

还有一个是调用语音识别sdk,上面有个类VoiceMode.class,贴上代码,具体调用方式在里面了

package com.example.voiceapplication;

import android.content.Context;
import android.widget.Toast;
import com.baidu.speech.EventListener;
import com.baidu.speech.EventManager;
import com.baidu.speech.EventManagerFactory;
import com.baidu.speech.asr.SpeechConstant;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

public class VoiceMode {

    private Boolean isWork = false;
    private EventManager asr = null;
    private EventListener listener;
    private Context context;
    private Listener callback;
    int c = 1;
    private String cache  = "";
    private String word = "";

    public interface Listener {
        /**
         * 系统提示:
         * */
        void state(String text);
        void append(String text);
        /**
         * 识别结果
         * */
        void update(String text);
    }

    public VoiceMode(Context context, Listener cb) {
        this.context = context;
        this.callback = cb;

        this.asr = EventManagerFactory.create(context, "asr");

        this.listener = new EventListener()
        {
            @Override
            public void onEvent(String name, String params, byte[] bytes, int i, int i1) {
                String result=null;

                switch (name){
                    case SpeechConstant.CALLBACK_EVENT_ASR_READY:
                    {
                        // 引擎就绪,可以说话,一般在收到此事件后通过UI通知用户可以说话了
                        result = "可以说话了";
                        callback.state(result);
                    }
                    break;
                    case SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL:
                    {
                        // 一句话的临时结果,最终结果及语义结果
                        result = "一句话最终结果";// + params;
                        callback.state(result);
                        try {
                            JSONObject obj = new JSONObject(params);

                            int errCode = obj.getInt("error");

                            if (errCode>0) {
                                String desc = obj.getString("desc");
                                result += "errorCode:" + errCode+ ", desc:" + desc;

                                int subError = obj.getInt("sub_error");
                                if (subError>0) {
                                    result += ", subError:"+subError;
                                }
                                word = result;
                            } else {
                                result = obj.getString("best_result");
                                word = result;
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            word = ">>>有错误!";
//                        return;
                        }

                        callback.update(cache+word);
                    }
                    break;
                    case SpeechConstant.CALLBACK_EVENT_ASR_FINISH:
                    {
                        result = "一句话识别结束";// "\n" + params;
                        callback.state(result);

                        try {
                            JSONObject obj = new JSONObject(params);

                            int errCode = obj.getInt("error");

                            if (errCode>0) {
                                String desc = obj.getString("desc");

                                result += "errorCode:" + errCode+ ", desc:" + desc;

                                int subError = obj.getInt("sub_error");

                                if (subError>0) {
                                    result += ", subError:"+subError;
                                }
                                word = result;
                            }
                            else {
                                if (cache=="") cache += word;
                                else cache = cache +"\n"+word;
                                callback.update(cache);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            result += "\n 有错误!";
                            word = result;
                            callback.update(cache+word);
                        }

                    }
                    break;
                    case SpeechConstant.CALLBACK_EVENT_ASR_EXIT:
                    {
                        result = "识别结束,资源释放";// \n" + params;
                        isWork = false;
                        callback.state(result);
                    }
                    break;
                    case SpeechConstant.CALLBACK_EVENT_ASR_ERROR:
                    {
                        result = "出现错误\n" + params;
                        callback.state(result);
                    }
                    break;
                    default:
                    {
                        // ... 支持的输出事件和事件支持的事件参数见“输入和输出参数”一节
                        result = "输入和输出参数\n"+params;
                        callback.state(result);
                    }

                    if (params!=null) {
//                        LogUtil.i("Result", params);
//{"results_recognition":["哈喽,"],"result_type":"final_result","best_result":"哈喽,","origin_result":{"corpus_no":7037130879383025219,"err_no":0,"result":{"word":["哈喽,"]},"sn":"78ae2481-0a5c-4c18-a8e4-cf42590fe8d5_s-0"},"error":0}
                    }

                }


            }
        };

        asr.registerListener(listener);
    }


    public void start() {
        if (this.asr!=null && this.listener!=null) {} else {
            return;
        }


        if (isWork) {
            asr.send(SpeechConstant.ASR_STOP, null, null, 0, 0);
            //发送停止录音事件,提前结束录音等待识别结果
            isWork = false;

            Toast.makeText(context, "Stop Work.", Toast.LENGTH_SHORT).show();
        } else {

            //Map String,Object 转换成 JSONObject 数据
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("accept-audio-data",false);
            map.put("disable-punctuation",false);
            //1837	四川话
            map.put("pid",1837);
            //开启长语音识别功能,此时VAD参数不能设为touch;长语音可以识别数小时的音频。注意选输入法模型。
            //
            //BDS_ASR_ENABLE_LONG_SPEECH= true 或 VAD_ENDPOINT_TIMEOUT = 0 都可以开启长语音
            //{"enable.long.speech":true,"accept-audio-volume":false}
            //或
            //{"accept-audio-volume":false,"vad.endpoint-timeout":0}
            map.put("accept-audio-volume",false);
            map.put("vad.endpoint-timeout",0);
//                map.put();

            String json = map.toString();

            asr.send(SpeechConstant.ASR_START, json, null, 0, 0);
            isWork = true;

            Toast.makeText(context, "Working...", Toast.LENGTH_SHORT).show();
        }
    }

    public void destory() {

        if (this.asr!=null && this.listener!=null) {
            if (this.isWork) {
                asr.send(SpeechConstant.ASR_CANCEL, null, null, 0, 0); // 取消识别
            }
            asr.unregisterListener(this.listener); //释放资源
            this.listener = null;
            this.asr = null;
        }
    }
}

接着,关键代码是下面这个,调用语音识别SDK的,这里不提供,需要的话去百度云语音识别那下载Android SDK来用,有带说明文档可以看看,有免费试用的

this.asr = EventManagerFactory.create(context, “asr”);

很详细了吧,上面照着做完,编译运行没问题的话,运行效果会如同下图这样的,OK,作业完成了,撒花~

图1,打开效果图2,一直运行
在这里插入图片描述在这里插入图片描述

哦,对了,悬浮窗拖动效果还没有实现,容我偷个懒吧,这里就不讲啦~~,有帮助的话,请给一点赞,鼓励一下哦~
在这里插入图片描述

写到最后,特别感谢以下作者提供解决方案:

  1. android 全局dialog,并且兼容android8.0
  2. 详解Android 全局弹出对话框SYSTEM_ALERT_WINDOW权限
  3. Android悬浮窗实现 使用WindowManager
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TA远方

谢谢!收到你的爱╮(╯▽╰)╭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值