语音控制示例
约 444 字大约 1 分钟
2025-05-29
该示例展示了如何使用 MagicRobot SDK 进行初始化、连接机器人、音频控制(获取音量、设置音量、TTS 播放)等基本操作。
示例文件:audio_example.cpp
参考文档:audio_reference.md
#include "magic_robot.h"
#include <unistd.h>
#include <csignal>
#include <iostream>
using namespace magic::robot;
magic::robot::MagicRobot robot;
void signalHandler(int signum) {
std::cout << "Interrupt signal (" << signum << ") received.\n";
robot.Shutdown();
// 退出进程
exit(signum);
}
int main() {
// 绑定 SIGINT(Ctrl+C)
signal(SIGINT, signalHandler);
std::string local_ip = "192.168.54.111";
// 配置本机网线直连机器的IP地址,并进行SDK初始化
if (!robot.Initialize(local_ip)) {
std::cerr << "robot sdk initialize failed." << std::endl;
robot.Shutdown();
return -1;
}
// 设置rpc超时时间为5s
robot.SetTimeout(5000);
// 连接机器人
auto status = robot.Connect();
if (status.code != ErrorCode::OK) {
std::cerr << "connect robot failed"
<< ", code: " << status.code
<< ", message: " << status.message << std::endl;
robot.Shutdown();
return -1;
}
// 获取音频控制器
auto& controller = robot.GetAudioController();
// 获取机器人当前音量
int get_volume = 0;
status = controller.GetVolume(get_volume);
if (status.code != ErrorCode::OK) {
std::cerr << "get volume failed"
<< ", code: " << status.code
<< ", message: " << status.message << std::endl;
robot.Shutdown();
return -1;
}
std::cout << "get volume success, volume: " << std::to_string(get_volume) << std::endl;
// 设置机器人音量
status = controller.SetVolume(7);
if (status.code != ErrorCode::OK) {
std::cerr << "set volume failed"
<< ", code: " << status.code
<< ", message: " << status.message << std::endl;
robot.Shutdown();
return -1;
}
// 校验设置的音量是否正确
status = controller.GetVolume(get_volume);
if (status.code != ErrorCode::OK) {
std::cerr << "get volume failed"
<< ", code: " << status.code
<< ", message: " << status.message << std::endl;
robot.Shutdown();
return -1;
}
std::cout << "get volume success, volume: " << std::to_string(get_volume) << std::endl;
// 播放语音
TtsCommand tts;
tts.id = "100000000001";
tts.content = "今天天气怎么样!";
tts.priority = TtsPriority::HIGH;
tts.mode = TtsMode::CLEARTOP;
status = controller.Play(tts);
if (status.code != ErrorCode::OK) {
std::cerr << "play tts failed"
<< ", code: " << status.code
<< ", message: " << status.message << std::endl;
robot.Shutdown();
return -1;
}
// 等待2s
usleep(5000000);
// 停止播放语音
status = controller.Stop();
if (status.code != ErrorCode::OK) {
std::cerr << "stop tts failed"
<< ", code: " << status.code
<< ", message: " << status.message << std::endl;
robot.Shutdown();
return -1;
}
// 等待5s
usleep(2000000);
// 断开与机器人的链接
status = robot.Disconnect();
if (status.code != ErrorCode::OK) {
std::cerr << "disconnect robot failed"
<< ", code: " << status.code
<< ", message: " << status.message << std::endl;
robot.Shutdown();
return -1;
}
robot.Shutdown();
return 0;
}