public void playVoice(String filePath) {
    if (!(new File(filePath).exists())) {
      return;
    }
    AudioManager audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);

    mediaPlayer = new MediaPlayer();
    if (EMChatManager.getInstance().getChatOptions().getUseSpeaker()) {
      audioManager.setMode(AudioManager.MODE_NORMAL);
      audioManager.setSpeakerphoneOn(true);
      mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
    } else {
      audioManager.setSpeakerphoneOn(false); // 关闭扬声器
      // 把声音设定成Earpiece(听筒)出来,设定为正在通话中
      audioManager.setMode(AudioManager.MODE_IN_CALL);
      mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
    }
    try {
      mediaPlayer.setDataSource(filePath);
      mediaPlayer.prepare();
      mediaPlayer.setOnCompletionListener(
          new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
              // TODO Auto-generated method stub
              mediaPlayer.release();
              mediaPlayer = null;
              stopPlayVoice(); // stop animation
            }
          });
      isPlaying = true;
      currentPlayListener = this;
      currentMessage = message;
      mediaPlayer.start();
      showAnimation();
      try {
        // 如果是接收的消息
        if (!message.isAcked && message.direct == EMMessage.Direct.RECEIVE) {
          message.isAcked = true;
          if (iv_read_status != null && iv_read_status.getVisibility() == View.VISIBLE) {
            // 隐藏自己未播放这条语音消息的标志
            iv_read_status.setVisibility(View.INVISIBLE);
            EMChatDB.getInstance().updateMessageAck(message.getMsgId(), true);
          }
          // 告知对方已读这条消息
          if (chatType != ChatType.GroupChat)
            EMChatManager.getInstance().ackMessageRead(message.getFrom(), message.getMsgId());
        }
      } catch (Exception e) {
        message.isAcked = false;
      }
    } catch (Exception e) {
    }
  }
 public void onReceive(Context context, Intent intent) {
   String msgid = intent.getStringExtra("msgid");
   String from = intent.getStringExtra("from");
   EMConversation conversation = EMChatManager.getInstance().getConversation(from);
   if (conversation != null) {
     EMMessage msg = conversation.getMessage(msgid);
     if (msg != null) {
       msg.isAcked = true;
     }
   }
   abortBroadcast();
 }
Example #3
0
 @Override
 public void onReceive(Context context, Intent intent) {
   abortBroadcast();
   String msgid = intent.getStringExtra("msgid");
   String from = intent.getStringExtra("from");
   EMConversation conversation = EMChatManager.getInstance().getConversation(from);
   if (conversation != null) {
     // 把message设为已读
     EMMessage msg = conversation.getMessage(msgid);
     if (msg != null) {
       // 2014-11-5 修复在某些机器上,在聊天页面对方发送已读回执时不立即显示已读的bug
       if (ChatActivity.activityInstance != null) {
         if (msg.getChatType() == ChatType.Chat) {
           if (from.equals(ChatActivity.activityInstance.getToChatUsername())) return;
         }
       }
       msg.isAcked = true;
     }
   }
 }