CocosCreator 接GVoice
安卓的比较简单就不说了,这里主要说IOS的,GVoide官方没有文档,不支持CocosCreator ,IOS的SDK不支持Object-c,是C++版本的,官方也只有API文档,没有demo,我花了一天才接好这个SDK,分享大家看下。
这是腾讯GVoice 客服发送的SDK 包,在官网注册 Gvoice官网,官网只支持Ue4 和 unity3d 平台,注册时候随便选择一个平台,我们只是为了拿到游戏ID 和 key
联系客户发了一个包,如下图
下面开始接入流程,将SDK 如下图所示导入工程
新建一个类 GVoice.h ,这个是C++ 的具体执行类,写成单例模式的。如
新建一个GVoiceNotify类,继承IGCloudVoiceNotify,主要是为了接受回调,如下图
在GVoice 中实现初始化方法
然后定时代用 Pool 方法,定时调用才能取到回调,我的定时写在Object-c中
万抽
完成这些后,设置语音模式,设置key,我是使用的离线语音。如下图
然后实现几个个方法,开始录音、结束录音、上传录音、下载录音、播放录音,具体如下
在info 添加麦克风权限Privacy - Microphone Usage Description
这样在Object-端就可以调用了,当我露营完毕,并上传腾讯服后,会返回ID,我们返回给游戏服,游戏服将ID 广播出来
播放录音,先根据id 下载下来,再播放,播放完毕后停止
下载录音
完毕后播放录音
播放完毕后停止播放录音
这样就完成了,具体代码我贴到下面
Gvoice.h
class Gvoice
{
public:
static Gvoice* getInstance();
virtual ~Gvoice();
IGCloudVoiceEngine* sharedInstance();
IGCloudVoiceEngine* getVoiceEngine();
GCloudVoiceErrno setAppInfo();
void updatePoll();
void startReCord(); //开始录音
void stopRecord(int date); //结束录音
void downLoadFile(const char *fileID); //下载语音
void playVoice(const char *filePath); //播放录音
void stopPlayVoice();
void SetFilePath(string fileDir);
int getTime();
private:
Gvoice();
static Gvoice* m_instance;
IGCloudVoiceEngine* sVoiceEngine; //GVoice引擎对象
IGCloudVoiceNotify* sVoiceNotify; //
int time; //当前录音时间
int timeOut; //超时时间
string fileDir; //文本存储文件夹
void setApplyMessage();
void setMode();
void UploadRecordedFile(); //发布录音时间
string GetFilePath(); //获取存储路径
};
Gvoice.cpp
#include "GVoice.h"
#include <iostream>
using namespace std;
using namespace gcloud_voice;
Gvoice* Gvoice::m_instance = new Gvoice();
Gvoice::Gvoice(){
cout << "Gvoice" << endl;
sVoiceEngine = gcloud_voice::GetVoiceEngine();
sVoiceNotify = new GVoiceNotify();
time = 0;
timeOut = 10000;
//调用OC 获取沙盒路径
// getLocalPath
}
Gvoice::~Gvoice(){
cout << "~Gvoice" << endl;
delete sVoiceNotify;
}
Gvoice* Gvoice::getInstance(){
return m_instance;
}
IGCloudVoiceEngine* Gvoice::getVoiceEngine(){
return sVoiceEngine;
}
GCloudVoiceErrno Gvoice::setAppInfo(){
cout << "---------- Gvoice::setAppInfo-------" << endl;
GCloudVoiceErrno ret = GCLOUD_VOICE_PARAM_NULL;
if(sVoiceEngine){
ret = sVoiceEngine->SetAppInfo(gvoiceOpenId,gvoiceKey,"openID");
if(ret == GCLOUD_VOICE_SUCC){
cout << "SetAppInfo Success" << endl;
}else{
cout << "SetAppInfo fail" << ret << endl;
}
ret = sVoiceEngine->Init();
if(ret == GCLOUD_VOICE_SUCC){
cout << "Init Success" << endl;
}else{
cout << "Init fail" << ret << endl;
}
ret = sVoiceEngine->SetNotify(sVoiceNotify);
if(ret == GCLOUD_VOICE_SUCC){
cout << "SetNotify Success" << endl;
setApplyMessage();
setMode();
}else{
cout << "SetNotify fail" << ret << endl;
}
}
return ret;
}
void Gvoice::setApplyMessage(){
if(sVoiceEngine){
GCloudVoiceErrno ret = sVoiceEngine->ApplyMessageKey(10000);
if(ret == GCLOUD_VOICE_SUCC){
cout << "ApplyMessageKey Success" << endl;
}else{
cout << "ApplyMessageKey fail" << ret << endl;
}
}
}
void Gvoice::setMode(){
if(sVoiceEngine){
GCloudVoiceErrno ret = sVoiceEngine->SetMode(Messages);//离线语音
if(ret == GCLOUD_VOICE_SUCC){
cout << "SetMode Success" << "设置为离线语音模式" <<endl;
}else{
cout << "SetMode fail" << ret << endl;
}
}
}
void Gvoice::updatePoll(){
GCloudVoiceErrno ret = sVoiceEngine->Poll();
if(ret == GCLOUD_VOICE_SUCC){
cout << "Poll Success" << endl;
}else{
cout << "Poll fail" << ret << endl;
}
}
//开始录音
void Gvoice::startReCord(){
cout << "startReCord" << endl;
string _filePath = GetFilePath() + "/record.dat";
GCloudVoiceErrno ret = sVoiceEngine->StartRecording(_filePath.c_str());
if(ret == GCLOUD_VOICE_SUCC){
cout << "正在开始录音" << endl;
}else{
cout << "未能开始录音" << ret << endl;
}
}
//结束录音
void Gvoice::stopRecord(int date){
time = date;
if(!sVoiceEngine) return;
GCloudVoiceErrno ret = sVoiceEngine->StopRecording();
if (ret == GCLOUD_VOICE_SUCC) {
cout << "录音停止" << endl;
UploadRecordedFile();
} else {
cout << "未能成功停止录音 Error code: " << ret << endl;
}
}
//发布录音时间
void Gvoice::UploadRecordedFile(){
if(!sVoiceEngine) return;
string _filePath = GetFilePath() + "/record.dat";
GCloudVoiceErrno ret = sVoiceEngine->UploadRecordedFile(_filePath.c_str(),timeOut);
if (ret == GCLOUD_VOICE_SUCC) {
cout << "UploadRecordedFile Success" << endl;
}else{
cout << "UploadRecordedFile faile" << ret << endl;
}
}
//下载录音
void Gvoice::downLoadFile(const char *fileID){
if(!sVoiceEngine) return;
string _filePath = GetFilePath() + "/record.dat";
GCloudVoiceErrno ret = sVoiceEngine->DownloadRecordedFile(fileID,_filePath.c_str());
if(ret == GCLOUD_VOICE_SUCC){
cout << "downLoadFile Success" << endl;
}else{
cout << "downLoadFile faile" << ret << endl;
}
}
//播放录音
void Gvoice::playVoice(const char *filePath){
if(!sVoiceEngine) return;
GCloudVoiceErrno ret = sVoiceEngine->PlayRecordedFile(filePath);
if(ret == GCLOUD_VOICE_SUCC){
cout << "playVoice Success" << endl;
}else{
cout << "playVoice faile" << ret << endl;
}
}
//停止播放录音
void Gvoice::stopPlayVoice(){
if(!sVoiceEngine) return;
GCloudVoiceErrno ret = sVoiceEngine->StopPlayFile();
if(ret == 0){
cout << "stopPlayVoice Success" << endl;
}else{
cout << "stopPlayVoice faile" << ret << endl;
}
}
string Gvoice::GetFilePath(){
return fileDir;
}
void Gvoice::SetFilePath(string file){
fileDir = file;
}
int Gvoice::getTime(){
return time;
}
GVoiceNotify.h
#ifndef GVoiceNotify_hpp
#define GVoiceNotify_hpp
#include <stdio.h>
#include <iostream>
#include "GCloudVoiceNotify.h"
#include "GVoice.h"
#include "AppDelegate.h"
using namespace std;
using namespace gcloud_voice;
class GVoiceNotify: public IGCloudVoiceNotify
{
public:
GVoiceNotify();
virtual ~GVoiceNotify();
virtual void OnApplyMessageKey(GCloudVoiceCompleteCode code);
virtual void OnUploadFile(GCloudVoiceCompleteCode code, const char *filePath, const char *fileID) ;
virtual void OnDownloadFile(GCloudVoiceCompleteCode code, const char *filePath, const char *fileID) ;
virtual void OnPlayRecordedFile(GCloudVoiceCompleteCode code, const char *filePath) ;
};
#endif /* GVoiceNotify_hpp */
GVoiceNotify.cpp
#include <stdio.h>
#include "GVoiceNotify.hpp"
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
GVoiceNotify::GVoiceNotify(){
}
GVoiceNotify::~GVoiceNotify(){
}
void GVoiceNotify::OnApplyMessageKey(GCloudVoiceCompleteCode code){
if(code == GV_ON_MESSAGE_KEY_APPLIED_SUCC){
cout << " OnApplyMessageKey Success " << endl;
}else{
cout << " OnApplyMessageKey faile " << endl;
}
}
void GVoiceNotify::OnUploadFile(GCloudVoiceCompleteCode code, const char *filePath, const char *fileID){
if(code == GV_ON_UPLOAD_RECORD_DONE){
cout << " OnUploadFile Success" << endl;
int time =Gvoice::getInstance()->getTime();
char str[1024] = "\0";
sprintf(str,"SDKCallFunc('GVoice','{\"ID\":\"%s\",\"time\":%d}')", fileID , time);
cout << "-------------- OnUploadFile faile --------------- " << str << endl;
se::ScriptEngine::getInstance()->evalString(str);
}else{
cout << " OnUploadFile Faile" << code << endl;
}
}
void GVoiceNotify::OnDownloadFile(GCloudVoiceCompleteCode code, const char *filePath, const char *fileID){
if(code == GV_ON_DOWNLOAD_RECORD_DONE){
cout << " OnDownloadFile Success" << endl;
Gvoice::getInstance()->playVoice(filePath);
}else{
cout << " OnDownloadFile Faile" << code << endl;
}
}
void GVoiceNotify::OnPlayRecordedFile(GCloudVoiceCompleteCode code, const char *filePath){
if(code == GV_ON_PLAYFILE_DONE){
cout << " OnPlayRecordedFile Success" << endl;
Gvoice::getInstance()->stopPlayVoice();
}else{
cout << " OnPlayRecordedFile Faile" << endl;
}
}
AppController(Object-C )
/------------------GVoice 语音 Start---------------------------
//初始化GVoice
-(void) initGVoice
{
Gvoice::getInstance()->setAppInfo();
//时间间隔
NSTimeInterval timeInterval = 0.5 ;
//定时器 repeats 表示是否需要重复,NO为只重复一次
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(Timered:) userInfo:nil repeats:YES];
}
- (void)Timered:(NSTimer*)timer {
Gvoice::getInstance()->updatePoll();
}
//开始录音
+(void) startRecord
{
//获取本地路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFilePath = paths.firstObject;
std::string path =[documentFilePath UTF8String];
Gvoice::getInstance()->SetFilePath(path);
//开始录音
Gvoice::getInstance()->startReCord();
}
//结束录音
+(void) stopRecord:(NSString*) time
{
int _time = [time intValue];
Gvoice::getInstance()->stopRecord(_time);
}
//下载并录音
+(void) play: (NSString*) ID
{
const char * id =[ID UTF8String];
Gvoice::getInstance()->downLoadFile(id);
}
//------------------GVoice 语音 End---------------------------
以上就是CocosCreator 接GVoice 的版本,谢谢大家!