index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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="5000" :duration="1000">
  6. <swiper-item>
  7. <view class="swiper-item"><text>创建步骤1<br>填写视频名称</text></view>
  8. </swiper-item>
  9. <swiper-item>
  10. <view class="swiper-item">创建步骤2<br>选择个人形象照片,最好是正面照</view>
  11. </swiper-item>
  12. <swiper-item>
  13. <view class="swiper-item">创建步骤3<br>选择生成视频内容的方式:<br>若选择文字输入,则输入口播的文字内容及使用音色;<br>若选择语音录入则点击开始录音,录音结束后再点击停止录音,可在下方播放容器试听录音内容;<br>若选择音频选择,则选择本机的音频文件</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. // 选择音频文件
  145. chooseFile() {
  146. if(!this.imagePath) {
  147. uni.showToast({
  148. title: '请先选择形象照片',
  149. icon: 'none'
  150. });
  151. return
  152. }
  153. uni.navigateTo({
  154. url: "/pages/root-filelist/root-filelist",
  155. });
  156. },
  157. // 选择图片文件
  158. async chooseImage() {
  159. const that = this;
  160. uni.chooseImage({
  161. count: 1,
  162. sizeType: ['original', 'compressed'],
  163. sourceType: ['album', 'camera'],
  164. success: function(res) {
  165. that.imagePath = res.tempFilePaths[0];
  166. that.uploadImagePath()
  167. },
  168. fail: function(err) {
  169. uni.showToast({
  170. title: '图片选择失败',
  171. icon: 'none'
  172. });
  173. }
  174. });
  175. },
  176. // 开始/停止录音
  177. toggleRecording() {
  178. if (this.isRecording) {
  179. this.recorderManager.stop();
  180. this.uploadAudioPath(this.audioPath)
  181. } else {
  182. // 开始录音前,如果已有录音则清空
  183. if (this.audioPath) {
  184. this.audioPath = null;
  185. }
  186. // 开始录音
  187. this.recorderManager.start({
  188. duration: 600000, // 最大录音时间(毫秒)
  189. sampleRate: 44100,
  190. numberOfChannels: 1,
  191. encodeBitRate: 192000,
  192. format: 'wav',
  193. });
  194. this.isRecording = true;
  195. }
  196. },
  197. // 上传形象照片
  198. uploadImagePath() {
  199. let data = {
  200. file: this.imagePath
  201. }
  202. uploadPerson(data).then(response => {
  203. this.personPath = response.data.url
  204. })
  205. },
  206. // 上传音频文件
  207. uploadAudioPath(url) {
  208. let data = {
  209. file: url
  210. }
  211. uploadPerson(data).then(response => {
  212. this.personAudio = response.data.url
  213. })
  214. },
  215. // 点击生成视频,跳转生成页面
  216. jumpToList() {
  217. let missingFields = [];
  218. if (!this.videoName.trim()) {
  219. missingFields.push('视频名称');
  220. }
  221. if (!this.imagePath) {
  222. missingFields.push('人物形象');
  223. }
  224. const hasContent = this.videoText.trim() !== '' && this.timbre;
  225. const hasVoice = this.audioPath;
  226. if (!hasContent && !hasVoice) {
  227. missingFields.push('文字内容或语音输入');
  228. }
  229. if (missingFields.length > 0) {
  230. this.missingContent = `请确定:${missingFields.join('、')}是否填写完整!`
  231. this.$refs.alertDialog.open()
  232. return;
  233. }
  234. uni.navigateTo({
  235. url: 'list/creatingVideo/creatingVideo',
  236. });
  237. if (this.current == 'record' || this.current == 'audio') {
  238. let data = {
  239. 'persona_template': this.personPath,
  240. 'persona_audio': this.personAudio,
  241. }
  242. audioCreateVideo(data).then(response => {
  243. if (response.status == 200) {
  244. this.createVideoPath = response.data
  245. }
  246. }).catch(e => {
  247. uni.showToast({
  248. title: '视频返回成功'
  249. })
  250. })
  251. } else if (this.current == 'text') {
  252. let data = {
  253. 'persona_template': this.personPath,
  254. 'voice_type': this.timbre,
  255. 'audio_text': this.videoText,
  256. }
  257. textCreateVideo(data).then(response => {
  258. if (response.status == 200) {
  259. this.createVideoPath = response.data
  260. }
  261. })
  262. }
  263. },
  264. dialogClose() {
  265. console.log('点击关闭')
  266. },
  267. }
  268. }
  269. </script>
  270. <style scoped lang="scss">
  271. .content {
  272. display: flex;
  273. flex-direction: column;
  274. }
  275. .uni-margin-wrap {
  276. width: 690rpx;
  277. width: 100%;
  278. }
  279. .swiper {
  280. height: 350rpx;
  281. }
  282. .swiper-item {
  283. display: block;
  284. height: 350rpx;
  285. text-align: center;
  286. background-color: #BD3124;
  287. color: #fff;
  288. padding-top:30px;
  289. }
  290. .form-area {
  291. padding: 20px;
  292. .form-area-item {
  293. display: flex;
  294. flex-direction: row;
  295. margin-top: 30px;
  296. align-items: center;
  297. .form-area-item-text {
  298. width: 120px;
  299. }
  300. /deep/ uni-input {
  301. border: 1px solid #e5e5e5;
  302. border-radius: 4px;
  303. height: 30px;
  304. width: 200px;
  305. }
  306. /deep/ uni-picker {
  307. border-bottom: 1px solid #aaa;
  308. height: 30px;
  309. width: 200px;
  310. display: flex;
  311. align-items: center;
  312. }
  313. /deep/ uni-radio-group,
  314. /deep/ uni-label {
  315. display: flex;
  316. align-items: center;
  317. }
  318. /deep/ uni-label {
  319. margin-right: 10px;
  320. }
  321. /deep/ textarea {
  322. border: 1px solid #aaa;
  323. border-radius: 4px;
  324. width: 320px;
  325. padding: 5px;
  326. margin: 0 auto;
  327. }
  328. .none-file {
  329. margin-left: 10px;
  330. color: #aaa;
  331. }
  332. .file-name {
  333. margin-left: 10px;
  334. width: 50px;
  335. overflow: hidden;
  336. white-space: nowrap;
  337. text-overflow: ellipsis;
  338. }
  339. }
  340. }
  341. .btn-area {
  342. display: flex;
  343. justify-content: center;
  344. margin: 20px auto;
  345. width: 100%;
  346. }
  347. .audio-bar {
  348. text-align: center;
  349. display: block;
  350. margin-top: 10px;
  351. }
  352. /deep/ .uni-border-left {
  353. display: none;
  354. }
  355. </style>