@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chat); if (getIntent() == null) { throw new IllegalArgumentException(); } String userId = getIntent().getStringExtra(EXTRA_USER_ID); int chatType = getIntent().getIntExtra(EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE); // 根据用户昵称 String nickName = EaseUI.getInstance().getUserProfileProvider().getUser(userId).getNick(); TitleBar titleBar = (TitleBar) findViewById(R.id.chat_toolbar); titleBar.setCenterTitle(nickName); titleBar.setTitle(R.string.title_message); titleBar.showBackIcon(true); initChatFragment(chatType, userId); }
@Override public void onCreate() { super.onCreate(); EaseUI.getInstance().init(this); }
public void playVoice(String filePath) { if (!(new File(filePath).exists())) { return; } playMsgId = message.getMsgId(); AudioManager audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE); mediaPlayer = new MediaPlayer(); if (EaseUI.getInstance().getSettingsProvider().isSpeakerOpened()) { 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; mediaPlayer.start(); showAnimation(); // 如果是接收的消息 if (message.direct == EMMessage.Direct.RECEIVE) { try { if (!message.isAcked) { message.isAcked = true; // 告知对方已读这条消息 if (chatType != ChatType.GroupChat && chatType != ChatType.ChatRoom) EMChatManager.getInstance().ackMessageRead(message.getFrom(), message.getMsgId()); } } catch (Exception e) { message.isAcked = false; } if (!message.isListened() && iv_read_status != null && iv_read_status.getVisibility() == View.VISIBLE) { // 隐藏自己未播放这条语音消息的标志 iv_read_status.setVisibility(View.INVISIBLE); EMChatManager.getInstance().setMessageListened(message); } } } catch (Exception e) { System.out.println(); } }