• Flutter高仿微信-第32篇-单聊-语音


    Flutter高仿微信系列共59篇,从Flutter客户端、Kotlin客户端、Web服务器、数据库表结构、Xmpp即时通讯服务器、视频通话服务器、腾讯云服务器全面讲解。

     详情请查看

    效果图:

    详情请参考 Flutter高仿微信-第29篇-单聊 , 这里只是提取语音聊天实现的部分代码。

    实现代码:

    //我的语言
    Widget meVoiceWidget(){
      return InkWell(
          onTap: () {
            widget.clickVoiceCallback(true);
            setState(() {
              widget.chatBean.isPlayVoice = true;
            });
            //点击语音
            AudioPlayer.getInstance().playLocal(widget.chatBean.voiceLocal??"", callback: (data){
              //录音回调
              setState(() {
                widget.chatBean.isPlayVoice = false;
              });
            });
          },
          child : Container(
            width: 120,
            height: 40,
            padding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 2.0),
            decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(1.0),),color: Color(0xFF9EEA6A),),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text("${widget.chatBean.second}''"),
                SizedBox(width: 4,),
                widget.chatBean.isPlayVoice?Image.asset("assets/chat/wn_chat_me_animator.gif", height: 24,):Image.asset("assets/chat/wn_chat_me_volume_3.png", height: 24,),
              ],
            ),
          )
      );
    }

    //朋友的语音
    Widget toVoiceWidget(){
      return InkWell(
          onTap: () {
            setState(() {
              widget.chatBean.isPlayVoice = true;
            });
            LogUtils.d("点击语音");
            AudioPlayer.getInstance().playLocal(widget.chatBean.voiceLocal??"", callback: (data){
              LogUtils.d("录音回调:${data}");
              setState(() {
                widget.chatBean.isPlayVoice = false;
              });
            });
          },
          child : Container(
            width: 120,
            height: 40,
            padding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 2.0),
            decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(5.0),),color: Color(0xFFEDEDED)),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                widget.chatBean.isPlayVoice?Image.asset("assets/chat/wn_chat_other_animator.gif", height: 34,):Image.asset("assets/chat/wn_chat_other_volume_3.png",  height: 34,),
                SizedBox(width: 4,),
                Text("${widget.chatBean.second}''"),
              ],
            ),
    
          )
      );
    }

    //发送多媒体(图片、语音、小视频)
    void _sendMedia(int type, String mediaURL, {int mediaSecond = 0, String messageId = ""}) async {
    
      bool isNetwork = await CommonNetwork.isNetwork();
      if(!isNetwork) {
        return;
      }
    
      bool deleteContacts = await isDeleteContacts(widget.account, widget.toChatId);
      if(deleteContacts){
        return;
      }
    
      //上传文件
      ChatBean serverChatBean;
      String message = "";
      ChatSendBean chatSendBean = ChatSendBean();
      chatSendBean.contentType = type;
      chatSendBean.messageId = messageId;
      chatSendBean.addTime = WnDateUtils.getCurrentTime();
      if(type == CommonUtils.CHAT_CONTENT_TYPE_VOICE){
        //语音
        serverChatBean = await UploadUtils.getInstance().uploadChatVoice(widget.account, widget.toChatId, mediaURL);
        chatSendBean.content = serverChatBean.voice??"";
        chatSendBean.second = mediaSecond;
      } else {
        return ;
      }
      message = jsonEncode(chatSendBean);
      _sendMessage(message);
    }

    接收语音:

    String serverVoicePath = CommonUtils.BASE_URL_UPLOAD + content;
    String localVoicePath = await FileUtils.getBaseFile("${DateUtil.getNowDateMs()}.mp3");
    //先下载语音
    await DownloadUtils.getInstance().downloadFile(serverVoicePath, localVoicePath);
    chatBean.voice = serverVoicePath;
    chatBean.voiceLocal = localVoicePath;
    chatBean.second = second;

    //通知栏提示
    NotificationUtils.getInstance().showNotification(chatBean);
    //插入本地数据库
    ChatRepository.getInstance().insertChat(chatBean);
    //刷新页面
    eventBus.emit(chatBean);

  • 相关阅读:
    NC Cloud uploadChunk文件上传漏洞复现
    【2014年数据结构真题】
    二叉树的三种层序遍历BFS
    C++程序结束
    计算机数制(进制转换,原码,反码,补码,真值)
    Leetcode刷题---两数之和
    nginx(三)实现反向代理客户端 IP透传
    GreenPlum DB向GBase迁移_TIMESTAM类型
    Android 图片上传
    web api前后分离开发时,jwt token无感刷新的实现
  • 原文地址:https://blog.csdn.net/maoning20080808/article/details/128012832