index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <template>
  2. <view class="content">
  3. <view class="uni-margin-wrap">
  4. <swiper class="swiper" circular :indicator-dots="true" indicator-color="#DE868F" indicator-active-color="#fff"
  5. :autoplay="true" :interval="1000" :duration="500">
  6. <swiper-item>
  7. <view class="swiper-item">功能介绍1</view>
  8. </swiper-item>
  9. <swiper-item>
  10. <view class="swiper-item">功能介绍2</view>
  11. </swiper-item>
  12. <swiper-item>
  13. <view class="swiper-item">功能介绍3</view>
  14. </swiper-item>
  15. </swiper>
  16. </view>
  17. <form class="form-area">
  18. <view class="form-area-item">
  19. <text class="form-area-item-text">视频名称</text>
  20. <input type="text" v-model="videoName" placeholder="请输入视频名称">
  21. </view>
  22. <view class="form-area-item">
  23. <text class="form-area-item-text">选择您的形象</text>
  24. <button @click="chooseImage" class="cu-btn bg-red">选择图片</button>
  25. <text class="none-file" v-if="!imagePath">未选择图片</text>
  26. <image :src="imagePath" v-else mode="aspectFit" style="width: 30px; height: 30px;margin-left:10px" />
  27. </view>
  28. <view class="form-area-item" style="align-items: baseline;">
  29. <text class="form-area-item-text">视频内容</text>
  30. <uni-data-checkbox selectedColor="#BD3124" selectedTextColor="#BD3124" v-model="current"
  31. :localdata="videoContent" />
  32. </view>
  33. <view class="form-area-item" v-if="current=='text'">
  34. <textarea v-model="videoText" placeholder="请输入视频中的文字内容" />
  35. </view>
  36. <view class="form-area-item" v-if="current=='text'">
  37. <text class="form-area-item-text">音色选择</text>
  38. <uni-data-checkbox selectedColor="#BD3124" selectedTextColor="#BD3124" v-model="timbre"
  39. :localdata="timbreContent" />
  40. </view>
  41. <view class="form-area-item" v-if="current=='record'">
  42. <button @click="toggleRecording">{{ isRecording ? '停止录音' : '开始录音' }}</button>
  43. </view>
  44. <view class="form-area-item" v-if="current=='audio'" :key="timeStamp">
  45. <text class="form-area-item-text">选择音频文件</text>
  46. <button @click="chooseFile" class="cu-btn bg-red">选择文件</button>
  47. <text class="file-name" v-if="fileInfo.name">{{fileInfo.name}}</text>
  48. <text class="none-file" v-if="!showPerson">未选择文件</text>
  49. </view>
  50. <audio v-if="audioPath" class="audio-bar" :src="audioPath" name="语音试听" poster="../static/images/xijiao/avator.png"
  51. :action="audioAction" controls></audio>
  52. </form>
  53. <view class="btn-area">
  54. <button @click="jumpToList" class="bg-red lg round create-project">生成数字人视频</button>
  55. <uni-popup ref="alertDialog" type="dialog">
  56. <uni-popup-dialog type="warning" cancelText="确定" title="提示" :content="missingContent"
  57. @close="dialogClose"></uni-popup-dialog>
  58. </uni-popup>
  59. </view>
  60. </view>
  61. </template>
  62. <script>
  63. import {
  64. uploadPerson,
  65. audioCreateVideo,
  66. textCreateVideo
  67. } from "@/api/system/user"
  68. import store from '@/store'
  69. const recorderManager = uni.getRecorderManager();
  70. const innerAudioContext = uni.createInnerAudioContext();
  71. innerAudioContext.autoplay = true;
  72. export default {
  73. data() {
  74. return {
  75. videoName: '',
  76. fileInfo: {},
  77. videoContent: [{
  78. value: 'text',
  79. text: '文字输入'
  80. },
  81. {
  82. value: 'record',
  83. text: '语音录入'
  84. },
  85. {
  86. value: 'audio',
  87. text: '音频选择'
  88. },
  89. ],
  90. current: 'text',
  91. imagePath: '',
  92. isRecording: false,
  93. audioPath: null,
  94. recorderManager: null,
  95. innerAudioContext: null,
  96. showPerson: false,
  97. timeStamp: null,
  98. audioAction: {
  99. method: 'pause'
  100. },
  101. videoText: '',
  102. missingContent: [],
  103. timbre: 'a',
  104. timbreContent: [{
  105. value: 'a',
  106. text: 'timbre1'
  107. },
  108. {
  109. value: 'b',
  110. text: 'timbre2'
  111. }
  112. ],
  113. personPath: '',
  114. createVideoPath: '',
  115. }
  116. },
  117. onLoad() {
  118. this.recorderManager = uni.getRecorderManager();
  119. this.innerAudioContext = uni.createInnerAudioContext();
  120. // 监听录音停止
  121. this.recorderManager.onStop((res) => {
  122. console.log('录音结束,临时路径:', res.tempFilePath);
  123. this.audioPath = res.tempFilePath; // 替换为新录音路径
  124. this.isRecording = false;
  125. });
  126. // 监听录音错误
  127. this.recorderManager.onError((err) => {
  128. uni.showToast({
  129. title: '录音出错',
  130. icon: 'none'
  131. });
  132. });
  133. },
  134. onShow() {
  135. this.timeStamp = new Date().getTime()
  136. this.fileInfo.path = uni.getStorageSync('filePath');
  137. this.fileInfo.name = uni.getStorageSync('fileName');
  138. if (this.fileInfo.name) {
  139. this.showPerson = true
  140. this.uploadAudioPath(this.fileInfo.path)
  141. }
  142. },
  143. methods: {
  144. chooseFile() {
  145. uni.navigateTo({
  146. url: "/pages/root-filelist/root-filelist",
  147. });
  148. },
  149. async chooseImage() {
  150. const that = this;
  151. uni.chooseImage({
  152. count: 1,
  153. sizeType: ['original', 'compressed'],
  154. sourceType: ['album', 'camera'],
  155. success: function(res) {
  156. that.imagePath = res.tempFilePaths[0];
  157. that.uploadImagePath()
  158. },
  159. fail: function(err) {
  160. uni.showToast({
  161. title: '图片选择失败',
  162. icon: 'none'
  163. });
  164. }
  165. });
  166. },
  167. radioChange(evt) {
  168. for (let i = 0; i < this.videoContent.length; i++) {
  169. if (this.videoContent[i].value === evt.detail.value) {
  170. this.current = i;
  171. break;
  172. }
  173. }
  174. },
  175. toggleRecording() {
  176. if (this.isRecording) {
  177. this.recorderManager.stop();
  178. this.uploadAudioPath(this.audioPath)
  179. } else {
  180. // 开始录音前,如果已有录音则清空
  181. if (this.audioPath) {
  182. this.audioPath = null;
  183. }
  184. // 开始录音
  185. this.recorderManager.start({
  186. duration: 600000, // 最大录音时间(毫秒)
  187. sampleRate: 44100,
  188. numberOfChannels: 1,
  189. encodeBitRate: 192000,
  190. format: 'wav',
  191. });
  192. this.isRecording = true;
  193. }
  194. },
  195. uploadImagePath() {
  196. let data = {
  197. file: this.imagePath
  198. }
  199. uploadPerson(data).then(response => {
  200. this.personPath = response.data.url
  201. })
  202. },
  203. uploadAudioPath(url) {
  204. let data = {
  205. file: url
  206. }
  207. uploadPerson(data).then(response => {
  208. this.personAudio = response.data.url
  209. })
  210. },
  211. jumpToList() {
  212. let missingFields = [];
  213. if (!this.videoName.trim()) {
  214. missingFields.push('视频名称');
  215. }
  216. if (!this.imagePath) {
  217. missingFields.push('人物形象');
  218. }
  219. const hasContent = this.videoText.trim() !== '' && this.timbre;
  220. const hasVoice = this.audioPath;
  221. if (!hasContent && !hasVoice) {
  222. missingFields.push('文字内容或语音输入');
  223. }
  224. if (missingFields.length > 0) {
  225. this.missingContent = `请确定:${missingFields.join('、')}是否填写完整!`
  226. this.$refs.alertDialog.open()
  227. return;
  228. }
  229. uni.navigateTo({
  230. url: 'list/creatingVideo/creatingVideo',
  231. });
  232. if (this.current == 'record' || this.current == 'audio') {
  233. let data = {
  234. 'persona_template': this.personPath,
  235. 'persona_audio': this.personAudio,
  236. }
  237. audioCreateVideo(data).then(response => {
  238. if (response.status == 200) {
  239. this.createVideoPath = response.data
  240. }
  241. }).catch(e => {
  242. uni.showToast({
  243. title: '视频返回成功'
  244. })
  245. })
  246. } else if (this.current == 'text') {
  247. let data = {
  248. 'persona_template': this.personPath,
  249. 'voice_type': this.timbre,
  250. 'audio_text': this.videoText,
  251. }
  252. textCreateVideo(data).then(response => {
  253. if (response.status == 200) {
  254. this.createVideoPath = response.data
  255. }
  256. })
  257. }
  258. },
  259. dialogClose() {
  260. console.log('点击关闭')
  261. },
  262. }
  263. }
  264. </script>
  265. <style scoped lang="scss">
  266. .content {
  267. display: flex;
  268. flex-direction: column;
  269. }
  270. .uni-margin-wrap {
  271. width: 690rpx;
  272. width: 100%;
  273. }
  274. .swiper {
  275. height: 300rpx;
  276. }
  277. .swiper-item {
  278. display: block;
  279. height: 300rpx;
  280. line-height: 300rpx;
  281. text-align: center;
  282. background-color: #BD3124;
  283. color: #fff;
  284. }
  285. .form-area {
  286. padding: 20px;
  287. .form-area-item {
  288. display: flex;
  289. flex-direction: row;
  290. margin-top: 30px;
  291. align-items: center;
  292. .form-area-item-text {
  293. width: 120px;
  294. }
  295. /deep/ uni-input {
  296. border: 1px solid #e5e5e5;
  297. border-radius: 4px;
  298. height: 30px;
  299. width: 200px;
  300. }
  301. /deep/ uni-picker {
  302. border-bottom: 1px solid #aaa;
  303. height: 30px;
  304. width: 200px;
  305. display: flex;
  306. align-items: center;
  307. }
  308. /deep/ uni-radio-group,
  309. /deep/ uni-label {
  310. display: flex;
  311. align-items: center;
  312. }
  313. /deep/ uni-label {
  314. margin-right: 10px;
  315. }
  316. /deep/ textarea {
  317. border: 1px solid #aaa;
  318. border-radius: 4px;
  319. width: 320px;
  320. padding: 5px;
  321. margin: 0 auto;
  322. }
  323. .none-file {
  324. margin-left: 10px;
  325. color: #aaa;
  326. }
  327. .file-name {
  328. margin-left: 10px;
  329. width: 50px;
  330. overflow: hidden;
  331. white-space: nowrap;
  332. text-overflow: ellipsis;
  333. }
  334. }
  335. }
  336. .btn-area {
  337. display: flex;
  338. justify-content: center;
  339. margin: 20px auto;
  340. width: 100%;
  341. }
  342. .audio-bar {
  343. text-align: center;
  344. display: block;
  345. margin-top: 10px;
  346. }
  347. /deep/ .uni-border-left {
  348. display: none;
  349. }
  350. </style>