android使用easeui3.0以上版本,怎么实现未读语音自动播放

点击对方(群成员随意)一个语音、后面的未读语音会自动播放、仿微信。求解、
已邀请:

wjgso

赞同来自:

​搞了快一周了,终于搞好了。给自己的提问回答上,填坑!。。。。(基于easeui3.3.1版本,对方语音未读连播功能)
  1. 在easeui的EaseChatVoicePresenter里写一个接口的,在播放完成后调用、关键代码如下
    // 写一个构造方法、把EaseChatFragment传进来、等下就是在这个页面监听的//、它(EaseChatFragment)的儿子也是可以的。public EaseChatVoicePresenter(EaseChatFragment fragment){        setOnVoicePlayOverListener((OnVoicePlayOverListener) fragment);    }private void playVoice(final EMMessage msg) {        voicePlayer.play(msg, (EaseChatRowVoice) getChatRow(), new MediaPlayer.OnCompletionListener() {            @Override            public void onCompletion(MediaPlayer mp) {                // 我这里将取消播放动画放到了EaseChatRowVoicePlayer里了、voicePlayer.play里面加个参数                // 传 (EaseChatRowVoice) getChatRow()                // 原因等下解释                if (onVoicePlayOverListener != null) {                    onVoicePlayOverListener.onVoicePlayOver(msg);                }            }        });    }private OnVoicePlayOverListener onVoicePlayOverListener;public interface OnVoicePlayOverListener{    void onVoicePlayOver(EMMessage message);}private void setOnVoicePlayOverListener(OnVoicePlayOverListener onVoiceListener){    onVoicePlayOverListener = onVoiceListener;}// 这里注释掉即可,删掉也行(那就要到EaseChatRow中把重写的方法和接口方法删除即可)    // 作用是,不怎么了解,这里是为了语音在播放时、滑动屏幕就暂停了。看个人需求    // 对于这方法 有需要了解的自行百度。    @Override    public void onDetachedFromWindow() {        super.onDetachedFromWindow();//        if (voicePlayer.isPlaying()) {//            voicePlayer.stop();//        }    }

beyond - imgeek运营

赞同来自:

这个得自己搞吧,之前看别人实现发过

beyond - imgeek运营

赞同来自:

wjgso

赞同来自:

2. 一般都是写个fragment(我这里是ChatFragment)继承EaseChatFragment来实现聊天页面监听等等、我们这里实现之前写的接口
public class ChatFragment extends EaseChatFragment implements EaseChatFragment.EaseChatFragmentHelper,EaseChatVoicePresenter.OnVoicePlayOverListener // (点击对方语音播放(播放自己语音不继续播放对方未读语音,仿微信),该语音后面的未读语音list) private List<EMMessage> unListenedList = new ArrayList<>(); @Override public void onVoicePlayOver(EMMessage message) { checkVoice(message); } private void checkVoice(EMMessage message){ // conversation 是EaseChatFragment中的当前回话 List<EMMessage> list = conversation != null ? conversation.getAllMessages() : null; if (list != null) { int j = list.indexOf(message); for (int i = j; i < list.size();i++){ if (list.get(i).getType() == EMMessage.Type.VOICE && !list.get(i).isListened() && list.get(i).getUserName().equals(list.get(i).getFrom())){ unListenedList.add(list.get(i)); } } } if (unListenedList.size() > 0){ int k = list.indexOf(unListenedList.get(0)); if (getViewByPosition(k,listView) != null){ // 这里getTag()是关键、找个好久才发现、EaseMessageAdapter在创建View的时候都给每行的Presenter赋给了View的Tag // 差不多在EaseMessageAdapter 273行。 if (getViewByPosition(k,listView).getTag() instanceof EaseChatVoicePresenter){ ((EaseChatVoicePresenter) getViewByPosition(k,listView).getTag()).onBubbleClick(unListenedList.get(0)); } } } unListenedList.clear(); } // 获取ListView中子控件,这里遇到个坑、之前直接用getChildAt方法获取 // 只能获取ListView当前可见的、比如滑动到某个位置显示在屏幕中第一个position就是0 依次加 // 直到最后一个显示完全的那个。再次滑动position就再次改变了。 private View getViewByPosition(int pos, ListView listView) { final int firstListItemPosition = listView.getFirstVisiblePosition(); final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1; if (pos < firstListItemPosition || pos > lastListItemPosition) { return listView.getAdapter().getView(pos, null, listView); } else { int childIndex = pos - firstListItemPosition; return listView.getChildAt(childIndex); } }
3. 然后就是为了给每条语音播放结束都可以在自己的聊天页面(ChatFragment)监听到
找到EaseMessageAdapter类、给构造方法里加个EaseChatFragment 关键代码
private EaseChatFragment fragment; //这里加个 EaseChatFragment public EaseMessageAdapter(Context context, String username, int chatType, ListView listView, EaseChatFragment fragment) { this.context = context; this.listView = listView; toChatUsername = username; this.fragment = fragment; this.conversation = EMClient.getInstance().chatManager().getConversation(username, EaseCommonUtils.getConversationType(chatType), true); } // 再到createChatRowPresenter方法中case VOICE:这里 (251行) // 构造方法里加上fragment 不加应该是报错的 case VOICE: presenter = new EaseChatVoicePresenter(fragment); break;
下面就是在EaseChatMessageList中的init()方法62行中加上EaseChatFragment参数 然后再给 EaseMessageAdapter public void init(String toChatUsername, int chatType, EaseCustomChatRowProvider customChatRowProvider, EaseChatFragment fragment) { this.chatType = chatType; this.toChatUsername = toChatUsername; conversation = EMClient.getInstance().chatManager().getConversation(toChatUsername, EaseCommonUtils.getConversationType(chatType), true); messageAdapter = new EaseMessageAdapter(context, toChatUsername, chatType, listView,fragment); messageAdapter.setItemStyle(itemStyle); messageAdapter.setCustomChatRowProvider(customChatRowProvider); // set message adapter listView.setAdapter(messageAdapter); refreshSelectLast(); }
再来就给EaseChatFragment中的onMessageListInit()里的messageList.init()里加上this
(如果继承的重写了onMessageListInit()里面也是加this)
protected void onMessageListInit(){ messageList.init(toChatUsername, chatType, chatFragmentHelper != null ? chatFragmentHelper.onSetCustomChatRowProvider() : null,this); setListItemClickListener(); messageList.getListView().setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { hideKeyboard(); inputMenu.hideExtendMenuContainer(); return false; } }); isMessageListInited = true; }
4. 取消播放的动画放在EaseChatRowVoicePlayer
给stop方面添加个boolean类型的来判断是否要被聊天页面监听
public void play(final EMMessage msg,EaseChatRowVoice rowVoice ,MediaPlayer.OnCompletionListener listener) { this.rowVoice = rowVoice; if (!(msg.getBody() instanceof EMVoiceMessageBody)) return; if (mediaPlayer.isPlaying()) { stop(false); } playingId = msg.getMsgId(); onCompletionListener = listener; try { setSpeaker(); EMVoiceMessageBody voiceBody = (EMVoiceMessageBody) msg.getBody(); mediaPlayer.setDataSource(voiceBody.getLocalUrl()); mediaPlayer.prepare(); mediaPlayer.start(); mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { //只有这里返回true其他都是false、改下即可、 正常播放结束 stop(true); playingId = null; // 这里要注释掉、如果不注释只能监听第一次。 //onCompletionListener = null; } }); } catch (IOException e) { e.printStackTrace(); } } public void stop(boolean isReturn) { // Stop the voice play animation. rowVoice.stopVoicePlayAnimation(); mediaPlayer.stop(); mediaPlayer.reset(); /** * This listener is to stop the voice play animation currently, considered the following 3 conditions: * * 1.A new voice item is clicked to play, to stop the previous playing voice item animation. * 2.The voice is play complete, to stop it's voice play animation. * 3.Press the voice record button will stop the voice play and must stop voice play animation. * */ if (isReturn){ if (onCompletionListener != null) { onCompletionListener.onCompletion(mediaPlayer); } } }
 
到这里就算大功告成了。  如有错或者有哪里不合理的,联系qq:1772028976。

要回复问题请先登录注册

知识价值的认可,源自您的赞赏

扫描二维码,你的支付将由imGeek代收后转给对方

×