Android API开发之TTS开发之Android TTS阿里云Android SDK开发

  • Post author:
  • Post category:其他



1.首先申请账号


连接:https://help.aliyun.com/document_detail/30437.html?spm=a2c4g.11186623.2.4.MpLjTc


2.阿里云开发


连接:https://help.aliyun.com/document_detail/30419.html?spm=a2c4g.11186623.6.574.ZKbbYw


3.开发


3.1.添加权限


<uses-sdk android:minSdkVersion=”17″ />

<uses-permission android:name=”android.permission.INTERNET” />

<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />

<uses-permission android:name=”android.permission.READ_PHONE_STATE” />

<uses-permission android:name=”android.permission.RECORD_AUDIO” />

<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />

<uses-permission android:name=”android.permission.ACCESS_WIFI_STATE” />

<uses-permission android:name=”android.permission.WRITE_SETTINGS” />

<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />

<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />

<uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE” />


3.2.配置jar及so文件






3.3.创建布局




3.4.java代码


3.4.1.MainActivity类


package com.alibaba.idst.nlsdemo;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import com.alibaba.idst.R;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

/**

* 获取页面点击对应的appkey

* 将appkey传入后面的服务

* Asr

* */



public void AsrDemo(View view){


Intent intent = new Intent(this, PublicAsrActivity.class);

startActivity(intent);

}

public void NluDemo(View view){


Intent intent = new Intent(this, PublicNluActivity.class);

startActivity(intent);

}

public void TtsDemo(View v){


Intent intent = new Intent(this, PublicTtsActivity.class);

startActivity(intent);

}

}


3.4.2.PublicAsrActivity类


package com.alibaba.idst.nlsdemo;

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

import com.alibaba.idst.R;

import com.alibaba.idst.nls.NlsClient;

import com.alibaba.idst.nls.NlsListener;

import com.alibaba.idst.nls.StageListener;

import com.alibaba.idst.nls.internal.protocol.NlsRequest;

import com.alibaba.idst.nls.internal.protocol.NlsRequestProto;

public class PublicAsrActivity extends Activity {

private boolean isRecognizing = false;

private EditText mFullEdit;

private EditText mResultEdit;

private Button mStartButton;

private Button mStopButton;

private NlsClient mNlsClient;

private NlsRequest mNlsRequest;

private Context context;

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_public_asr);

context = getApplicationContext();

mFullEdit = (EditText) findViewById(R.id.editText2);

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

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

mStopButton = (Button) findViewById(R.id.button2);

String appkey = “nls-service”; //请设置申请到的Appkey

mNlsRequest = initNlsRequest();

mNlsRequest  = initNlsRequest();

mNlsRequest.setApp_key(appkey);    //appkey请从 “快速开始” 帮助页面的appkey列表中获取

mNlsRequest.setAsr_sc(“opu”);      //设置语音格式

/*设置热词相关属性*/

//mNlsRequest.setAsrVocabularyId(“vocabid”);

/*设置热词相关属性*/

NlsClient.openLog(true);

NlsClient.configure(getApplicationContext()); //全局配置

mNlsClient = NlsClient.newInstance(this, mRecognizeListener, mStageListener,mNlsRequest);                          //实例化NlsClient

mNlsClient.setMaxRecordTime(60000);  //设置最长语音

mNlsClient.setMaxStallTime(1000);    //设置最短语音

mNlsClient.setMinRecordTime(500);    //设置最大录音中断时间

mNlsClient.setRecordAutoStop(false);  //设置VAD

mNlsClient.setMinVoiceValueInterval(200); //设置音量回调时长

initStartRecognizing();

initStopRecognizing();

}

private NlsRequest initNlsRequest(){


NlsRequestProto proto = new NlsRequestProto(context);

//proto.setApp_user_id(“”); //设置在应用中的用户名,可选

return new NlsRequest(proto);

}

private void initStartRecognizing(){


mStartButton.setOnClickListener(new View.OnClickListener() {


@Override

public void onClick(View view) {


isRecognizing = true;

mResultEdit.setText(“正在录音,请稍候!”);

mNlsRequest.authorize(“”, “”); //请替换为用户申请到的数加认证key和密钥

mNlsClient.start();

mStartButton.setText(“录音中。。。”);

}

});

}

private void initStopRecognizing(){


mStopButton.setOnClickListener(new View.OnClickListener() {


@Override

public void onClick(View view) {


isRecognizing = false;

mResultEdit.setText(“”);

mNlsClient.stop();

mStartButton.setText(“开始 录音”);

}

});

}

private NlsListener mRecognizeListener = new NlsListener() {

@Override

public void onRecognizingResult(int status, RecognizedResult result) {


switch (status) {


case NlsClient.ErrorCode.SUCCESS:

Log.i(“asr”, “[demo]  callback onRecognizResult ” + result.asr_out);

mResultEdit.setText(result.asr_out);

mFullEdit.setText(result.asr_out);

break;

case NlsClient.ErrorCode.RECOGNIZE_ERROR:

Toast.makeText(PublicAsrActivity.this, “recognizer error”, Toast.LENGTH_LONG).show();

break;

case NlsClient.ErrorCode.RECORDING_ERROR:

Toast.makeText(PublicAsrActivity.this,”recording error”,Toast.LENGTH_LONG).show();

break;

case NlsClient.ErrorCode.NOTHING:

Toast.makeText(PublicAsrActivity.this,”nothing”,Toast.LENGTH_LONG).show();

break;

}

isRecognizing = false;

}

} ;

private StageListener mStageListener = new StageListener() {


@Override

public void onStartRecognizing(NlsClient recognizer) {


super.onStartRecognizing(recognizer);    //To change body of overridden methods use File | Settings | File Templates.

}

@Override

public void onStopRecognizing(NlsClient recognizer) {


super.onStopRecognizing(recognizer);    //To change body of overridden methods use File | Settings | File Templates.

}

@Override

public void onStartRecording(NlsClient recognizer) {


super.onStartRecording(recognizer);    //To change body of overridden methods use File | Settings | File Templates.

}

@Override

public void onStopRecording(NlsClient recognizer) {


super.onStopRecording(recognizer);    //To change body of overridden methods use File | Settings | File Templates.

}

@Override

public void onVoiceVolume(int volume) {


super.onVoiceVolume(volume);

}

};

}


3.4.3.PublicNluActivity类


public class PublicNluActivity extends Activity {

private boolean isRecognizing = false;

private EditText mFullEdit;

private EditText mResultEdit;

private Button mStartButton;

private Button mStopButton;

private NlsClient mNlsClient;

private NlsRequest mNlsRequest;

private Context context;

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_public_nlu);

context = getApplicationContext();

mFullEdit = (EditText) findViewById(R.id.editText2_nlu);

mResultEdit = (EditText) findViewById(R.id.editText_nlu);

mStartButton = (Button) findViewById(R.id.button_nlu);

mStopButton = (Button) findViewById(R.id.button2_nlu);

String appkey = “nls-service”; //请设置申请到的Appkey

mNlsRequest = initNlsRequest();

mNlsRequest.setApp_key(appkey);    //appkey请从 “快速开始” 帮助页面的appkey列表中获取

mNlsRequest.setAsr_sc(“opu”);      //设置语音格式

mNlsRequest.enableCloudNLUResult(); //初始化NLU请求

NlsClient.openLog(true);

NlsClient.configure(getApplicationContext()); //全局配置

mNlsClient = NlsClient.newInstance(this, mRecognizeListener, mStageListener,mNlsRequest);                          //实例化NlsClient

mNlsClient.setMaxRecordTime(60000);  //设置最长语音

mNlsClient.setMaxStallTime(1000);    //设置最短语音

mNlsClient.setMinRecordTime(500);    //设置最大录音中断时间

mNlsClient.setRecordAutoStop(false);  //设置VAD

mNlsClient.setMinVoiceValueInterval(200); //设置音量回调时长

initStartRecognizing();

initStopRecognizing();

}

private NlsRequest initNlsRequest(){


NlsRequestProto proto = new NlsRequestProto(context);

//proto.setApp_user_id(“user_id”); //设置用户名

return new NlsRequest(proto);

}

private void initStartRecognizing(){


mStartButton.setOnClickListener(new View.OnClickListener() {


@Override

public void onClick(View view) {


isRecognizing = true;

mResultEdit.setText(“正在录音,请稍候!”);

mNlsRequest.authorize(“”, “”); //请替换为用户申请到的数加认证key和密钥

mNlsClient.start();

mStartButton.setText(“录音中。。。”);

}

});

}

private void initStopRecognizing(){


mStopButton.setOnClickListener(new View.OnClickListener() {


@Override

public void onClick(View view) {


isRecognizing = false;

mResultEdit.setText(“”);

mNlsClient.stop();

mStartButton.setText(“开始 录音”);

}

});

}

private NlsListener mRecognizeListener = new NlsListener() {

@Override

public void onRecognizingResult(int status, RecognizedResult result) {


switch (status) {


case NlsClient.ErrorCode.SUCCESS:

mResultEdit.setText(result.asr_out);

mFullEdit.setText(result.ds_out);

break;

case NlsClient.ErrorCode.RECOGNIZE_ERROR:

Toast.makeText(PublicNluActivity.this, “recognizer error”, Toast.LENGTH_LONG).show();

break;

case NlsClient.ErrorCode.RECORDING_ERROR:

Toast.makeText(PublicNluActivity.this,”recording error”,Toast.LENGTH_LONG).show();

break;

case NlsClient.ErrorCode.NOTHING:

Toast.makeText(PublicNluActivity.this,”nothing”,Toast.LENGTH_LONG).show();

break;

}

isRecognizing = false;

}

} ;

private StageListener mStageListener = new StageListener() {


@Override

public void onStartRecognizing(NlsClient recognizer) {


super.onStartRecognizing(recognizer);    //To change body of overridden methods use File | Settings | File Templates.

}

@Override

public void onStopRecognizing(NlsClient recognizer) {


super.onStopRecognizing(recognizer);    //To change body of overridden methods use File | Settings | File Templates.

}

@Override

public void onStartRecording(NlsClient recognizer) {


super.onStartRecording(recognizer);    //To change body of overridden methods use File | Settings | File Templates.

}

@Override

public void onStopRecording(NlsClient recognizer) {


super.onStopRecording(recognizer);    //To change body of overridden methods use File | Settings | File Templates.

}

@Override

public void onVoiceVolume(int volume) {


super.onVoiceVolume(volume);

}

};

}


3.4.4.


public class PublicTtsActivity extends Activity {


private static final String TAG = “PublicTtsActivity”;

private TextView UserContent;

private Button  Send_User_Content;

private NlsClient mNlsClient;

private Context context;

private NlsRequest mNlsRequest;

int iMinBufSize = AudioTrack.getMinBufferSize(8000,

AudioFormat.CHANNEL_CONFIGURATION_STEREO,

AudioFormat.ENCODING_PCM_16BIT);

AudioTrack audioTrack=new AudioTrack(AudioManager.STREAM_MUSIC, 8000,

AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT,

iMinBufSize, AudioTrack.MODE_STREAM) ; //使用audioTrack播放返回的pcm数据

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

context = getApplicationContext();

setContentView(R.layout.activity_public_tts);

UserContent = (TextView) findViewById(R.id.UserContent_tts);

Send_User_Content = (Button) findViewById(R.id.send_tts);

String appkey = “”;//请设置申请到的Appkey

mNlsRequest = initNlsRequest();

mNlsRequest.setApp_key(appkey);    //appkey请从 “快速开始” 帮助页面的appkey列表中获取

mNlsRequest.initTts();               //初始化tts请求

NlsClient.openLog(true);

NlsClient.configure(getApplicationContext()); //全局配置

mNlsClient = NlsClient.newInstance(this, mRecognizeListener, null ,mNlsRequest);                          //实例化NlsClient

initTtsContentButton();

}

private NlsRequest initNlsRequest(){


NlsRequestProto proto = new NlsRequestProto(context);

//proto.setApp_user_id(“”); //设置用户名

return new NlsRequest(proto);

}

private void initTtsContentButton(){


Send_User_Content.setOnClickListener(new View.OnClickListener() {


@Override

public void onClick(View view) {


String user_input = UserContent.getText().toString();

if (user_input.equals(“”)){


Toast.makeText(PublicTtsActivity.this, “输入不能为空!”, Toast.LENGTH_LONG).show();

}else {


mNlsRequest.authorize(“”, “”);       //请替换为用户申请到的数加认证key和密钥

mNlsRequest.setTtsEncodeType(“pcm”); //返回语音数据格式,支持pcm,wav.alaw

mNlsRequest.setTtsVolume(50);   //音量大小默认50,阈值0-100

mNlsRequest.setTtsSpeechRate(0);//语速,阈值-500~500

mNlsClient.PostTtsRequest(user_input); //用户输入文本

audioTrack.play();

}

}

});

}

private NlsListener mRecognizeListener = new NlsListener() {


@Override

public void onTtsResult(int status, byte[] ttsResult){


switch (status) {


case NlsClient.ErrorCode.TTS_BEGIN :

audioTrack.play();

Log.e(TAG, “tts begin”);

audioTrack.write(ttsResult, 0, ttsResult.length);

break;

case NlsClient.ErrorCode.TTS_TRANSFERRING :

Log.e(TAG,”tts transferring”+ttsResult.length);

audioTrack.write(ttsResult, 0, ttsResult.length);

break;

case NlsClient.ErrorCode.TTS_OVER :

audioTrack.stop();

Log.e(TAG,”tts over”);

break;

case NlsClient.ErrorCode.CONNECT_ERROR :

Toast.makeText(PublicTtsActivity.this, “CONNECT ERROR”, Toast.LENGTH_LONG).show();

break;

}

}

} ;

@Override

protected void onDestroy() {


audioTrack.release();

super.onDestroy();

}

}



版权声明:本文为weixin_37730482原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。