Android自定义UI实现微信语音

Android自定义UI实现微信语音,第1张

概述本文实例为大家分享了java获取不同路径的方法,供大家参考,具体内容如下思路:

本文实例为大家分享了java获取不同路径的方法,供大家参考,具体内容如下

思路:
自定义button
获取DialogManager、Au@R_404_6901@Manager setonLongClickListener长按事件--做好Au@R_404_6901@Manager的录音准备工作
Au@R_404_6901@Manager.setonAu@R_404_6901@StateListener(this)实现录音准备完毕的接口回调方法,方法中去发送MSG_AU@R_404_6901@_PREPARE消息代表录音准备工作完毕
在mHandler中接收消息,开始开启线程录音,并且计时,计时的过程中,去发送MSG_VOICE_CHANGED消息,去updateVoiceLeve更新音量图标,另外在MotionEvent.ACTION_UP事件中,去定义au@R_404_6901@FinishRecorderListener.onFinish接口方法,方法是录音完毕的回调方法,在MainActivity中,mAu@R_404_6901@Recorderbutton.setAu@R_404_6901@FinishRecorderListener实现录音完毕回调方法去更新ListVIEwUi界面

点击ListVIEwItem条目播放动画, MediaManager.playSound并且播放音频

activity_main.xml

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:tools="http://schemas.androID.com/tools"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:orIEntation="vertical"  tools:context="com.example.imooc_recorder.MainActivity" >   <ListVIEw  androID:ID="@+ID/ID_ListvIEw"  androID:layout_wIDth="fill_parent"  androID:layout_height="0dp"  androID:layout_weight="1"  androID:background="#ebebeb"  androID:divIDer="@null"  androID:divIDerHeight="10dp" >  </ListVIEw>   <FrameLayout  androID:layout_wIDth="fill_parent"  androID:layout_height="wrap_content" >  <VIEw   androID:background="#ccc"   androID:layout_wIDth="fill_parent"   androID:layout_height="1dp" />   <com.example.recorder_vIEw.Au@R_404_6901@Recorderbutton   androID:ID="@+ID/ID_recorder_button"   androID:layout_wIDth="fill_parent"   androID:layout_height="wrap_content"   androID:layout_marginBottom="7dp"   androID:layout_marginleft="50dp"   androID:layout_marginRight="50dp"   androID:layout_margintop="6dp"   androID:background="@drawable/button_recorder_normal"   androID:gravity="center"   androID:minHeight="0dp"   androID:padding="5dp"   androID:text="@string/str_recorder_nomal"   androID:textcolor="#727272" >  </com.example.recorder_vIEw.Au@R_404_6901@Recorderbutton>   </FrameLayout>  </linearLayout> 

dialog_recorder.xml---dialog布局文件

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:tools="http://schemas.androID.com/tools"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:background="@drawable/dialog_loading_bg"  androID:gravity="center"  androID:orIEntation="vertical"  androID:padding="20dp" >   <linearLayout  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:orIEntation="horizontal" >   <ImageVIEw   androID:ID="@+ID/ID_recorder_dialog_icon"   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content"   androID:src="@drawable/recorder"   androID:visibility="visible" />   <ImageVIEw   androID:ID="@+ID/ID_recorder_dialog_voice"   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content"   androID:src="@drawable/v1"   androID:visibility="visible" />  </linearLayout>   <TextVIEw  androID:ID="@+ID/ID_recorder_dialog_label"  androID:layout_margintop="5dp"  androID:text="手指上滑,取消发送"  androID:textcolor="#ffffff"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"/>  </linearLayout> 

item_recorder.xml--ListvIEw中的每个item

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:tools="http://schemas.androID.com/tools"  androID:layout_wIDth="match_parent"  androID:background="#eeeeee"  androID:layout_height="60dp"  androID:layout_margintop="5dp" >   <ImageVIEw  androID:ID="@+ID/ID_icon"  androID:layout_wIDth="40dp"  androID:layout_height="40dp"  androID:layout_alignParentRight="true"  androID:layout_centerVertical="true"  androID:layout_marginRight="5dp"  androID:src="@drawable/icon" />   <FrameLayout  androID:ID="@+ID/ID_recorder_length"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:layout_centerVertical="true"  androID:layout_toleftOf="@ID/ID_icon"  androID:background="@drawable/chatto_bg_focused" >   <VIEw   androID:ID="@+ID/ID_recorder_anim"   androID:layout_wIDth="25dp"   androID:layout_height="25dp"   androID:layout_gravity="center_vertical|right"   androID:background="@drawable/adj" />  </FrameLayout>   <TextVIEw  androID:ID="@+ID/ID_recorder_time"  androID:layout_centerVertical="true"  androID:layout_toleftOf="@ID/ID_recorder_length"  androID:layout_marginRight="3dp"  androID:textcolor="#ff777777"  androID:text=""  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content" />  </relativeLayout> 

styles.xml

<style name="theme_Au@R_404_6901@Dialog" parent="@androID:theme.Dialog">  <item name="androID:windowBackground">@androID:color/transparent</item>  <item name="androID:windowFrame">@null</item>  <item name="androID:windowIsfloating">true</item>   <item name="androID:windowIsTranslucent">true</item>   <item name="androID:backgroundDimEnabled">false</item>   </style> 

button_recorder_normal.xml

<?xml version="1.0" enCoding="utf-8"?> <shape xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:shape="rectangle" >   <solID androID:color="#ffffff"/>  <stroke androID:wIDth="1px" androID:color="#9b9b9b"/>  <corners androID:radius="3dp"/> </shape> 

button_recorder_recording.xml

<?xml version="1.0" enCoding="utf-8"?> <shape xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:shape="rectangle" >   <solID androID:color="#ffffff"/>  <stroke androID:wIDth="1px" androID:color="#eeeeee"/>  <corners androID:radius="3dp"/> </shape> 

play_anim.xml

<?xml version="1.0" enCoding="utf-8"?> <animation-List xmlns:androID="http://schemas.androID.com/apk/res/androID" >   <item  androID:drawable="@drawable/v_anim1"  androID:duration="300">  </item>  <item  androID:drawable="@drawable/v_anim2"  androID:duration="300">  </item>   <item  androID:drawable="@drawable/v_anim3"  androID:duration="300">  </item>   </animation-List> 

MainActivity

package com.example.imooc_recorder;  import java.util.ArrayList; import java.util.List;  import com.example.recorder_vIEw.Au@R_404_6901@Recorderbutton; import com.example.recorder_vIEw.Au@R_404_6901@Recorderbutton.Au@R_404_6901@FinishRecorderListener; import com.example.recorder_vIEw.MediaManager;  import androID.app.Activity; import androID.graphics.drawable.AnimationDrawable; import androID.media.MediaPlayer; import androID.os.Bundle; import androID.vIEw.Menu; import androID.vIEw.MenuItem; import androID.vIEw.VIEw; import androID.Widget.AdapterVIEw.OnItemClickListener; import androID.Widget.AdapterVIEw; import androID.Widget.ArrayAdapter; import androID.Widget.ListVIEw;  public class MainActivity extends Activity {   private ListVIEw mListVIEw;  private ArrayAdapter<Recorder> mAdapter;  private List<Recorder> mDatas = new ArrayList<MainActivity.Recorder>();  private Au@R_404_6901@Recorderbutton mAu@R_404_6901@Recorderbutton;  private VIEw animVIEw;  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentVIEw(R.layout.activity_main);  mListVIEw = (ListVIEw) findVIEwByID(R.ID.ID_ListvIEw);  mAu@R_404_6901@Recorderbutton = (Au@R_404_6901@Recorderbutton) findVIEwByID(R.ID.ID_recorder_button);  //录音完毕,回调  mAu@R_404_6901@Recorderbutton.setAu@R_404_6901@FinishRecorderListener(new Au@R_404_6901@FinishRecorderListener() {     @OverrIDe   public voID onFinish(float seconds,String filePath) {   // Todo auto-generated method stub   Recorder recorder = new Recorder(seconds,filePath);   mDatas.add(recorder);   mAdapter.notifyDataSetChanged();   mListVIEw.setSelection(mDatas.size()-1);   }  });  mAdapter = new RecorderAdapter(this,mDatas);  mListVIEw.setAdapter(mAdapter);    mListVIEw.setonItemClickListener(new OnItemClickListener() {   @OverrIDe   public voID onItemClick(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) {   if (animVIEw !=null) {    animVIEw.setBackgroundResource(R.drawable.adj);    animVIEw = null;       }   //播放动画   animVIEw = vIEw.findVIEwByID(R.ID.ID_recorder_anim);   animVIEw.setBackgroundResource(R.drawable.play_anim);   AnimationDrawable anim = (AnimationDrawable) animVIEw.getBackground();   anim.start();   //播放音频   MediaManager.playSound(mDatas.get(position).filePath,new MediaPlayer.OnCompletionListener() {       @OverrIDe    public voID onCompletion(MediaPlayer mp) {    // Todo auto-generated method stub    animVIEw.setBackgroundResource(R.drawable.adj);    }   });   }  });  }   @OverrIDe  protected voID onPause() {  // Todo auto-generated method stub  super.onPause();  MediaManager.pause();  }  @OverrIDe  protected voID onResume() {  // Todo auto-generated method stub  super.onResume();  MediaManager.resume();  }  @OverrIDe  protected voID onDestroy() {  // Todo auto-generated method stub  super.onDestroy();  MediaManager.release();  }  /**内部类*/  class Recorder{  float time;  String filePath;  public Recorder(float time,String filePath) {   super();   this.time = time;   this.filePath = filePath;  }  public float getTime() {   return time;  }  public voID setTime(float time) {   this.time = time;  }  public String getfilePath() {   return filePath;  }  public voID setfilePath(String filePath) {   this.filePath = filePath;  }    } } 

RecorderAdapter

package com.example.imooc_recorder;  import java.util.List;  import com.example.imooc_recorder.MainActivity.Recorder;  import androID.content.Context; import androID.util.displayMetrics; import androID.vIEw.LayoutInflater; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.vIEw.WindowManager; import androID.Widget.ArrayAdapter; import androID.Widget.BaseAdapter; import androID.Widget.linearLayout; import androID.Widget.TextVIEw;  public class RecorderAdapter extends ArrayAdapter<Recorder> {  // private List<Recorder> mDatas; // private Context mContext;  private int mMinItemWIDth;  private int mMaxItemWIDth;  private LayoutInflater mInflater;   public RecorderAdapter(Context context,List<Recorder> datas) {  super(context,-1,datas); // mContext = context; // mDatas = datas;  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);  }   @OverrIDe  public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {  // Todo auto-generated method stub  VIEwHolder holder = null;  if (convertVIEw == null) {   convertVIEw = mInflater.inflate(R.layout.item_recorder,parent,false);   holder = new VIEwHolder();   holder.seconds = (TextVIEw) convertVIEw.findVIEwByID(R.ID.ID_recorder_time);   holder.length = convertVIEw.findVIEwByID(R.ID.ID_recorder_length);    convertVIEw.setTag(holder);  }else {   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 convertVIEw;  }   private class VIEwHolder {  private TextVIEw seconds;  private VIEw length;  } } 

Au@R_404_6901@Recorderbutton

package com.example.recorder_vIEw;  import com.example.imooc_recorder.R; import com.example.recorder_vIEw.Au@R_404_6901@Manager.Au@R_404_6901@StateListener;  import androID.content.Context; import androID.os.Environment; import androID.os.Handler; import androID.telephony.SignalStrength; import androID.util.AttributeSet; import androID.vIEw.MotionEvent; import androID.vIEw.VIEw; import androID.Widget.button;  public class Au@R_404_6901@Recorderbutton extends button implements Au@R_404_6901@StateListener {  private static final int disTANCE_Y_CANCEL = 50;   /** 提示框管理工具类 */  private DialogManager mDialogManager;  /** 正常、松开手指取消发送、手指上滑取消发送 */  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 mCurState = STATE_norMAL;  /** 当前是否正在录音 */  private boolean isRecording = false;   /** 录音工具类 */  private Au@R_404_6901@Manager mAu@R_404_6901@Manager;   /** 录音总时间 */  private float mTime;   /** 是否触发button */  private boolean mReady;   /** *****************自定义button************************************* */  public Au@R_404_6901@Recorderbutton(Context context) {  super(context);  }   public Au@R_404_6901@Recorderbutton(Context context,AttributeSet attrs,int defStyle) {  super(context,attrs,defStyle);  }   public Au@R_404_6901@Recorderbutton(Context context,AttributeSet attrs) {  super(context,attrs);  mDialogManager = new DialogManager(getContext());  String dir = Environment.getExternalStorageDirectory()   + "/imooc_recorder_au@R_404_6901@s";  mAu@R_404_6901@Manager = Au@R_404_6901@Manager.getInstance(dir);  mAu@R_404_6901@Manager.setonAu@R_404_6901@StateListener(this);  /** 监听回调 */  setonLongClickListener(new OnLongClickListener() {    @OverrIDe   public boolean onLongClick(VIEw v) {   mReady = true;   // 录音准备--准备好后,有实现接口方法   mAu@R_404_6901@Manager.prepareAn@R_404_6901@();   return false;   }  });  }   /******************************************************  * 完成录音准备的回调函数  */  @OverrIDe  public voID wellPrepared() {  // 准备完毕,发送消息  mHandler.sendEmptyMessage(MSG_AU@R_404_6901@_PREPARE);  }   private Handler mHandler = new Handler() {  public voID handleMessage(androID.os.Message msg) {   switch (msg.what) {   case MSG_AU@R_404_6901@_PREPARE:   mDialogManager.showRecordingDailog();   isRecording = true;    new Thread(new Runnable() {     @OverrIDe    public voID run() {     // Todo auto-generated method stub    while (isRecording) {     try {     Thread.sleep(100);     // 录音计时     mTime += 0.1f;     mHandler.sendEmptyMessage(MSG_VOICE_CHANGED);     } catch (InterruptedException e) {     e.printstacktrace();     }     }     }   }).start();   break;   case MSG_VOICE_CHANGED:    mDialogManager.updateVoiceLeve(mAu@R_404_6901@Manager.getVoiceLevel(7));    break;   case MSG_DIALOG_DIMISS:    mDialogManager.dimissDailog();   break;    default:   break;   }   super.handleMessage(msg);  };  };   private static final int MSG_AU@R_404_6901@_PREPARE = 0x110;  private static final int MSG_VOICE_CHANGED = 0x111;  private static final int MSG_DIALOG_DIMISS = 0x112;   @OverrIDe  public boolean ontouchEvent(MotionEvent event) {  // Todo auto-generated method stub  int action = event.getAction();  int x = (int) event.getX();  int y = (int) event.getY();   switch (action) {  case MotionEvent.ACTION_DOWN:   changeState(STATE_RECORDING);   break;  case MotionEvent.ACTION_MOVE:   if (isRecording) {   // 根据xy坐标。判断是否想要取消   if (wantToCancel(x,y)) {    changeState(STATE_WANT_TO_CANCEL);   } else {    changeState(STATE_RECORDING);   }    }   break;  case MotionEvent.ACTION_UP:   if (!mReady) {   // 没有触发onclick   reset();   return super.ontouchEvent(event);    }   if (!isRecording || mTime < 0.6f) {   // prapred没有完成已经up了   mDialogManager.tooShort();   mAu@R_404_6901@Manager.cancel();   mHandler.sendEmptyMessageDelayed(MSG_DIALOG_DIMISS,1300);    } else if (mCurState == STATE_RECORDING) {    mDialogManager.dimissDailog();   mAu@R_404_6901@Manager.release();   if (au@R_404_6901@FinishRecorderListener != null) {    au@R_404_6901@FinishRecorderListener.onFinish(mTime,mAu@R_404_6901@Manager.getCurrentfilePath());   }   } else if (mCurState == STATE_WANT_TO_CANCEL) {   mDialogManager.dimissDailog();   mAu@R_404_6901@Manager.cancel();   }   // 恢复标志位   reset();   break;   default:   break;  }  return super.ontouchEvent(event);  }   private voID reset() {  // Todo auto-generated method stub  mReady = false;  mTime = 0;  isRecording = false;  changeState(STATE_norMAL);  }   private boolean wantToCancel(int x,int y) {  // x<0代表来到按钮的范围以外  if (x < 0 || x > getWIDth()) {   return true;   }  if (y < -disTANCE_Y_CANCEL || y > getHeight() + disTANCE_Y_CANCEL) {   return true;   }  return false;  }   private voID changeState(int state) {  // Todo auto-generated method stub  if (mCurState != state) {   mCurState = state;    switch (state) {   case STATE_norMAL:   setBackgroundResource(R.drawable.button_recorder_normal);   setText(R.string.str_recorder_nomal);   break;    case STATE_RECORDING:   setBackgroundResource(R.drawable.button_recorder_recording);   setText(R.string.str_recorder_recording);   if (isRecording) {    // to do    mDialogManager.recording();   }   break;    case STATE_WANT_TO_CANCEL:   setBackgroundResource(R.drawable.button_recorder_recording);   setText(R.string.str_recorder_want_cancel);   mDialogManager.wantToCancle();   break;   default:   break;   }  }  }   /** 完成录音回调 */  public interface Au@R_404_6901@FinishRecorderListener {  voID onFinish(float seconds,String filePath);  }   private Au@R_404_6901@FinishRecorderListener au@R_404_6901@FinishRecorderListener;   public voID setAu@R_404_6901@FinishRecorderListener(   Au@R_404_6901@FinishRecorderListener au@R_404_6901@FinishRecorderListener) {  this.au@R_404_6901@FinishRecorderListener = au@R_404_6901@FinishRecorderListener;  } } 

DialogManager

package com.example.recorder_vIEw;  import com.example.imooc_recorder.R;  import androID.app.Dialog; import androID.content.Context; import androID.vIEw.LayoutInflater; import androID.vIEw.VIEw; import androID.Widget.ImageVIEw; import androID.Widget.TextVIEw;  public class DialogManager {  private Dialog mDialog;  private ImageVIEw mIcon;  private ImageVIEw mVoice;  private TextVIEw mLabel;  private Context mContext;   public DialogManager(Context context) {  this.mContext = context;  }   public voID showRecordingDailog() {  mDialog = new Dialog(mContext,R.style.theme_Au@R_404_6901@Dialog);  LayoutInflater inflater = LayoutInflater.from(mContext);  VIEw vIEw = inflater.inflate(R.layout.dialog_recorder,null);  mDialog.setContentVIEw(vIEw);   mIcon = (ImageVIEw) mDialog.findVIEwByID(R.ID.ID_recorder_dialog_icon);  mVoice = (ImageVIEw) mDialog   .findVIEwByID(R.ID.ID_recorder_dialog_voice);  mLabel = (TextVIEw) mDialog.findVIEwByID(R.ID.ID_recorder_dialog_label);   mDialog.show();  }   public voID recording() {  if (mDialog != null && mDialog.isShowing()) {   mIcon.setVisibility(VIEw.VISIBLE);   mVoice.setVisibility(VIEw.VISIBLE);   mLabel.setVisibility(VIEw.VISIBLE);    mIcon.setimageResource(R.drawable.recorder);   mLabel.setText("手指上滑,取消发送");   }  }   public voID wantToCancle() {  if (mDialog != null && mDialog.isShowing()) {   mIcon.setVisibility(VIEw.VISIBLE);   mVoice.setVisibility(VIEw.GONE);   mLabel.setVisibility(VIEw.VISIBLE);    mIcon.setimageResource(R.drawable.cancel);   mLabel.setText("松开手指,取消发送");   }  }   public voID tooShort() {  if (mDialog != null && mDialog.isShowing()) {   mIcon.setVisibility(VIEw.VISIBLE);   mVoice.setVisibility(VIEw.GONE);   mLabel.setVisibility(VIEw.VISIBLE);    mIcon.setimageResource(R.drawable.voice_to_short);   mLabel.setText("录音时间过短");   }   }   public voID dimissDailog() {  if (mDialog != null && mDialog.isShowing()) {   mDialog.dismiss();   mDialog = null;  }  }   public voID updateVoiceLeve(int level) {  if (mDialog != null && mDialog.isShowing()) { //  mIcon.setVisibility(VIEw.VISIBLE); //  mVoice.setVisibility(VIEw.VISIBLE); //  mLabel.setVisibility(VIEw.VISIBLE);    // 通过方法名字,找到资源   int resID = mContext.getResources().getIDentifIEr("v" + level,"drawable",mContext.getPackagename());   mVoice.setimageResource(resID);  }  }  } 

Au@R_404_6901@Manager

package com.example.recorder_vIEw;  import java.io.file; import java.io.IOException; import java.util.UUID;  import javax.security.auth.PrivateCredentialPermission;  import androID.R.integer; import androID.media.MediaRecorder;  public class Au@R_404_6901@Manager {   private MediaRecorder mMediaRecorder;  private String mDir;  private String mCurrentfilepath;  private static Au@R_404_6901@Manager mInstance;   private boolean isPrepare;   /** 接口回调 */  public interface Au@R_404_6901@StateListener {  voID wellPrepared();  }   public Au@R_404_6901@StateListener mAu@R_404_6901@StateListener;   public voID setonAu@R_404_6901@StateListener(Au@R_404_6901@StateListener au@R_404_6901@StateListener) {  mAu@R_404_6901@StateListener = au@R_404_6901@StateListener;   }   private Au@R_404_6901@Manager(String dir) {  this.mDir = dir;  }   public static Au@R_404_6901@Manager getInstance(String dir) {  if (mInstance == null) {   synchronized (Au@R_404_6901@Manager.class) {   if (mInstance == null) {    mInstance = new Au@R_404_6901@Manager(dir);    }    }  }  return mInstance;  }   public voID prepareAn@R_404_6901@() {  isPrepare = false;  file dir = new file(mDir);  if (!dir.exists()) {   dir.mkdirs();  }  String filename = genefilename();   file file = new file(dir,filename);  mCurrentfilepath = file.getabsolutePath();  mMediaRecorder = new MediaRecorder();  // 设置输出文件  mMediaRecorder.setoutputfile(file.getabsolutePath());  // 音频源头  mMediaRecorder.setAu@R_404_6901@Source(MediaRecorder.Au@R_404_6901@Source.MIC);  // 设置音频格式  mMediaRecorder.setoutputFormat(MediaRecorder.OutputFormat.RAW_AMR);  // 设置音频编码  mMediaRecorder.setAu@R_404_6901@Encoder(MediaRecorder.Au@R_404_6901@Encoder.AMR_NB);  try {   mMediaRecorder.prepare();   mMediaRecorder.start();   isPrepare = true;   if (mAu@R_404_6901@StateListener != null) {   mAu@R_404_6901@StateListener.wellPrepared();    }  } catch (Exception e) {   // Todo auto-generated catch block   e.printstacktrace();  }  }   private String genefilename() {  return UUID.randomUUID().toString() + ".amr";  }   public int getVoiceLevel(int maxLevel) {  if (isPrepare) {   try {   // mMediaRecorder.getMaxAmplitude() 1~32767   // return maxlevel*mMediaRecorder.getMaxAmplitude()/32768+1;   return maxLevel * new MediaRecorder().getMaxAmplitude() / 32768    + 1;   } catch (Exception e) {   }   }  return 1;  }   public voID release() {  mMediaRecorder.release();  mMediaRecorder = null;  }   public voID cancel() {  release();  if (mCurrentfilepath != null) {   file file = new file(mCurrentfilepath);   file.delete();   mCurrentfilepath = null;   }  }   public String getCurrentfilePath() {  // Todo auto-generated method stub  return mCurrentfilepath;  }  } 

MediaManager

package com.example.recorder_vIEw;  import java.io.IOException;  import androID.media.Au@R_404_6901@Manager; import androID.media.MediaPlayer; import androID.media.MediaPlayer.OnCompletionListener; import androID.media.MediaPlayer.OnErrorListener;  public class MediaManager {   private static MediaPlayer mediaPlayer;  private static boolean isPause;  public static voID playSound(String filePath,OnCompletionListener onCompletionListener) {  if (mediaPlayer == null) {   mediaPlayer = new MediaPlayer();   mediaPlayer.setonErrorListener(new OnErrorListener() {      @OverrIDe   public boolean onError(MediaPlayer mp,int what,int extra) {    mediaPlayer.reset();    return false;   }   });  }else {   mediaPlayer.reset();  }  try {   mediaPlayer.setAu@R_404_6901@StreamType(Au@R_404_6901@Manager.STREAM_MUSIC);   mediaPlayer.setonCompletionListener(onCompletionListener);   mediaPlayer.setDataSource(filePath);   mediaPlayer.prepare();  } catch (Exception e) {   e.printstacktrace();  }  mediaPlayer.start();  }    public static voID pause(){  if (mediaPlayer!=null && mediaPlayer.isPlaying()) {   mediaPlayer.pause();   isPause = true;  }  }  public static voID resume(){  if (mediaPlayer!=null && isPause) {   mediaPlayer.start();   isPause = false;  }  }  public static voID release(){  if (mediaPlayer!=null) {   mediaPlayer.release();   mediaPlayer = null;    }  } } 

本文已被整理到了《Android微信开发教程汇总》,欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android自定义UI实现微信语音全部内容,希望文章能够帮你解决Android自定义UI实现微信语音所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1141918.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存