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

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

beyond

2018-11-22 18:40

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

beyond

2018-11-22 18:40

你可以参考下http://www.imgeek.org/question/11816

wjgso

2018-11-28 08:05

感谢回复。

wjgso

2018-11-27 16:51

​搞了快一周了,终于搞好了。给自己的提问回答上,填坑!。。。。(基于easeui3.3.1版本,对方语音未读连播功能) [list=1] [*]在easeui的EaseChatVoicePresenter里写一个接口的,在播放完成后调用、关键代码如下[code]// 写一个构造方法、把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(); // } }[/code][/*] [/list]

beyond

2018-11-27 17:47

赞,大神考虑下写篇文章教程?有现金奖励http://www.imgeek.org/article/825308713

wjgso

2018-11-28 16:28

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。

wjgso

2018-11-28 16:29

建议拷贝到文档里看。

xiaoyan2015

2019-01-14 10:19

html5微信语音聊天效果 [url]https://www.cnblogs.com/xiaoyan2017/p/10163043.html[/url]

kulong

2019-01-31 17:08

可以试下下在 EaseChatRowVoice里面添加如下代码即可 EMLog.d(TAG, "it is receive msg"); if (voiceBody.downloadStatus() == EMFileMessageBody.EMDownloadStatus.DOWNLOADING || voiceBody.downloadStatus() == EMFileMessageBody.EMDownloadStatus.PENDING) { if (EMClient.getInstance().getOptions().getAutodownloadThumbnail()) { progressBar.setVisibility(View.VISIBLE); } else { progressBar.setVisibility(View.INVISIBLE); } } else { if (!message.isListened() && voiceBody.downloadStatus() == EMFileMessageBody.EMDownloadStatus.SUCCESSED && message.status() == EMMessage.Status.SUCCESS) { EMLog.d(TAG, "-----自动播放----------------"); if (bubbleLayout != null) { bubbleLayout.callOnClick(); } } progressBar.setVisibility(View.INVISIBLE); }  
点击此处继续提问与开发者们互动

申请试用

提交后工作人员会尽快与您联系进行功能演示
技术咨询已转移到管理后台,请先登录

即时通讯云使用者请在应用详情页面咨询
客服云使用者请进入管理员模式咨询

×

400电话

商务咨询

技术咨询

免费试用