Cocos2dx realizes Android's TTS voice (Implementation of language reading)

Source: Internet
Author: User

In fact very easy, but some details need attention.


About the application of the voice in the Android, we need to download the SDK, and then according to the voice of the audio API on the Android demo on the success of the implementation, that thing is quite simple.


Then, we also need to put their own COCOS2DX project on the Android deployment, these online information is very much, also do not explain, perhaps I will also make a corresponding summary. Not much to say here.


OK, nonsense not much to say, directly on the code


The idea is to implement C + + Java calls through COCOS2DX's Jnihelper class, and then implement speech functions


Not much nonsense said:

Directly on the code: here I used a voice class to call the speech directly in the project



Voice.h


Voice.cpp

#include "Voice.h" #include "cocos2d.h" #include "IconvString.h" #if (cc_target_platform = = cc_platform_android) # Include "jni.h" #include "platform/android/jni/jnihelper.h" #endif using_ns_cc;void onvoice (const char *data) {#if (cc_ Target_platform = = cc_platform_android) jnimethodinfo Jmi;//java class name const char * jniclass = "org/cocos2dx/cpp/ Appactivity ";//Get the Java part of the method, the specific reference please refer to the Jnimethodinfo class source code, the parameters are, class object, Java class, Java class method, the method's parameters and return value, V represents the void type return value (that is, no return value, The representation of a polygon in parentheses is a string "See JNI Signature Specification") bool Ishave = Jnihelper::getstaticmethodinfo (Jmi,jniclass, "Onvoicestart", "(ljava/ lang/string;) V "); if (Ishave) {//Convert string and format jstring jdata = Jmi.env->newstringutf (GBKToUTF8 (data));//Method calling Java, The first 2 parameters are fixed, followed by the number of references in the Java method Jmi.env->callstaticvoidmethod (Jmi.classid, Jmi.methodid, jdata);} #endif}

Java-side code such as the following (with Baidu ads implementation code)

Package Org.cocos2dx.cpp;import Org.cocos2dx.lib.cocos2dxactivity;import Android.app.ActionBar.LayoutParams; Import Android.os.bundle;import Android.util.log;import Android.widget.relativelayout;import Com.baidu.mobads.adview;import Com.iflytek.cloud.speechconstant;import Com.iflytek.cloud.speecherror;import Com.iflytek.cloud.speechsynthesizer;import Com.iflytek.cloud.speechutility;import Com.iflytek.cloud.synthesizerlistener;public class Appactivity extends Cocos2dxactivity {//Voice + installation helper Classes @overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Speechutility.createutility (Appactivity.this, "appid=53bb48d3"); ShowAds ();} /** * C + + call method to implement voice broadcast * @param value to broadcast * @param the role of the person voice, suggested with the little girl Vinn * @param voicespeed voice speed, suggested * @param voic Epitch tone, suggested * @param voicevolume volume recommended *///string value,string person,string voicespeed,string voicepitch,string Voicevolumepublic static void Onvoicestart (final String value) throws Exception{speechsynthesizerMTts = Speechsynthesizer.createsynthesizer (Appactivity.getcontext (), null); Mtts.setparameter (Speechconstant.voice _name, "Vinn");//Set Speech speed Mtts.setparameter (speechconstant.speed, "45");//Set Tone Mtts.setparameter (Speechconstant.pitch, "55");//Set Volume Mtts.setparameter (speechconstant.volume, "100");//set player audio stream type Mtts.setparameter (speechconstant.stream _type, "2"); Mtts.startspeaking (value, Mttslistener);} private static Synthesizerlistener Mttslistener = new Synthesizerlistener () {@Overridepublic void Onspeakbegin () {}@ overridepublic void onspeakpaused () {} @Overridepublic void onspeakresumed () {} @Overridepublic void onbufferprogress ( int percent, int beginpos, int endpos,string info) {} @Overridepublic void onspeakprogress (int percent, int beginpos, int e Ndpos) {} @Overridepublic void oncompleted (speecherror error) {}};//show ad private void ShowAds () {//relative positioning layout relativelayout l  Ayout = new Relativelayout (this); Define Addcontentview (Layout,new layoutparams (Android.view.ViewGroup.LayoutParams.MATCH_PARE) for the wide-height attribute of the ad area.NT, Android.view.ViewGroup.LayoutParams.MATCH_PARENT));      Add the ad layer to the current interface AdView AdView = new AdView (this); Size Relativelayout.layoutparams layoutparams = new Relativelayout.layoutparams (     Android.view.ViewGroup.LayoutParams.MATCH_PARENT, Android.view.ViewGroup.LayoutParams.WRAP_CONTENT);     Position Layoutparams.addrule (relativelayout.align_parent_top); Added into the project to display Layout.addview (adview,layoutparams); }}

Code for the calling language:

Onvoice ("You Are my little Apple");

External use don't forget to introduce a header file.

#include "voice/voice.h"

This can be achieved, Baidu Advertising section, we are interested to study, but also need the SDK OH


Cocos2dx realizes Android's TTS voice (Implementation of language reading)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.