百度语音生成AndroidSDK在线功能简单使用

折腾了半小时,百度语音生成SDK的简单使用,只能使用网络播放,离线出现BUg未修复,

public class SayActivity extends AppCompatActivity {
    String AppId = "146***70";
    String AppKey = "shg****ld";
    String AppSecret = "YY*****WfN2xS";
    private static final String TAG = "MiniActivity";
    private SpeechSynthesizer mSpeechSynthesizer;

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

        initPermission();//权限检测

        mSpeechSynthesizer = SpeechSynthesizer.getInstance();
        mSpeechSynthesizer.setContext(this); // this 是Context的之类,如Activity

        mSpeechSynthesizer.setAppId(AppId/*这里只是为了让Demo运行使用的APPID,请替换成自己的id。*/);
        mSpeechSynthesizer.setApiKey(AppKey, AppSecret/*这里只是为了让Demo正常运行使用APIKey,请替换成自己的APIKey*/);
        mSpeechSynthesizer.auth(TtsMode.ONLINE);  // 纯在线
        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "4"); // 设置发声的人声音,在线生效
        mSpeechSynthesizer.initTts(TtsMode.ONLINE); // 初始化离在线混合模式,如果只需要在线合成功能,使用 TtsMode.ONLINE
        mSpeechSynthesizer.setSpeechSynthesizerListener(new SpeechSynthesizerListener() {....自己补上去...博客看着太长...省略若干字....}); //listener是SpeechSynthesizerListener 的实现类,需要实现您自己的业务逻辑。SDK合成后会对这个类的方法进行回调。

        LoggerProxy.printable(true);
        findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "0"); // 设置发声的人声音,在线生效
                TextInputEditText ed = (TextInputEditText) findViewById(R.id.ed);
                mSpeechSynthesizer.speak(ed.getText().toString());
            }
        });
        findViewById(R.id.bt2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "3"); // 设置发声的人声音,在线生效
                List bags = new ArrayList();
                bags.add(getSpeechSynthesizeBag("123456", "0"));
                bags.add(getSpeechSynthesizeBag("你好", "1"));
                bags.add(getSpeechSynthesizeBag("使用百度语音合成SDK", "2"));
                bags.add(getSpeechSynthesizeBag("hello", "3"));
                bags.add(getSpeechSynthesizeBag("这是一个demo工程", "4"));
                int result = mSpeechSynthesizer.batchSpeak(bags);

            }
        });

    }

    private SpeechSynthesizeBag getSpeechSynthesizeBag(String text, String utteranceId) {
        SpeechSynthesizeBag speechSynthesizeBag = new SpeechSynthesizeBag();
        //需要合成的文本text的长度不能超过1024个GBK字节。
        speechSynthesizeBag.setText(text);
        speechSynthesizeBag.setUtteranceId(utteranceId);
        return speechSynthesizeBag;
    }


    /**
     * android 6.0 以上需要动态申请权限
     */
    private void initPermission() {
        String permissions[] = {
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.MODIFY_AUDIO_SETTINGS,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_SETTINGS,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.CHANGE_WIFI_STATE
        };

        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);
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        // 此处为android 6.0以上动态授权的回调,用户自行实现。
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSpeechSynthesizer.release();
    }

}

 

 

需要权限:








关于导入官方有介绍,下载后放入jniLIb中跟Assets中 ,直接复制拷贝

app配置中添加一句话自动生成jnilib目录: 

sourceSets.main.jniLibs.srcDirs = ['libs']

 

你可能感兴趣的:(百度语音生成AndroidSDK在线功能简单使用)