androidstudio的语音唤醒功能

Manifest配置 写在application之外

  
    
    
    
    
application之内 具体去百度获取

        
        
        

语音唤醒获取获取Wakeup.bin

语音唤醒-百度AI开放平台 (baidu.com)

WakeUpbin放在

同时导入jinLibs在安卓下的包里有

androidstudio的语音唤醒功能_第1张图片

libs的导入 右键加入到Library当中

简单的语音唤醒

public class MainActivity extends AppCompatActivity implements EventListener {
    private EventManager wakeup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initPermission();
        wakeup = EventManagerFactory.create(this, "wp");
        // 基于SDK唤醒词集成1.3 注册输出事件
        wakeup.registerListener(this); //

        start();
    }
    private void start() {
        Map params = new TreeMap();
        params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME, false);
        params.put(SpeechConstant.WP_WORDS_FILE, "assets:///WakeUp.bin");
        String json = null;
        json   = new JSONObject(params).toString();
        wakeup.send(SpeechConstant.WAKEUP_START, json, null, 0, 0);
    }

    protected void onDestroy() {
        super.onDestroy();
        wakeup.send(SpeechConstant.WAKEUP_STOP, "{}", null, 0, 0);
    }

    @Override
    public void onEvent(String name, String params, byte[] bytes, int i, int i1) {
        if ("wp.data".equals(name)) {
            JSONObject json = null;
            String word= null;
            try {
                json = new JSONObject(params);
                word= json.getString("word");
                Toast toast = Toast.makeText(this, word , Toast.LENGTH_SHORT);
                toast.show();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }
    private void initPermission() {
        String[] permissions = {Manifest.permission.RECORD_AUDIO,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.INTERNET,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        };
        ArrayList toApplyList = new ArrayList();

        for (String perm : permissions) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, perm)) {
                toApplyList.add(perm);
            }
        }
        String[] tmpList = new String[toApplyList.size()];
        if (!toApplyList.isEmpty()) {
            ActivityCompat.requestPermissions(this, toApplyList.toArray(tmpList), 123);
        }

    }

}

你可能感兴趣的:(android,java,开发语言)