android sdk例子,Android SDK(旧版)

注意

推荐您使用新版本Android SDK,本版本后续将不再更新。详情请参见

前提条件阅读接口说明,详情请参见

已获取项目appkey,详情请参见

已获取智能语音服务访问令牌,详情请参见

关键接口NlsClient:语音处理客户端,利用该客户端可以进行一句话识别、实时语音识别和语音合成的语音处理任务。该客户端为线程安全,建议全局仅创建一个实例。

SpeechSynthesizer:代表一次语音合成请求。

SpeechSynthesizerCallback:语音合成回调接口,在获得合成音频数据、发生错误等事件发生时会触发回调。您需要实现此接口,在回调方法中加入自己的处理逻辑。

调用顺序创建NlsClient实例。

定义SpeechSynthesizerCallback实现类,按业务需求处理识别结果或错误情况。

调用NlsClient.createSynthesizerRequest()方法得到SpeechSynthesizer实例。

设置SpeechSynthesizer参数。

包括access Token、appkey、语音文本(text)、发音人(voice)和语速(speechRate)等。

调用SpeechSynthesizer.start()方法开始与云端服务连接。

在回调中获得合成的音频数据并播放,或者处理错误。

调用SpeechSynthesizer.stop()方法结束语音合成。

说明

如果需要发起新的请求,请重复步骤3~7。

调用NlsClient.release()方法释放客户端实例。

Proguard配置

如果代码使用了混淆,请在proguard-rules.pro中配置:-keep class com.alibaba.idst.util.*{*;}

代码示例创建识别请求

// 创建语音合成对象

speechSynthesizer = client.createSynthesizerRequest(callback);

speechSynthesizer.setToken("");

speechSynthesizer.setAppkey("");

// 设置语音编码,PCM编码可以直接用audioTrack播放,其他编码不行。

speechSynthesizer.setFormat(SpeechSynthesizer.FORMAT_PCM);

// 以下选项都会改变最终合成的语音效果。

// 设置语音数据采样率

speechSynthesizer.setSampleRate(SpeechSynthesizer.SAMPLE_RATE_16K);

// 设置人声

speechSynthesizer.setVoice(SpeechSynthesizer.VOICE_XIAOGANG);

// 设置语音合成方法

speechSynthesizer.setMethod(SpeechSynthesizer.METHOD_RUS);

// 设置语速

speechSynthesizer.setSpeechRate(100);

// 设置是否返回语音对应的时间戳信息

speechSynthesizer.setEnableSubtitle(true);

// 设置要转为语音的文本

speechSynthesizer.setText("欢迎使用智能语音!");

speechSynthesizer.start()获取合成语音并播放// 获取音频数据的回调,在这里将音频写入播放器。

@Override

public void OnBinaryReceived(byte[] data, int code)

{

Log.d(TAG, "binary received length: " + data.length);

if (!playing) {

playing = true;

audioTrack.play();

}

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

}返回语音时间戳信息// 调用onMetaInfo,需要设置:SpeechSynthesizer.setEnableSubtitle(true)。

@Override

public void onMetaInfo(String message, int code) {

Log.d(TAG,"onMetaInfo " + message + ": " + String.valueOf(code));

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
List of Sample Apps The list below provides a summary of the sample applications that are available with the Android SDK. Using the links on this page, you can view the source files of the sample applications in your browser. You can also download the source of these samples into your SDK, then modify and reuse it as you need. For more information, see Getting the Samples. API Demos A variety of small applications that demonstrate an extensive collection of framework topics. Backup and Restore A simple example that illustrates a few different ways for an application to implement support for the Android data backup and restore mechanism. Bluetooth Chat An application for two-way text messaging over Bluetooth. BusinessCard An application that demonstrates how to launch the built-in contact picker from within an activity. This sample also uses reflection to ensure that the correct version of the contacts API is used, depending on which API level the application is running under. Contact Manager An application that demonstrates how to query the system contacts provider using the ContactsContract API, as well as insert contacts into a specific account. Home A home screen replacement application. JetBoy A game that demonstrates the SONiVOX JET interactive music technology, with JetPlayer. Live Wallpaper An application that demonstrates how to create a live wallpaper and bundle it in an application that users can install on their devices. Lunar Lander A classic Lunar Lander game. Multiple Resolutions A sample application that shows how to use resource directory qualifiers to provide different resources for different screen configurations. Note Pad An application for saving notes. Similar (but not identical) to the Notepad tutorial. SampleSyncAdapter Demonstrates how an application can communicate with a cloud-based service and synchronize its data with data stored locally in a content provider. The sample uses two related parts of the Android framework — the account manager and the synchronization manager (through a sync adapter). Searchable Dictionary A sample application that demonstrates Android's search framework, including how to provide search suggestions for Quick Search Box. Snake An implementation of the classic game "Snake." Soft Keyboard An example of writing an input method for a software keyboard. Spinner A simple application that serves as an application-under-test for the SpinnerTest sample application. SpinnerTest An example test application that contains test cases run against the Spinner sample application. To learn more about the application and how to run it, please read the Activity Testing tutorial. TicTacToeLib An example of an Android library project that provides a game-play Activity to any dependent application project. For an example of how an application can use the code and resources in an Android library project, see the TicTacToeMain sample application. TicTacToeMain An example of an Android application that makes use of code and resources provided in an Android library project. Specifically, this application uses code and resources provided in the TicTacToeLib library project. Wiktionary An example of creating interactive widgets for display on the Android home screen. Wiktionary (Simplified) A simple Android home screen widgets example.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值