choose-file.uvue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex: 1;">
  4. <!-- #endif -->
  5. <view>
  6. <page-head :title="title"></page-head>
  7. <!-- #ifdef APP-IOS -->
  8. <view style="padding-left: 20px; padding-right: 20px;">
  9. <text style="color: #353535; font-size: 15px;">
  10. iOS 调试首先需要添加对应 audio、video、image 存储到文件系统中,步骤如下:\n
  11. 1. 从系统相册或者语音备忘录中选择具体的video、image、audio;\n
  12. 2. 点击 ‘分享’ 按钮;\n
  13. 3. 下滑点击 ’存储到”文件“‘按钮;\n
  14. </text>
  15. </view>
  16. <!-- #endif -->
  17. <button size="mini" @click="log=''">清空日志</button>
  18. <text style="margin: 2px; padding: 2px; border: 1px solid #000000;">{{ log }}</text>
  19. <button class="btnstyle" type="primary" @tap="chooseVideo">选择文件(video) 单选</button>
  20. <button class="btnstyle" type="primary" @tap="chooseVideoMul">选择文件(video) 多选</button>
  21. <button class="btnstyle" type="primary" @tap="playVideo">选择文件(video)并播放</button>
  22. <video class="video" :src="src" :controls="true" :autoplay="true" :loop="true">111</video>
  23. <button class="btnstyle" type="primary" @tap="chooseImage">选择文件(image) 单选</button>
  24. <button class="btnstyle" type="primary" @tap="chooseImageMul">选择文件(image) 多选</button>
  25. <button class="btnstyle" type="primary" @tap="viewImg">选择文件(image) 并预览</button>
  26. <button class="btnstyle" type="primary" @tap="chooseAudio">选择文件(audio) 单选</button>
  27. <button class="btnstyle" type="primary" @tap="chooseAudioMul">选择文件(audio) 多选</button>
  28. <button class="btnstyle" type="primary" @tap="playAudio">选择文件(audio) 并播放</button>
  29. <button class="btnstyle" type="primary" @tap='chooseAll'>选择文件(all) 单选</button>
  30. <button class="btnstyle" type="primary" @tap='chooseAllMul'>选择文件(all) 多选</button>
  31. <view style="height: 4px;"></view>
  32. </view>
  33. <!-- #ifdef APP -->
  34. </scroll-view>
  35. <!-- #endif -->
  36. </template>
  37. <script>
  38. export default {
  39. data() {
  40. return {
  41. log: '',
  42. title: 'choose-file',
  43. src: '',
  44. _audioContext: null as InnerAudioContext | null,
  45. }
  46. },
  47. unmounted() {
  48. if (this._audioContext != null) {
  49. this._audioContext!.destroy()
  50. }
  51. },
  52. methods: {
  53. getPath(chooseFils : ChooseFileTempFile[]) : string {
  54. var urls = new Array<string>()
  55. chooseFils.forEach(value => {
  56. urls.push(value.path)
  57. })
  58. return urls.join(', ')
  59. },
  60. chooseVideo() {
  61. uni.chooseFile({
  62. type: 'video',
  63. count: 1,
  64. success: (res) => {
  65. console.log(res)
  66. if (res.tempFiles.length > 0) {
  67. this.log += this.getPath(res.tempFiles) + '\n\n'
  68. }
  69. },
  70. complete: (res) => {
  71. console.log(res)
  72. }
  73. })
  74. },
  75. chooseVideoMul() {
  76. uni.chooseFile({
  77. type: 'video',
  78. success: (res) => {
  79. console.log(res)
  80. if (res.tempFiles.length > 0) {
  81. this.log += this.getPath(res.tempFiles) + '\n\n'
  82. }
  83. },
  84. complete: (res) => {
  85. console.log(res)
  86. }
  87. })
  88. },
  89. playVideo() {
  90. uni.chooseFile({
  91. type: 'video',
  92. count: 1,
  93. success: (res) => {
  94. console.log(res)
  95. if (res.tempFiles.length > 0) {
  96. this.src = res.tempFiles[0].path
  97. this._audioContext?.destroy()
  98. this._audioContext = null
  99. }
  100. },
  101. complete: (res) => {
  102. console.log(res)
  103. }
  104. })
  105. },
  106. chooseImage() {
  107. uni.chooseFile({
  108. type: 'image',
  109. count: 1,
  110. success: (res) => {
  111. console.log(res)
  112. if (res.tempFiles.length > 0) {
  113. this.log += this.getPath(res.tempFiles) + '\n\n'
  114. }
  115. },
  116. complete: (res) => {
  117. console.log(res)
  118. }
  119. })
  120. },
  121. chooseImageMul() {
  122. uni.chooseFile({
  123. type: 'image',
  124. count: 90,
  125. success: (res) => {
  126. console.log(res)
  127. if (res.tempFiles.length > 0) {
  128. this.log += this.getPath(res.tempFiles) + '\n\n'
  129. }
  130. },
  131. complete: (res) => {
  132. console.log(res)
  133. }
  134. })
  135. },
  136. viewImg() {
  137. uni.chooseFile({
  138. type: 'image',
  139. success: (res) => {
  140. console.log(res)
  141. if (res.tempFiles.length > 0) {
  142. const tempFiles: Array<string> = res.tempFiles.map((value) => {
  143. return value.path
  144. })
  145. uni.previewImage({
  146. current: 0,
  147. urls: tempFiles,
  148. success: (res) => {
  149. console.log(res)
  150. },
  151. fail: (err) => {
  152. console.log(err)
  153. }
  154. })
  155. }
  156. },
  157. complete: (res) => {
  158. console.log(res)
  159. }
  160. })
  161. },
  162. chooseAudio() {
  163. uni.chooseFile({
  164. type: 'audio',
  165. count: 1,
  166. success: (res) => {
  167. console.log(res)
  168. if (res.tempFiles.length > 0) {
  169. this.log += this.getPath(res.tempFiles) + '\n\n'
  170. }
  171. },
  172. complete: (res) => {
  173. console.log(res)
  174. }
  175. })
  176. },
  177. chooseAudioMul() {
  178. uni.chooseFile({
  179. type: 'audio',
  180. success: (res) => {
  181. console.log(res)
  182. if (res.tempFiles.length > 0) {
  183. this.log += this.getPath(res.tempFiles) + '\n\n'
  184. }
  185. },
  186. complete: (res) => {
  187. console.log(res)
  188. }
  189. })
  190. },
  191. playAudio() {
  192. uni.chooseFile({
  193. type: 'audio',
  194. count: 1,
  195. success: (res) => {
  196. console.log(res);
  197. if (res.tempFiles.length > 0) {
  198. if (this._audioContext == null) {
  199. this.src = ''
  200. this._audioContext = uni.createInnerAudioContext()
  201. this._audioContext!.autoplay = true
  202. } else if (!this._audioContext!.paused) {
  203. this._audioContext!.stop()
  204. }
  205. this._audioContext!.src = res.tempFiles[0].path
  206. }
  207. },
  208. complete: (res) => {
  209. console.log(res);
  210. }
  211. });
  212. },
  213. chooseAll() {
  214. uni.chooseFile({
  215. type: 'all',
  216. count: 1,
  217. success: (res) => {
  218. console.log(res)
  219. if (res.tempFiles.length > 0) {
  220. this.log += this.getPath(res.tempFiles) + '\n\n'
  221. }
  222. },
  223. complete: (res) => {
  224. console.log(res)
  225. }
  226. })
  227. },
  228. chooseAllMul() {
  229. uni.chooseFile({
  230. type: 'all',
  231. success: (res) => {
  232. console.log(res)
  233. if (res.tempFiles.length > 0) {
  234. this.log += this.getPath(res.tempFiles) + '\n\n'
  235. }
  236. },
  237. complete: (res) => {
  238. console.log(res)
  239. }
  240. })
  241. }
  242. }
  243. }
  244. </script>
  245. <style>
  246. .btnstyle {
  247. margin: 4px;
  248. }
  249. .video {
  250. width: 100%;
  251. height: 225px;
  252. }
  253. .uni-uploader__input-box {
  254. margin: 5px;
  255. width: 104px;
  256. height: 104px;
  257. border: 1px solid #D9D9D9;
  258. }
  259. </style>