android 百度语音合成例子eclipse版和studio版



package com.example.baiduspeechmydemo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.baidu.speechsynthesizer.SpeechSynthesizer;
import com.baidu.speechsynthesizer.SpeechSynthesizerListener;
import com.baidu.speechsynthesizer.publicutility.DataInfoUtils;
import com.baidu.speechsynthesizer.publicutility.SpeechError;
import com.baidu.speechsynthesizer.publicutility.SpeechLogger;


import android.media.AudioManager;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity  {

 
 protected static final int UI_LOG_TO_VIEW = 0;
    protected static final int UI_TOAST = 1;
    private SpeechSynthesizer speechSynthesizer;
 
    /** 指定license路径,需要保证该路径的可读写权限 */
    private static final String LICENCE_FILE_NAME = Environment.getExternalStorageDirectory()
            + "/tts/bd_etts_speech_female.dat";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  
  System.loadLibrary("gnustl_shared");
        // 部分版本不需要BDSpeechDecoder_V1
        try {
            System.loadLibrary("BDSpeechDecoder_V1");
        } catch (UnsatisfiedLinkError e) {
            SpeechLogger.logD("load BDSpeechDecoder_V1 failed, ignore");
        }
        System.loadLibrary("bd_etts");
        System.loadLibrary("bds");

        if (!new File(LICENCE_FILE_NAME).getParentFile().exists()) {
            new File(LICENCE_FILE_NAME).getParentFile().mkdirs();
        }
        
        // 复制license到指定路径
        InputStream licenseInputStream = getResources().openRawResource(R.raw.temp_license_2015_07_09);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(LICENCE_FILE_NAME);
            byte[] buffer = new byte[1024];
            int size = 0;
            while ((size = licenseInputStream.read(buffer, 0, 1024)) >= 0) {
                SpeechLogger.logD("size written: " + size);
                fos.write(buffer, 0, size);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                licenseInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        speechSynthesizer =
                SpeechSynthesizer.newInstance(SpeechSynthesizer.SYNTHESIZER_AUTO, getApplicationContext(), "holder",
                        new BaiDuVevio());
        // 请替换为语音开发者平台注册应用得到的apikey和secretkey (在线授权)
       // speechSynthesizer.setApiKey("apikey", "secretkey");
        // 请替换为语音开发者平台上注册应用得到的App ID (离线授权)
    //    speechSynthesizer.setAppId("appid");
        // 设置授权文件路径
        speechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_LICENCE_FILE, LICENCE_FILE_NAME);
        // TTS所需的资源文件,可以放在任意可读目录,可以任意改名
        String ttsTextModelFilePath =
                getApplicationContext().getApplicationInfo().dataDir + "/lib/libbd_etts_text.dat.so";
        String ttsSpeechModelFilePath =
                getApplicationContext().getApplicationInfo().dataDir + "/lib/libbd_etts_speech_female.dat.so";
        speechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_TEXT_MODEL_FILE, ttsTextModelFilePath);
        speechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_SPEECH_MODEL_FILE, ttsSpeechModelFilePath);
        DataInfoUtils.verifyDataFile(ttsTextModelFilePath);
        DataInfoUtils.getDataFileParam(ttsTextModelFilePath, DataInfoUtils.TTS_DATA_PARAM_DATE);
        DataInfoUtils.getDataFileParam(ttsTextModelFilePath, DataInfoUtils.TTS_DATA_PARAM_SPEAKER);
        DataInfoUtils.getDataFileParam(ttsTextModelFilePath, DataInfoUtils.TTS_DATA_PARAM_GENDER);
        DataInfoUtils.getDataFileParam(ttsTextModelFilePath, DataInfoUtils.TTS_DATA_PARAM_CATEGORY);
        DataInfoUtils.getDataFileParam(ttsTextModelFilePath, DataInfoUtils.TTS_DATA_PARAM_LANGUAGE);
        speechSynthesizer.initEngine();
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
       
        int ret = speechSynthesizer.speak("百度几下才知道天下");
        if (ret != 0) {
         //   logError("开始合成器失败:" + errorCodeAndDescription(ret));
        } else {
           // logDebug("开始工作,请等待数据...");
         Log.i("MainAcitivity", "开始缓冲");
        }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }


 
 class BaiDuVevio implements SpeechSynthesizerListener {

  @Override
  public void onBufferProgressChanged(SpeechSynthesizer arg0, int arg1) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onCancel(SpeechSynthesizer arg0) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onError(SpeechSynthesizer arg0, SpeechError arg1) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onNewDataArrive(SpeechSynthesizer arg0, byte[] arg1,
    boolean arg2) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onSpeechFinish(SpeechSynthesizer arg0) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onSpeechPause(SpeechSynthesizer arg0) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onSpeechProgressChanged(SpeechSynthesizer arg0, int arg1) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onSpeechResume(SpeechSynthesizer arg0) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onSpeechStart(SpeechSynthesizer arg0) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onStartWorking(SpeechSynthesizer arg0) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onSynthesizeFinish(SpeechSynthesizer arg0) {
   // TODO Auto-generated method stub
   
  }
  
 }

}


这个是临时的授权,如果你要得到 授权必须到百度上去注册;然后下载语音包;包里也有DEMO,DEMO里有几行其它的界面上的东西,我就把它删除了,现在有就是最主要有的

Studio版的引用别人的一个文章:

http://www.cnblogs.com/xrwang/p/AndroidStudioImportJarAndSoLibrary.html





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值