软件杯语音

一、Android自带的语音播报

 

1.查看是否支持中文,在测试的设备中打开‘设置’ -->找到 '语言和输入法'-->查看语音选项,是否支持中文,默认仅支持英文.

使用如下: 

复制代码
public class AndroidTTSActivity extends AppCompatActivity implements View.OnClickListener {


    private TextToSpeech textToSpeech = null;//创建自带语音对象

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.android_tts_layout);
        findViewById(R.id.btn0).setOnClickListener(this);
        initTTS();
    }

    private void initTTS() {
        //实例化自带语音对象
        textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == textToSpeech.SUCCESS) {
                    // Toast.makeText(MainActivity.this,"成功输出语音",
                    // Toast.LENGTH_SHORT).show();
                    // Locale loc1=new Locale("us");
                    // Locale loc2=new Locale("china");

                    textToSpeech.setPitch(1.0f);//方法用来控制音调
                    textToSpeech.setSpeechRate(1.0f);//用来控制语速

                    //判断是否支持下面两种语言
                    int result1 = textToSpeech.setLanguage(Locale.US);
                    int result2 = textToSpeech.setLanguage(Locale.
                            SIMPLIFIED_CHINESE);
                    boolean a = (result1 == TextToSpeech.LANG_MISSING_DATA || result1 == TextToSpeech.LANG_NOT_SUPPORTED);
                    boolean b = (result2 == TextToSpeech.LANG_MISSING_DATA || result2 == TextToSpeech.LANG_NOT_SUPPORTED);

                    Log.i("zhh_tts", "US支持否?--》" + a +
                            "\nzh-CN支持否》--》" + b);

                } else {
                    Toast.makeText(AndroidTTSActivity.this, "数据丢失或不支持", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn0) {
            startAuto("big sea");
        }

    }

    private void startAuto(String data) {
        // 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
        textToSpeech.setPitch(1.0f);
        // 设置语速
        textToSpeech.setSpeechRate(0.3f);
        textToSpeech.speak(data,//输入中文,若不支持的设备则不会读出来
                TextToSpeech.QUEUE_FLUSH, null);

    }

    @Override
    protected void onStop() {
        super.onStop();
        textToSpeech.stop(); // 不管是否正在朗读TTS都被打断
        textToSpeech.shutdown(); // 关闭,释放资源
    }


}
复制代码

 

二、讯飞语音播报封装(直接用)

1.接入项目前准备:

1.申请APPID(步骤):

I.登录讯飞官网-->创建应用--》创建完成在''我的应用"中即可看见自己新建的项目&APPID-->

 

II.添加需要开通的服务:这里选择在线语音合成+sdk下载(so+jar文件),注意:so文件必须用你对应的项目的,用别人so文件,会导致与你的APPID不匹配,

 

2.使用说明+接入高频易发问题:

  • 语音次数是有限制的,提高次数需要实名认证+上传项目
  • 引入的so文件必须是你项目所对应的
  • 不可多次初始化合成对象

3.接入项目(AndroidStudio):

 

 I.相关sdk文件引入,如图(再次说明:so文件用的是你新建项目的so文件,不要用他人so):

 

II.初始化语音播报(API>=23需要授权,所以先授权,再初始化,如下:)

复制代码
public class StartActivity extends AppCompatActivity {


    private List<String> permissionList = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SystemClock.sleep(1000);//延时加载
        requestPermissions();
    }

    private void openActivity(Class<? extends AppCompatActivity> clazz) {
        initTTS();
        startActivity(new Intent(this, clazz));
        finish();
    }

    //权限申请
    private void requestPermissions() {
        // 版本判断。当手机系统大于 23 时,才有必要去判断权限是否获取
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            addListPermission();
            boolean isGranted = false;//是否全部授权
            // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
            Iterator<String> iterator = permissionList.iterator();
            while (iterator.hasNext()) {
                // 检查该权限是否已经获取
                int granted = ContextCompat.checkSelfPermission(this, iterator.next());
                if (granted == PackageManager.PERMISSION_GRANTED) {
                    iterator.remove();//已授权则remove
                }
            }
            if (permissionList.size() > 0) {
                // 如果没有授予该权限,就去提示用户请求
                //将List转为数组
                String[] permissions = permissionList.toArray(new String[permissionList.size()]);
                // 开始提交请求权限
                ActivityCompat.requestPermissions(this, permissions, 0x10);
            } else {
                Log.i("zhh", "权限已申请");
                openActivity(MainActivity.class);
            }

        } else {
            openActivity(MainActivity.class);
        }
    }

    //初始化语音合成
    private void initTTS() {
        //讯飞语音播报平台
        SpeechUtility.createUtility(this, "appid=");//=号后面写自己应用的APPID
        Setting.setShowLog(true); //设置日志开关(默认为true),设置成false时关闭语音云SDK日志打印
        TTSUtils.getInstance().init(); //初始化工具类
    }


    /**
     * 权限申请返回结果
     *
     * @param requestCode  请求码
     * @param permissions  权限数组
     * @param grantResults 申请结果数组,里面都是int类型的数
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 0x10:
                if(grantResults.length>0&&ifGrantResult(grantResults)){
                    Toast.makeText(this, "同意权限申请", Toast.LENGTH_SHORT).show();
                    openActivity(MainActivity.class);
                }else{
                    Toast.makeText(this, "权限被拒绝了", Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            default:
                break;
        }

    }

    private boolean ifGrantResult(int[] grants) {
        boolean isGrant = true;
        for (int grant : grants) {
            if (grant == PackageManager.PERMISSION_DENIED) {
                isGrant = false;
                break;
            }
        }
        return isGrant;
    }


    //敏感权限添加
    private void addListPermission() {
        if (null == permissionList) {
            permissionList = new ArrayList<>();
            permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            permissionList.add(Manifest.permission.READ_PHONE_STATE);
            permissionList.add(Manifest.permission.RECORD_AUDIO);
        }
    }

}
复制代码

 

III.语音播报封装(部分代码)

复制代码
public class TTSUtils implements InitListener, SynthesizerListener {

    private static volatile TTSUtils instance = null;
    private boolean isInitSuccess = false;
    private SpeechSynthesizer mTts;

    //单例模式
    public static TTSUtils getInstance() {
        if (instance == null) {
            synchronized (TTSUtils.class) {
                if (instance == null) {
                    instance = new TTSUtils();
                }
            }
        }
        return instance;
    }

    public TTSUtils() {

    }

    // 初始化合成对象
    public void init() {
        //判断进程是否已启动,初始化多次会报错
        //个人遇到问题:极光推送引入后,不加该条件回报错
        if (CourseUtils.resultProcess("com.zhanghai.ttsapp")) {

            mTts = SpeechSynthesizer.createSynthesizer(App.getContext(), this);
            // 清空参数
            mTts.setParameter(SpeechConstant.PARAMS, null);
            // 设置在线云端
            mTts.setParameter(SpeechConstant.ENGINE_TYPE,
                    SpeechConstant.TYPE_CLOUD);

            // 设置发音人--发音人选择--具体见values-string
            mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoqi");

            // 设置发音语速
            mTts.setParameter(SpeechConstant.SPEED, "50");
            // 设置音调
            mTts.setParameter(SpeechConstant.PITCH, "50");
            // 设置合成音量
            mTts.setParameter(SpeechConstant.VOLUME, "100");
            // 设置播放器音频流类型
            mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");
            // 设置播放合成音频打断音乐播放,默认为true
            mTts.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "true");
            // 设置音频保存路径,需要申请WRITE_EXTERNAL_STORAGE权限,如不需保存注释该行代码
//        mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH,"./sdcard/iflytek.pcm");
            Log.i("zhh", "--初始化成完成-");
        }

    }

    //开始合成
    public void speak(String msg) {
        if (isInitSuccess) {
            if (mTts.isSpeaking()) {
                stop();
            }
            mTts.startSpeaking(msg, this);
        } else {
            init();
        }
    }
}
复制代码

 

 IV:调用实例

复制代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    private EditText et = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et = findViewById(R.id.et);
        findViewById(R.id.btn0).setOnClickListener(this);
        findViewById(R.id.btn1).setOnClickListener(this);
        findViewById(R.id.btn2).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn0:
                TTSUtils.getInstance().speak("bigsea是大海");
                break;
            case R.id.btn1:
                String msg = et.getText().toString();
                TTSUtils.getInstance().speak(TextUtils.isEmpty(msg) ? "输入信息为空" : msg);
                break;
            case R.id.btn2:
                startActivity(new Intent(this, AndroidTTSActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        //移动数据统计分析--不用可不用加入
        FlowerCollector.onResume(MainActivity.this);
        FlowerCollector.onPageStart(TAG);
        super.onResume();
    }

    @Override
    protected void onPause() {
        //移动数据统计分析
        FlowerCollector.onPageEnd(TAG);
        FlowerCollector.onPause(MainActivity.this);
        super.onPause();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        TTSUtils.getInstance().release();//释放资源
    }

}
复制代码
posted @ 2018-06-13 12:32  (谢谢)  阅读(210)  评论(0编辑  收藏  举报