private void RecieveAudioMessage(AudioBody body) { byte[] bytes = SdkUtils.DownloadThumbnail(body.url, body.secret, conn.TokenData.access_token); string fileTempDir = SdkUtils.GetRevFileTempDir(); File.WriteAllBytes(fileTempDir + "\\" + body.filename, bytes); string wavFile = AudioConverter.ConvertToWave(fileTempDir, body.filename, Path.GetFileNameWithoutExtension(body.filename) + ".wav"); if (!File.Exists(wavFile)) { return; } chatBrower.AppendLeftHtml(toUserName, "收到音频"); //AppendAudioTag(null); PlayAudio(wavFile); }
/// <summary> /// 发送文件 /// </summary> /// <param name="toUser">发给谁</param> /// <param name="postedfile">已经上传的文件返回信息</param> public void SendFile(string toUser, PostedFileResp postedfile) { Jid to = new Jid(BuildJid(toUser)); Jid from = new Jid(BuildJid(UserName)); BodyBase[] bodies = new BodyBase[postedfile.entities.Length]; // 构建发送文件的 message 消息 for (int i = 0; i < postedfile.entities.Length; i++) { PostFileEntity entity = postedfile.entities[i]; // 文件类型 img audio string otype = SdkUtils.GetFileType(entity.filename); // 文件的url string ourl = postedfile.uri + "/" + entity.uuid; string osecret = entity.share_secret; string ofilename = entity.filename; /* * 传图片 * ReceivedData: * <message xmlns='jabber:client' from='easemob-demo#[email protected]/webim' * to='easemob-demo#[email protected]' id='124420481838219668' type='chat'> * <body> * {"from":"weigang75","to":"march3","bodies": * [{"type":"img","url":"https://a1.easemob.com/easemob-demo/chatdemoui/chatfiles/cd6f8050-81f7-11e5-a16a-05187e341cb0", * "secret":"zW-AWoH3EeWmJevV5n4Fpkxnnu3e5okMLIhENE0QHaZbvqg5", * "filename":"原生+自定义.jpg", * "thumb":"https://a1.easemob.com/easemob-demo/chatdemoui/chatfiles/cd6f8050-81f7-11e5-a16a-05187e341cb0", * "thumb_secret":"","size":{"width":952,"height":671}}],"ext":{}}</body></message> * * 传语音 * ReceivedData: * <message xmlns='jabber:client' from='easemob-demo#[email protected]/webim' * to='easemob-demo#[email protected]' id='124421298246910356' type='chat'> * <body> * {"from":"weigang75","to":"march3","bodies": * [{"type":"audio", * "url":"https://a1.easemob.com/easemob-demo/chatdemoui/chatfiles/3ec2bb50-81f8-11e5-8e7c-1fa6315dec2d", * "secret":"PsK7WoH4EeWwmIkeyVsexnkK-Rmqu2X_N2qqK9FQYmUkko8W", * "filename":"环信通讯 - 副本.mp3", * "length":3}],"ext":{}}</body></message> * */ // 如果是文件,需要多一些字段 thumb、thumb_secret、size if ("img".Equals(otype)) { bodies[i] = new ImageBody { type = otype, url = ourl, secret = osecret, filename = ofilename, thumb = ourl, thumb_secret = "", size = new ImageSize { width = entity.imageSize.Width, height = entity.imageSize.Height } }; } else if ("audio".Equals(otype)) { bodies[i] = new AudioBody { type = otype, url = ourl, secret = osecret, filename = ofilename }; } else { bodies[i] = new FileBody { type = otype, url = ourl, secret = osecret, filename = ofilename }; } } MsgData data = new MsgData() { from = UserName, to = toUser, bodies = bodies, ext = new { } }; WSMessage msgNode = new WSMessage(to, from); msgNode.Type = MessageType.chat; msgNode.SetBodyData(data); Send(msgNode); }