uniapp 将流转化为视频并播放 微信小程序

该问题最关键的一步是将后端传回来的流,转化成视频,并播放。

1、从服务器请求数据流到前端

2、新建一个临时文件,将数据流写入这个文件里面,并自定义路径+命名。

复制代码
 const fs = wx.getFileSystemManager(); //获取全局唯一的文件管理器
          fs.writeFile({ })

3、可以将该文件保存到相册(这一步也可以不保存)

复制代码
 wx.saveVideoToPhotosAlbum({ // 新开页面打开文档
                filePath: wx.env.USER_DATA_PATH + "/myvideo.mp4",  //拿上面存入的文件路径
                success: function (res) {
                  
                }
              })

4、直接用video播放这个临时文件路径即可。

直接上代码:

复制代码
<video :src="src"></video>

                        uni.request({
                        url: 服务器地址,
                        method: 'GET',
                        responseType: 'arraybuffer',
                        header: {
                            Authorization: 'Bearer ' + user.DEFAULT_USER.access_token
                        },
                        success: res => {
                            const fs = wx.getFileSystemManager(); 
                               //获取全局唯一的文件管理器
                            fs.writeFile({ // 写文件
                                filePath: wx.env.USER_DATA_PATH + "/myvideo.mp4", 
                                // 指定临时文件存入的路径,后面字符串自定义
                                data: res.data,
                                encoding: "binary", //二进制流文件必须是 binary
                                success(res) {
                                    console.log('写入成功', res)
                                    this.src = wx.env.USER_DATA_PATH + "/myvideo.mp4"
                                    })
                                }
                            });
                        },
                    });
相关推荐
叱咤少帅(少帅)14 小时前
Uniapp开发pc端,小程序和APK
小程序·uni-app
2501_915918411 天前
iOS性能测试工具 Instruments、Keymob的使用方法 不局限 FPS
android·ios·小程序·https·uni-app·iphone·webview
2501_915918412 天前
iOS 混淆流程 提升 IPA 分析难度 实现 IPA 深度加固
android·ios·小程序·https·uni-app·iphone·webview
前端 贾公子2 天前
解决uni-app 输入框,键盘弹起时页面整体上移问题
前端·vue.js·uni-app
Muchen灬2 天前
【uniapp】(5) 创建gitee仓库并推送源码
gitee·uni-app
Muchen灬2 天前
【uniapp】(6) uniapp中使用vuex
uni-app
2501_915909062 天前
React Native 上架 App Store:项目运行与审核构建的流程
android·ios·小程序·https·uni-app·iphone·webview
李庆政3702 天前
uniapp+unicloud打包部署微信小程序
微信小程序·小程序·uni-app
2501_915909062 天前
苹果 Safari 浏览器抓包 页面刷新后的请求分析
android·前端·ios·小程序·uni-app·iphone·safari