uniapp内把视频mp4的base64保持到手机文件系统

我们用了uniapp中的webview实现调用摄像头视频流并显示界面,webview引用的界面地址是同一个项目中的html界面,这个界面放了video,显示视频,vue界面中引用此webview。webview和vue界面通讯传参也成了问题,在录制视频保存时,也是遇到了很大挑战。这篇博客只记录了视频base64保存安卓手机的代码。

javascript 复制代码
//调用示例
var fileFullName = '/storage/emulated/0/video_' + that.getFormattedDate() + '.mp4';
that.base64ToFile(base64String,fileFullName);


//去掉头部标识后的,base64字符串转byteArray
function base64ToByteArray(base64Str) {
		console.log(base64Str.length)
		const binaryString = atob(base64Str);
		const uint8Array = new Uint8Array(binaryString.length);

		for (let i = 0; i < binaryString.length; i++) {
			uint8Array[i] = binaryString.charCodeAt(i);
		}
		let arr = []
		Array.from(uint8Array).map(num => {
			arr.push(num >= 128 ? (num - 256) : num)
		})
		return arr;
},
//获取日期的年月日时分秒
function getFormattedDate() {
    const now = new Date();
    const year = now.getFullYear();        // 获取年份
    const month = String(now.getMonth() + 1).padStart(2, '0');  // 获取月份,注意月份是从0开始的,因此需要加1,且格式化为两位数
    const day = String(now.getDate()).padStart(2, '0');         // 获取日期,格式化为两位数
    const hours = String(now.getHours()).padStart(2, '0');      // 获取小时,格式化为两位数
    const minutes = String(now.getMinutes()).padStart(2, '0');  // 获取分钟,格式化为两位数
    const seconds = String(now.getSeconds()).padStart(2, '0');  // 获取秒数,格式化为两位数

    // 拼接为所需格式:YYYYMMDDHHmmss
    return `${year}${month}${day}${hours}${minutes}${seconds}`;
},
/**  
* base64字符串转成文件保存手机文件系统
 * @param {String} base64Str // 允许包含前缀,可以是图片、视频 base64
 * @param {String} fileName // 文件名称:video.mp4、a.jpeg
 */ 
function base64ToLocalFile (base64Str, fullPath) {
		var that = this;
		// 去除base64前缀
		var index=base64Str.indexOf(',')  
		var base64Str=base64Str.slice(index+1,base64Str.length)
		console.log(base64Str.length)
		
		plus.io.requestFileSystem(plus.io.PRIVATE_DOC,function(fs){  
			console.log(fullPath)
			
			let platform = uni.getSystemInfoSync().platform
			if(platform == 'android'){    
				
				var FileOutputStream = plus.android.importClass("java.io.FileOutputStream");  
				try{
					var out = new FileOutputStream(fullPath);  
					let bytes = that.base64ToByteArray(base64Str);
					console.log(bytes.length)
					console.log(fullPath)
					out.write(bytes);   
					out.close();
					console.log('写手机文件系统完成')
				}catch(e){  
					console.log(e.message);  
				}
			}else if(platform == 'ios'){  
				var NSData = plus.ios.importClass('NSData');  
				var nsData = new NSData();  
				nsData = nsData.initWithBase64EncodedStringoptions(base64Str,0);  
				if (nsData) {  
					nsData.plusCallMethod({writeToFile: fullPath,atomically:true});  
					plus.ios.deleteObject(nsData);  
				}  
				
			}
		})
	}

参考:

原博主:uniapp中图片视频的base64保存手机上

原博主:base64遇到大文件处理方法

相关推荐
耶啵奶膘4 小时前
uniapp+firstUI——上传视频组件fui-upload-video
前端·javascript·uni-app
耶啵奶膘18 小时前
uniapp——地图路线绘制map
uni-app
shadouqi18 小时前
uniapp实现图片预览,懒加载,下拉刷新等
uni-app
走,带你去玩19 小时前
uniapp 微信小程序水印
微信小程序·小程序·uni-app
菌菇汤21 小时前
uni-app实现单选,多选也能搜索,勾选,选择,回显
前端·javascript·vue.js·微信小程序·uni-app·app
某公司摸鱼前端1 天前
uniapp socket 封装 (可拿去直接用)
前端·javascript·websocket·uni-app
遗憾随她而去.1 天前
uniapp 中使用路由导航守卫,进行登录鉴权
前端·uni-app
牧杉-惊蛰1 天前
uniapp微信小程序css中background-image失效问题
css·微信小程序·uni-app
Little_Code2 天前
uniapp 使用ffmpeg播放rtsp
ffmpeg·uni-app·rtsp
儿歌八万首2 天前
uniapp 和原生插件交互
uni-app·交互