You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 line
4.2 KiB

  1. local PluginBase = require("luaScript.Plugins.PluginBase")
  2. local PluginYaya = class("PluginYaya" , PluginBase)
  3. function PluginYaya:ctor()
  4. PluginYaya.super.ctor(self);
  5. self.plugin = nil
  6. self.pluginKey = "PluginYaya";
  7. end
  8. -- 启动插件
  9. function PluginYaya:start()
  10. log("PluginYaya::start()")
  11. if isAndroidPlatform() then
  12. self.plugin = cc.PluginManager:getInstance():createPlugin("PluginYaya" , "com/ddgame/plugin/PluginYaya");
  13. elseif isIOSPlatform() then
  14. self.plugin = cc.PluginManager:getInstance():createPlugin("PluginYaya" , "PluginYaya");
  15. end
  16. if self.plugin then
  17. local userInfo = json.decode(app.user.userInfo)
  18. local tName = string.getShortName2(userInfo.nickname, 4) or tostring(app.user.loginInfo.uid)
  19. local developerInfo =
  20. {
  21. appId = "1002779",
  22. userId = app.user.loginInfo.uid,
  23. userName = tName,
  24. serverId = "3",--getGameId(),
  25. maxRecordTime = 30,
  26. pluginKey = self.pluginKey,
  27. callback = handler(self, self.onResult); -- 回调函数
  28. };
  29. log("PluginYaya::start() developerInfo = ", table.tostring(developerInfo))
  30. self.plugin:callVoid("initPlugin", developerInfo);
  31. else
  32. log("PluginYaya::start() failed")
  33. end
  34. end
  35. -- 停止插件
  36. function PluginYaya:stop()
  37. if self.plugin then
  38. cc.PluginManager:getInstance():removePlugin(self.plugin);
  39. self.plugin = nil;
  40. end
  41. end
  42. -- 开始录音
  43. function PluginYaya:startRecord()
  44. if self.plugin then
  45. self.plugin:callVoid("startRecord");
  46. end
  47. end
  48. -- 取消录音
  49. function PluginYaya:cancelRecord()
  50. if self.plugin then
  51. self.plugin:callVoid("cancelRecord");
  52. end
  53. end
  54. -- 结束录音
  55. function PluginYaya:stopRecord()
  56. if self.plugin then
  57. self.plugin:callVoid("stopRecord");
  58. end
  59. end
  60. -- 播放录音
  61. function PluginYaya:playRecord(filePath)
  62. log("PluginYaya:playRecord() filePath = ", filePath);
  63. -- 记录回调
  64. if self.plugin then
  65. local data =
  66. {
  67. filePath = tostring(filePath)
  68. }
  69. log("PluginYaya:playRecord() data = ", table.tostring(data))
  70. self.plugin:callVoid("playRecord", data);
  71. end
  72. end
  73. -- 停止播放录音
  74. function PluginYaya:stopPlayRecord()
  75. log("PluginYaya:stopPlayRecord() ");
  76. if self.plugin then
  77. self.plugin:callVoid("stopPlayRecord");
  78. end
  79. end
  80. -- 下载录音
  81. function PluginYaya:downloadRecord(nUserId, recordUrl, downloadCallback)
  82. self.downloadCallback = downloadCallback
  83. if self.plugin then
  84. local data =
  85. {
  86. recordTag = tostring(nUserId),
  87. recordUrl = tostring(recordUrl),
  88. play = true,
  89. }
  90. log("PluginYaya:downloadRecord() data = ", table.tostring(data))
  91. self.plugin:callVoid("downloadRecord", data);
  92. end
  93. end
  94. -- 回调函数
  95. function PluginYaya:onResult(result)
  96. log("PluginYaya:onResult() result = ", result)
  97. local resultJson = json.decode(result);
  98. if not resultJson then
  99. return
  100. end
  101. local pluginKey = tostring(resultJson.pluginKey)
  102. if pluginKey ~= self.pluginKey then
  103. return
  104. end
  105. local code = tonumber(resultJson.code);
  106. local info = json.decode(resultJson.message);
  107. if code == self.Code.INIT_SUCCESS then -- 初始化成功
  108. --showTooltip("呀呀语音 初始化成功")
  109. print("初始化成功", "info = ", table.tostring(info))
  110. elseif code == self.Code.INIT_FAIL then -- 初始化失败
  111. --showTooltip("呀呀语音 初始化失败")
  112. print("初始化失败", "info = ", table.tostring(info))
  113. elseif code == self.Code.RECORD_SUCCESS then -- 录音成功
  114. --showTooltip("呀呀语音 录音成功")
  115. print("录音成功", "info = ", table.tostring(info))
  116. app:dispatchEvent({name = "recordCallback", filePath = info.filePath, recordUrl = info.recordUrl, recordTime = info.recordTime });
  117. elseif code == self.Code.RECORD_FAIL then -- 录音失败
  118. --showTooltip("呀呀语音 录音失败")
  119. print("录音失败", "info = ", table.tostring(info))
  120. elseif code == self.Code.RECORD_DOWNLOAD_SUCCESS then -- 录音下载成功
  121. --showTooltip("呀呀语音 录音下载成功")
  122. print("录音下载成功", "info = ", table.tostring(info))
  123. if self.downloadCallback then
  124. self.downloadCallback(info.recordTag, info.filePath);
  125. end
  126. elseif code == self.Code.RECORD_DOWNLOAD_FAIL then -- 录音下载失败
  127. --showTooltip("呀呀语音 录音下载失败")
  128. print("录音下载失败", "info = ", table.tostring(info))
  129. end
  130. end
  131. return PluginYaya