openVideo.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * 视频:先下载远程地址到本地,再用系统默认应用打开;本地路径直接打开。
  3. */
  4. function openLocalVideoPath(filePath) {
  5. if (!filePath) return
  6. // #ifdef APP-PLUS
  7. try {
  8. if (typeof plus !== 'undefined' && plus.runtime && typeof plus.runtime.openFile === 'function') {
  9. plus.runtime.openFile(filePath, () => {
  10. uni.openDocument({
  11. filePath,
  12. fail: () => {
  13. uni.showToast({ title: '无法打开视频', icon: 'none' })
  14. }
  15. })
  16. })
  17. return
  18. }
  19. } catch (e) {
  20. // fall through
  21. }
  22. // #endif
  23. uni.openDocument({
  24. filePath,
  25. fail: () => {
  26. uni.showToast({ title: '无法打开视频', icon: 'none' })
  27. }
  28. })
  29. }
  30. /**
  31. * @param {string} pathOrUrl - 本地临时路径或 http(s) 地址
  32. */
  33. export function openVideoWithSystemPlayer(pathOrUrl) {
  34. if (!pathOrUrl) return
  35. const s = String(pathOrUrl).trim()
  36. if (/^https?:\/\//i.test(s)) {
  37. uni.showLoading({ title: '下载中', mask: true })
  38. uni.downloadFile({
  39. url: s,
  40. success: (res) => {
  41. if (res.statusCode === 200 && res.tempFilePath) {
  42. openLocalVideoPath(res.tempFilePath)
  43. } else {
  44. uni.showToast({ title: '下载失败', icon: 'none' })
  45. }
  46. },
  47. fail: () => {
  48. uni.showToast({ title: '下载失败', icon: 'none' })
  49. },
  50. complete: () => {
  51. uni.hideLoading()
  52. }
  53. })
  54. return
  55. }
  56. openLocalVideoPath(s)
  57. }