微信小程序语音(A)发给别人(B),也能播放,是需要先把语音上传到自己的服务器上才可以
小程序语音(A)发给别人(B),也能播放,是需要先把语音上传到自己的服务器上才可以。
https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/InnerAudioContext.html
A端:
Wxml:
<button wx:if="{{selected}}" bind:touchstart="start" bind:touchend="stop" data-index="{{index}}" data-memberid="{{item.memberid}}">
按住回答
</button>
Js:
定义公用语音包组件
const recorderManager = wx.getRecorderManager();
const innerAudioContext = wx.createInnerAudioContext();
//开始录音的时候
start: function() {
const options = {
duration: 10000, //指定录音的时长,单位 ms
sampleRate: 16000, //采样率
numberOfChannels: 1, //录音通道数
encodeBitRate: 96000, //编码码率
format: 'mp3', //音频格式,有效值 aac/mp3
frameSize: 50, //指定帧大小,单位 KB
}
//开始录音
recorderManager.start(options);
recorderManager.onStart(() => {
console.log('开始录音')
});
//错误回调
recorderManager.onError((res) => {
console.log(res);
})
},
//停止录音
stop: function(e) {
var that = this;
var memberid= e.currentTarget.dataset.memberid;
recorderManager.stop();
recorderManager.onStop((res) => {
var list = that.data.vediolist;
var start = this.data.start;
var end = e.timeStamp;
var seconds = (end % (1000 * 60)) / 1000;
var shijian = seconds - start;
//这里也能按照时间显示留言宽度,暂时没做
var src = res.tempFilePath; //语音临时文件
//先把语音上传到自己的服务器上才可以,然后把返回的语音放入数组存储,最后在一起存到数据库中的字段里
wx.uploadFile({
url: hcxcx.apiUrl.getuploadvedio,
filePath: src,
header: {
'content-type': 'multipart/form-data'
},
name: "files",
formData: {
user: 'tesdt'
},
success: function(res) {
if (res.statusCode == 200) {
var aa = {
src: res.data,
memberid: memberid
}
list.push(aa);
that.setData({
vediolist: list
})
} else {
wx.showToast({
title: res.body,
icon: 'none',
duration: 2000
});
}
},
fail: function(res) {
wx.showToast({
title: res.body,
icon: 'none',
duration: 2000
});
}
});
})
},
C#端
#region 小程序录音上传
/// <summary>
/// 小程序录音上传
/// </summary>
/// <param name="_"></param>
/// <returns></returns>
[HttpPost]
public ActionResult GetUploadVedio()
{
string ImgId = string.Empty;
string webRootPath = _hostingEnvironment.WebRootPath;
var hp = _accessor.HttpContext.Request.Form.Files[0];//和前面input的name决定这个参数
string filemd5key = Guid.NewGuid().ToString("N");
try
{
var datetimename = DateTime.Now.ToString("yyyyMMdd");
var filedic = "ResourceTemp/Vedio/" + datetimename;
string filePath = webRootPath + "\\ResourceTemp\\Vedio\\" + datetimename;
//图片默认保存路径和文件路径是不一样的
//保存文件到层级目录下
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
filePath = filePath + "\\" + filemd5key + Path.GetExtension(hp.FileName);
using (FileStream fs = System.IO.File.Create(filePath))
{
hp.CopyTo(fs);
fs.Flush();
}
byte[] buffer = null;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
}
var url = ConfigUtil.GetStr("ApiDomain");
if (!url.Contains("http"))
{
url = "http://" + url;
}
url = url + "/";
ImgId = url + filedic + "/" + filemd5key + Path.GetExtension(hp.FileName);
}
catch (Exception ex)
{
NlogUtil.wlog("GetUploadVedio", "小程序录音上传错误信息:" + ex.Message);
}
return Content(ImgId);
}
#endregion
B端
Wxml:
<view class="yuyin" bindtap="vedioplay" data-vediosrc='{{vediosrc}}'>
<image src='./../../../centent/img/yuyinshuru.png' class="yuyin_img"></image>
</view>
Js
const innerAudioContext = wx.createInnerAudioContext();
vedioplay: function(e) {
let that = this;
var src = e.currentTarget.dataset.vediosrc;
innerAudioContext.autoplay = true;
innerAudioContext.src = src;
innerAudioContext.onPlay(() => {
console.log('历史记录播放');
})
innerAudioContext.obeyMuteSwitch = false
innerAudioContext.onError((res) => {
console.log(res.errMsg)
console.log(res.errCode)
})
}
微信小程序语音(A)发给别人(B),也能播放,是需要先把语音上传到自己的服务器上才可以的更多相关文章
- 微信小程序开发学习资料
作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- 微信小程序学习指南
作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- 微信小程序踩坑集合
1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=1476434678461 2:简易教程:https://mp.weixin.qq.com/debu ...
- 微信小程序开发初体验--教你开发小程序
微信小程序 微信小程序面世以来受到的关注颇多,直到最近我才动手尝试进行了小程序的开发,总体上感觉还是不错的,有一点不适应的就是要摆脱Web APP开发对DOM的操作.在这里我就把我是如何利用API开发 ...
- 微信小程序开发带来的思考
若无小程序开发经验,可先阅读 玩转微信小程序 一文. 微信小程序正式上线已有几周时间,相信它的开发模式你已烂熟于胸,可能你也有所疑问,我竟能用 web 语言开发出如此流畅的几乎原生体验的应用.可能你又 ...
- [干货教程]仿网易云课堂微信小程序开发实战经验
本篇文章想跟大家分享下:我们公司“湖北诚万兴科技”最近刚帮客户定制开发.目前已上线的“哎咆课堂”微信小程序的开发经验分享.首先大概介绍下这个小程序所涉及到的主要技术点:微信登录.微信支付.微信小程序F ...
- 微信小程序开发环境搭建
关注,QQ群,微信应用号社区 511389428 微信小程序可谓是今天最火的一个名词了,一经出现真是轰炸了整个开发人员,当然很多App开发人员有了一个担心,微信小程序的到来会不会给移动端App带来一个 ...
- 如何一键式搭建微信小程序
有了微信小程序,对你到底意味着什么? 对于用户来说,再也不用担心手机的内存不够用了!一个小程序只有1M,随便卸载一个App,就能安装很多小程序! 对于老板来说,你不再需要花费数十万来去请外包公司帮你去 ...
- 微信小程序“满月”:尝鲜之后你还用过它吗?
距离 2017 年 1 月 9 日微信小程序上线,整整过去了一个月时间.和互联网时代每天出现的众多新鲜事物相似,小程序甫一诞生,立即占据了各大科技媒体网站头屏并引起社交圈的兴奋讨论.由于背靠微信,纷纷 ...
随机推荐
- 下载安装charles并导入证书、使用
抓包原理 1.截获真实客户端的 HTTPS请求,伪装客户端向真实服务端发送 HTTPS 请求. 2.接受真实服务器响应,用 Charles 自己证书伪装服务端向真实客户端发送内容 3.证书导入 看博客 ...
- Spark以yarn方式运行时抛出异常
Spark以yarn方式运行时抛出异常: cluster.YarnClientSchedulerBackend: Yarn application has already exited with st ...
- 4 JavaScript异常&debugger&保留关键字
try:语句测试代码块错误 catch:语句处理错误,一般提供一个对象如catch(err)用来存储错误信息 throw: 语句创建自定义错误,抛出的信息可以被catch捕获 JavaScript错误 ...
- typescript 起步之安装及配置 ts-node 环境变量
最近vue 3.0 版本发布,让我认识到 typescript 将占有越来越重要的地位,所以我也开启了typescript学习之旅. 要想编写第一个 hello typescript 程序,当然要经过 ...
- spring boot 整合 Camunda
官网:https://camunda.com/ 论坛:https://forum.camunda.org/ 一. 创建 spring boot 项目,添加项目依赖 <?xml version=& ...
- Atcoder Grand Contest 037B(DP,组合数学,思维)
#include<bits/stdc++.h>using namespace std;const long long mod = 998244353;string s;int a[3000 ...
- axios发送post请求[body-parser]--['Content-type': 'application/x-www-form-urlencoded']
const express = require('express') const axios = require('axios') const bodyParser = require('body-p ...
- Codeforces1307D. Cow and Fields
对于本题,最短路,考虑bfs,那么我们可以跑2次bfs,求出每个点到1与n的最短路,设为x_a, x_b,那我们可以把问题转换成max(min{x_a+y_b,x_b+y_a}+1)(x,y属于1到n ...
- 操作Easy_UI案例以及模板
操作easy_ui案例以及模板 https://pan.baidu.com/s/1dHfclwP 密码:jygk
- C. Magic Grid 构造矩阵
C. Magic Grid time limit per test 1 second memory limit per test 256 megabytes input standard input ...