Skip to content

Commit

Permalink
1、加入接收与发送消息音效 2、增加显示发信人昵称(右)
Browse files Browse the repository at this point in the history
  • Loading branch information
soulofhan committed Oct 31, 2018
1 parent e47c93f commit 227fe8b
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 17 deletions.
8 changes: 1 addition & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# Gradle files
.gradle/
.idea/
LANTalk.iml

# Mobile Tools for Java (J2ME)
.mtj.tmp/
Expand All @@ -23,12 +24,5 @@
*.tar.gz
*.rar

# .iml
app.iml
LANTalk.iml

#.properties
local.properties

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
16 changes: 12 additions & 4 deletions app/src/main/java/com/andeddo/lanchat/ChatAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,21 @@ public View getView(int position, View convertView, ViewGroup parent) {
if(holderView == null) {
holderView = new HolderView();
if(isMeSend) {
//显示自己发送
convertView = View.inflate(context,R.layout.chat_dialog_right_item,null);
holderView.tv_chat_me_message = convertView.findViewById(R.id.tv_chat_me_message);
holderView.tv_chat_me_message.setText(entity.getChatMsg());
holderView.tv_me_name = convertView.findViewById(R.id.tv_me_name); //绑定发送昵称TextView
holderView.tv_chat_me_message = convertView.findViewById(R.id.tv_chat_me_message); //绑定消息显示TextView
holderView.tv_me_name.setText(entity.getName()); //显示昵称
holderView.tv_chat_me_message.setText(entity.getChatMsg()); //显示消息
} else {
//显示接收消息
convertView = View.inflate(context,R.layout.chat_dialog_left_item,null);
holderView.tv_chat_message = convertView.findViewById(R.id.tv_chat_message);
holderView.tv_chat_message.setText(entity.getChatMsg());
// holderView.tv_name = convertView.findViewById(R.id.tv_name); //绑定接收昵称TextView
holderView.tv_chat_message = convertView.findViewById(R.id.tv_chat_message); //绑定消息显示TextView
holderView.tv_chat_message.setText(entity.getChatMsg()); //显示消息
}
if(isTip){
//显示提示信息
convertView = View.inflate(context,R.layout.chat_dialog_tip_item,null);
holderView.tv_tip = convertView.findViewById(R.id.tv_chat_tip);
holderView.tv_tip.setText(entity.getChatMsg());
Expand All @@ -83,6 +89,8 @@ public View getView(int position, View convertView, ViewGroup parent) {
}

class HolderView {
TextView tv_me_name;
TextView tv_name;
TextView tv_chat_me_message; //绑定自己发送文本框
TextView tv_chat_message; //绑定非自己发送文本框
TextView tv_tip; //绑定提示语
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/andeddo/lanchat/ChatMsgActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
Expand Down Expand Up @@ -32,10 +33,14 @@ public class ChatMsgActivity extends Activity {
private static ListView lv_chatMsg;
private EditText et_getMsg;

private String myName;

private ChatAdapter chatAdapter;
ProgressDialog waitingDialog;
private static List<PersonChat> personChats = new ArrayList<PersonChat>();

static SoundPoolManager soundPoolManager;

@SuppressLint("HandlerLeak")
private static Handler handler = new Handler() {
@Override
Expand Down Expand Up @@ -88,6 +93,10 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_msg);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //显示状态栏
Intent intent = getIntent();
myName = intent.getStringExtra("decideName"); //获取自己的昵称,用于显示

soundPoolManager = SoundPoolManager.getInstance(ChatMsgActivity.this);
chat_titleBar = findViewById(R.id.chat_titleBar);
chat_titleBar.setOnTitleBarListener(new OnTitleBarListener() {
@Override
Expand Down Expand Up @@ -121,17 +130,20 @@ public void onClick(View v) {
});
}

//发送消息
private void mClickListener() {
if (TextUtils.isEmpty(et_getMsg.getText().toString())) {
Toast.makeText(ChatMsgActivity.this, getMsg(R.string.not_empty), Toast.LENGTH_SHORT).show();
return;
}
PersonChat personChat = new PersonChat();
personChat.setName(myName);
personChat.setMeSend(true);
personChat.setChatMsg(et_getMsg.getText().toString());
personChats.add(personChat);
chatAdapter.notifyDataSetChanged();
handler.sendEmptyMessage(1);
soundPoolManager.playSingle(SoundPoolManager.SEND);
SocketManager.sendMessage(et_getMsg.getText().toString());
et_getMsg.setText("");
}
Expand All @@ -149,6 +161,7 @@ public static void setMsg(String name, String getMsg) {
personChat.setName(name);
personChat.setChatMsg(getMsg);
personChats.add(personChat);
soundPoolManager.playSingle(SoundPoolManager.LOAD);
handler.sendEmptyMessage(1);
}

Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/com/andeddo/lanchat/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void handleMessage(Message msg) {
if ("success".equals(MsgHandle.getSuccess())) {
waitingDialog.dismiss();
Intent intent = new Intent(MainActivity.this, ChatMsgActivity.class);
intent.putExtra("decideName",msg.getData().getString("name"));
startActivity(intent);
} else if ("failure".equals(MsgHandle.getSuccess())) {
waitingDialog.dismiss();
Expand Down Expand Up @@ -125,7 +126,14 @@ public void onClick(DialogInterface dialog, int which) {
}
SocketManager.sendMessage(decideName);
showWaitingDialog(getMsg(R.string.logging));
mHandler.sendEmptyMessage(1);

Message msg = new Message();
msg.what = 1;
Bundle bundle = new Bundle();
bundle.putString("name", decideName); //往Bundle中存放数据
msg.setData(bundle);//mes利用Bundle传递数据
mHandler.sendMessage(msg);

isFocus = false; //取消handle检测掉线
dialog.dismiss();
}
Expand Down
113 changes: 113 additions & 0 deletions app/src/main/java/com/andeddo/lanchat/SoundPoolManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.andeddo.lanchat;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.util.Log;

import java.util.HashMap;

import static android.content.Context.AUDIO_SERVICE;

public class SoundPoolManager {
private static final String TAG = "SoundPoolManager";

public final static int SEND = 1;
public final static int LOAD = 2;

private boolean playing = false;
private boolean loaded = false;
private float actualVolume; //当前音量
private float maxVolume; //最大音量
private float volume; //播放音量
private AudioManager audioManager;
private SoundPool soundPool;
private HashMap<Integer, Integer> musicMap; //加载声音列表
private int ringingStreamId;
private static SoundPoolManager instance;

private SoundPoolManager(Context context) {
if (audioManager == null) {
// AudioManager audio settings for adjusting the volume
audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE); //初始化声音管理器
}
Log.d(TAG, "31 SoundPoolManager,audioManager :" + audioManager);
if (audioManager != null) {
actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); //获取音乐当前音量
maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //获取音乐最大音量
}
volume = actualVolume / maxVolume; //得到播放音量
Log.d(TAG, "SoundPoolManager: volume:" + volume + "actualVolume:" + actualVolume + "maxVolume:" + maxVolume);

// Load the sounds
//因为在5.0上new SoundPool();被弃用 5.0上利用Builder
//创建SoundPool
int maxStreams = 5;
Log.d(TAG, "42 SoundPoolManager: build SDK:" + Build.VERSION.SDK_INT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
soundPool = new SoundPool.Builder()
.setMaxStreams(maxStreams)
.build();
} else {
soundPool = new SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0);
}

soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded = true;
}

});
//加载资源ID
musicMap = new HashMap<Integer, Integer>();
musicMap.put(1, soundPool.load(context, R.raw.msg_send, 1));
musicMap.put(2, soundPool.load(context, R.raw.msg_load, 1));
}

public static SoundPoolManager getInstance(Context context) {
if (instance == null) {
instance = new SoundPoolManager(context);
}
return instance;
}

//仅播放一次声音
public void playSingle(int ringingSoundId) {
//当前播放的音乐
ringingStreamId = soundPool.play(musicMap.get(ringingSoundId), volume, volume, 1, 0, 1f);
}

/**
* 可循环播放:
* -1:一直循环
* 0:播放一次
* 1:播放两次
* n:播放n+1次
*
* @param ringingSoundId 需要播放的声音
* @param loop 循环次数
*/
public void playLooping(int ringingSoundId, int loop) {
ringingStreamId = soundPool.play(musicMap.get(ringingSoundId), volume, volume, 1, loop, 1f);
playing = true;
}

//停止播放声音--适用于循环播放
public void stopRinging() {
if (playing) {
soundPool.stop(ringingStreamId);
playing = false;
}
}

public void release() {
if (soundPool != null) {
soundPool.unload(ringingStreamId);
soundPool.release();
soundPool = null;
}
instance = null;
}
}
24 changes: 19 additions & 5 deletions app/src/main/res/layout/chat_dialog_right_item.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="end"
android:orientation="horizontal">

<TextView
android:id="@+id/tv_me_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="28dp"
android:gravity="center"
android:text="@string/tv_default"
android:textSize="8dp" />

<TextView
android:id="@+id/tv_chat_me_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="12dp"
android:layout_marginEnd="26dp"
android:background="@drawable/acn"
android:gravity="center"
android:text="@string/tv_default" />
Expand All @@ -19,7 +31,9 @@
android:id="@+id/iv_chat_image_right"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_gravity="top"
android:src="@drawable/message_background" />

</LinearLayout>
</RelativeLayout>
Binary file added app/src/main/res/raw/e_s_two.mp3
Binary file not shown.
Binary file added app/src/main/res/raw/e_scan_henshin.mp3
Binary file not shown.
Binary file added app/src/main/res/raw/e_wait_henshin.mp3
Binary file not shown.
Binary file added app/src/main/res/raw/end_henshin.mp3
Binary file not shown.
Binary file added app/src/main/res/raw/msg_load.mp3
Binary file not shown.
Binary file added app/src/main/res/raw/msg_send.mp3
Binary file not shown.
1 change: 1 addition & 0 deletions library/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
library.iml

0 comments on commit 227fe8b

Please sign in to comment.