安卓机微信在跟别人聊天中,来新消息提示音的提示音怎么去掉

深圳市世炬科技有限公司
地址:深圳市龙岗区坂田街道环城南路金方华物流园3栋215
传真:9-805
数据库出错Android模仿微信语音聊天功能
项目效果如下:
项目目录结构如下:
代码如下:
AudioManager.java
package com.xuliugen.
import java.io.F
import java.io.IOE
import java.util.UUID;
import android.media.MediaR
public class AudioManager {
private MediaRecorder mMediaR
private String mD
private String mCurrentFileP
private static AudioManager mI
private boolean isP
private AudioManager(String dir) {
public static AudioManager getInstance(String dir) {
if (mInstance == null) {
synchronized (AudioManager.class) {
if (mInstance == null) {
mInstance = new AudioManager(dir);
* 使用接口 用于回调
public interface AudioStateListener {
void wellPrepared();
public AudioStateListener mAudioStateL
* 回调方法
public void setOnAudioStateListener(AudioStateListener listener) {
mAudioStateListener =
public void prepareAudio() {
isPrepare =
File dir = new File(mDir);
if (!dir.exists()) {
dir.mkdirs();
String fileName = generateFileName();
File file = new File(dir, fileName);
mCurrentFilePath =file.getAbsolutePath();
mMediaRecorder = new MediaRecorder();
// 设置输出文件
mMediaRecorder.setOutputFile(dir.getAbsolutePath());
// 设置MediaRecorder的音频源为麦克风
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 设置音频格式
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
// 设置音频编码
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// 准备录音
mMediaRecorder.prepare();
mMediaRecorder.start();
// 准备结束
isPrepare =
if (mAudioStateListener != null) {
mAudioStateListener.wellPrepared();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
* 随机生成文件的名称
private String generateFileName() {
return UUID.randomUUID().toString() + .
public int getVoiceLevel(int maxlevel) {
if (isPrepare) {
// mMediaRecorder.getMaxAmplitude() 1~32767
return maxlevel * mMediaRecorder.getMaxAmplitude() / 32768 + 1;
} catch (Exception e) {
* 释放资源
public void release() {
//mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder =
* 取消录音
public void cancel() {
release();
if (mCurrentFilePath != null) {
File file = new File(mCurrentFilePath);
file.delete();
mCurrentFilePath =
public String getCurrentFilePath() {
return mCurrentFileP
AudioRecorderButton.java
package com.xuliugen.
import android.content.C
import android.os.E
import android.os.H
import android.os.M
import android.util.AttributeS
import android.view.MotionE
import android.view.V
import android.widget.B
import com.xuliugen.weichat.R;
import com.xuliugen.weichat.AudioManager.AudioStateL
public class AudioRecorderButton extends Button {
private static final int STATE_NORMAL = 1;// 默认的状态
private static final int STATE_RECORDING = 2;// 正在录音
private static final int STATE_WANT_TO_CANCEL = 3;// 希望取消
private int mCurrentState = STATE_NORMAL; // 当前的状态
private boolean isRecording =// 已经开始录音
private static final int DISTANCE_Y_CANCEL = 50;
private DialogManager mDialogM
private AudioManager mAudioM
private float mT
// 是否触发longClick
private boolean mR
private static final int MSG_AUDIO_PREPARED = 0x110;
private static final int MSG_VOICE_CHANGED = 0x111;
private static final int MSG_DIALOG_DIMISS = 0x112;
* 获取音量大小的线程
private Runnable mGetVoiceLevelRunnable = new Runnable() {
public void run() {
while (isRecording) {
Thread.sleep(100);
mTime += 0.1f;
mHandler.sendEmptyMessage(MSG_VOICE_CHANGED);
} catch (InterruptedException e) {
e.printStackTrace();
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_AUDIO_PREPARED:
// 显示對話框在开始录音以后
mDialogManager.showRecordingDialog();
isRecording =
// 开启一个线程
new Thread(mGetVoiceLevelRunnable).start();
case MSG_VOICE_CHANGED:
mDialogManager.updateVoiceLevel(mAudioManager.getVoiceLevel(7));
case MSG_DIALOG_DIMISS:
mDialogManager.dimissDialog();
super.handleMessage(msg);
* 以下2个方法是构造方法
public AudioRecorderButton(Context context, AttributeSet attrs) {
super(context, attrs);
mDialogManager = new DialogManager(context);
String dir = /storage/sdcard0/my_
//String dir = Environment.getExternalStorageDirectory()+/my_
mAudioManager = AudioManager.getInstance(dir);
mAudioManager.setOnAudioStateListener(new AudioStateListener() {
public void wellPrepared() {
mHandler.sendEmptyMessage(MSG_AUDIO_PREPARED);
// 由于这个类是button所以在构造方法中添加监听事件
setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
mAudioManager.prepareAudio();
public AudioRecorderButton(Context context) {
this(context, null);
* 录音完成后的回调
public interface AudioFinishRecorderListener {
void onFinish(float seconds, String filePath);
private AudioFinishRecorderListener audioFinishRecorderL
public void setAudioFinishRecorderListener(AudioFinishRecorderListener listener) {
audioFinishRecorderListener =
* 屏幕的触摸事件
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int x = (int) event.getX();// 获得x轴坐标
int y = (int) event.getY();// 获得y轴坐标
switch (action) {
case MotionEvent.ACTION_DOWN:
changeState(STATE_RECORDING);
case MotionEvent.ACTION_MOVE:
if (isRecording) {
// 如果想要取消,根据x,y的坐标看是否需要取消
if (wantToCancle(x, y)) {
changeState(STATE_WANT_TO_CANCEL);
changeState(STATE_RECORDING);
case MotionEvent.ACTION_UP:
if (!mReady) {
return super.onTouchEvent(event);
if (!isRecording || mTime & 0.6f) {
mDialogManager.tooShort();
mAudioManager.cancel();
mHandler.sendEmptyMessageDelayed(MSG_DIALOG_DIMISS, 1000);// 延迟显示对话框
} else if (mCurrentState == STATE_RECORDING) { // 正在录音的时候,结束
mDialogManager.dimissDialog();
mAudioManager.release();
if (audioFinishRecorderListener != null) {
audioFinishRecorderListener.onFinish(mTime,mAudioManager.getCurrentFilePath());
} else if (mCurrentState == STATE_WANT_TO_CANCEL) { // 想要取消
mDialogManager.dimissDialog();
mAudioManager.cancel();
return super.onTouchEvent(event);
* 恢复状态及标志位
private void reset() {
isRecording =
mTime = 0;
changeState(STATE_NORMAL);
private boolean wantToCancle(int x, int y) {
if (x & 0 || x & getWidth()) { // 超过按钮的宽度
// 超过按钮的高度
if (y & -DISTANCE_Y_CANCEL || y & getHeight() + DISTANCE_Y_CANCEL) {
private void changeState(int state) {
if (mCurrentState != state) {
mCurrentState =
switch (state) {
case STATE_NORMAL:
setBackgroundResource(R.drawable.btn_recorder_normal);
setText(R.string.str_recorder_normal);
case STATE_RECORDING:
setBackgroundResource(R.drawable.btn_recorder_recording);
setText(R.string.str_recorder_recording);
if (isRecording) {
mDialogManager.recording();
case STATE_WANT_TO_CANCEL:
setBackgroundResource(R.drawable.btn_recorder_recording);
setText(R.string.str_recorder_want_cancel);
mDialogManager.wantToCancel();
DialogManager.java
package com.xuliugen.
import android.app.AlertD
import android.content.C
import android.view.LayoutI
import android.view.V
import android.widget.ImageV
import android.widget.TextV
import com.xuliugen.weichat.R;
* 用于管理Dialog
* @author xuliugen
public class DialogManager {
private AlertDialog.B
private ImageView mI
private ImageView mV
private TextView mL
private Context mC
private AlertD//用于取消AlertDialog.Builder
* 构造方法 传入上下文
public DialogManager(Context context) {
this.mContext =
// 显示录音的对话框
public void showRecordingDialog() {
builder = new AlertDialog.Builder(mContext, R.style.AudioDialog);
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.dialog_recorder,null);
mIcon = (ImageView) view.findViewById(R.id.id_recorder_dialog_icon);
mVoice = (ImageView) view.findViewById(R.id.id_recorder_dialog_voice);
mLable = (TextView) view.findViewById(R.id.id_recorder_dialog_label);
builder.setView(view);
builder.create();
dialog = builder.show();
public void recording(){
if(dialog != null && dialog.isShowing()){ //显示状态
mIcon.setVisibility(View.VISIBLE);
mVoice.setVisibility(View.VISIBLE);
mLable.setVisibility(View.VISIBLE);
mIcon.setImageResource(R.drawable.recorder);
mLable.setText(手指上滑,取消发送);
// 显示想取消的对话框
public void wantToCancel() {
if(dialog != null && dialog.isShowing()){ //显示状态
mIcon.setVisibility(View.VISIBLE);
mVoice.setVisibility(View.GONE);
mLable.setVisibility(View.VISIBLE);
mIcon.setImageResource(R.drawable.cancel);
mLable.setText(松开手指,取消发送);
// 显示时间过短的对话框
public void tooShort() {
if(dialog != null && dialog.isShowing()){ //显示状态
mIcon.setVisibility(View.VISIBLE);
mVoice.setVisibility(View.GONE);
mLable.setVisibility(View.VISIBLE);
mIcon.setImageResource(R.drawable.voice_to_short);
mLable.setText(录音时间过短);
// 显示取消的对话框
public void dimissDialog() {
if(dialog != null && dialog.isShowing()){ //显示状态
dialog.dismiss();
// 显示更新音量级别的对话框
public void updateVoiceLevel(int level) {
if(dialog != null && dialog.isShowing()){ //显示状态
mIcon.setVisibility(View.VISIBLE);
mVoice.setVisibility(View.VISIBLE);
mLable.setVisibility(View.VISIBLE);
//设置图片的id
int resId = mContext.getResources().getIdentifier(v+level, drawable, mContext.getPackageName());
mVoice.setImageResource(resId);
MainActivity.java
package com.xuliugen.
import java.util.ArrayL
import java.util.L
import com.xuliugen.weichat.AudioRecorderButton.AudioFinishRecorderL
import android.app.A
import android.graphics.drawable.AnimationD
import android.media.MediaP
import android.os.B
import android.view.V
import android.widget.AdapterV
import android.widget.ArrayA
import android.widget.ListV
import android.widget.AdapterView.OnItemClickL
public class MainActivity extends Activity {
private ListView mListV
private ArrayAdapter mA
private List mDatas = new ArrayList();
private AudioRecorderButton mAudioRecorderB
private View animV
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.id_listview);
mAudioRecorderButton = (AudioRecorderButton) findViewById(R.id.id_recorder_button);
mAudioRecorderButton.setAudioFinishRecorderListener(new AudioFinishRecorderListener() {
public void onFinish(float seconds, String filePath) {
Recorder recorder = new Recorder(seconds, filePath);
mDatas.add(recorder);
mAdapter.notifyDataSetChanged(); //通知更新的内容
mListView.setSelection(mDatas.size() - 1); //将lisview设置为最后一个
mAdapter = new RecoderAdapter(this, mDatas);
mListView.setAdapter(mAdapter);
//listView的item点击事件
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView arg0, View view,int position, long id) {
// 播放动画(帧动画)
if (animView != null) {
animView.setBackgroundResource(R.drawable.adj);
animView =
animView = view.findViewById(R.id.id_recoder_anim);
animView.setBackgroundResource(R.drawable.play_anim);
AnimationDrawable animation = (AnimationDrawable) animView.getBackground();
animation.start();
// 播放录音
MediaManager.playSound(mDatas.get(position).filePath,new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
animView.setBackgroundResource(R.drawable.adj);
protected void onPause() {
super.onPause();
MediaManager.pause();
protected void onResume() {
super.onResume();
MediaManager.resume();
protected void onDestroy() {
super.onDestroy();
MediaManager.release();
class Recorder {
String fileP
public Recorder(float time, String filePath) {
this.time =
this.filePath = fileP
public float getTime() {
public void setTime(float time) {
this.time =
public String getFilePath() {
return fileP
public void setFilePath(String filePath) {
this.filePath = fileP
MediaManager.java
package com.xuliugen.
import android.media.AudioM
import android.media.MediaP
import android.media.MediaPlayer.OnCompletionL
import android.media.MediaPlayer.OnErrorL
public class MediaManager {
private static MediaPlayer mMediaP
private static boolean isP
* 播放音乐
* @param filePath
* @param onCompletionListener
public static void playSound(String filePath,OnCompletionListener onCompletionListener) {
if (mMediaPlayer == null) {
mMediaPlayer = new MediaPlayer();
//设置一个error监听器
mMediaPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
mMediaPlayer.reset();
mMediaPlayer.reset();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnCompletionListener(onCompletionListener);
mMediaPlayer.setDataSource(filePath);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
* 暂停播放
public static void pause() {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) { //正在播放的时候
mMediaPlayer.pause();
* 当前是isPause状态
public static void resume() {
if (mMediaPlayer != null && isPause) {
mMediaPlayer.start();
* 释放资源
public static void release() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer =
RecoderAdapter.java
package com.xuliugen.
import java.util.L
import android.content.C
import android.util.DisplayM
import android.view.LayoutI
import android.view.V
import android.view.ViewG
import android.view.WindowM
import android.widget.ArrayA
import android.widget.TextV
import com.xuliugen.weichat.MainActivity.R
public class RecoderAdapter extends ArrayAdapter {
private Context mC
private List mD
private int mMinItemW //最小的item宽度
private int mMaxItemW //最大的item宽度
private LayoutInflater mI
public RecoderAdapter(Context context, List datas) {
super(context, -1, datas);
mContext =
//获取屏幕的宽度
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
mMaxItemWidth = (int) (outMetrics.widthPixels * 0.7f);
mMinItemWidth = (int) (outMetrics.widthPixels * 0.15f);
mInflater = LayoutInflater.from(context);
* 定义一个ViewHolder
private class ViewHolder {
TextV // 时间
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder =
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_recoder, parent,false);
holder = new ViewHolder();
holder.seconds = (TextView) convertView.findViewById(R.id.id_recoder_time);
holder.length = convertView.findViewById(R.id.id_recoder_lenght);
convertView.setTag(holder);
holder = (ViewHolder) convertView.getTag();
holder.seconds.setText(Math.round(getItem(position).time) + );
ViewGroup.LayoutParams lp = holder.length.getLayoutParams();
lp.width = (int) (mMinItemWidth + (mMaxItemWidth / 60f)* getItem(position).time);
return convertV
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'}

我要回帖

更多关于 微信消息提示音下载 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信