This article has shared with you how to obtain different paths in Java for your reference. The specific content is as follows
Ideas:
Custom Button
Get DialogManager, AudioManager setOnLongClickListener long press event-do the audio recording work of AudioManager
(this) Implement the interface callback method for recording preparation. Sending the MSG_AUDIO_PREPARE message in the method means that the recording preparation work has been completed
Receive the message in mHandler, start the thread recording, and count the timing. During the timing process, send the MSG_VOICE_CHANGED message, go to updateVoiceLeve to update the volume icon, and in the MotionEvent.ACTION_UP event, define the interface method. The method is the callback method after recording. In MainActivity, implement the callback method after recording to update the listViewUi interface after recording.
Click the listViewItem entry to play the animation and play the audio
activity_main.xml
<LinearLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".imooc_recorder.MainActivity" > <ListView android: 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" /> <.recorder_view.AudioRecorderButton android: 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" > </.recorder_view.AudioRecorderButton> </FrameLayout> </LinearLayout>
dialog_recorder.xml---dialog layout file
<LinearLayout xmlns:andro xmlns:tools="/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: android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/recorder" android:visibility="visible" /> <ImageView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/v1" android:visibility="visible" /> </LinearLayout> <TextView android: android:layout_marginTop="5dp" android:text="Swipe your fingers, cancel sending" android:textColor="#ffffff" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
item_recorder.xml--every item in listview
<RelativeLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:background="#eeeeee" android:layout_height="60dp" android:layout_marginTop="5dp" > <ImageView android: 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: 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: android:layout_width="25dp" android:layout_height="25dp" android:layout_gravity="center_vertical|right" android:background="@drawable/adj" /> </FrameLayout> <TextView android: 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>
<style name="Theme_AudioDialog" parent="@android:"> <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:andro 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:andro 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:andro > <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 .imooc_recorder; import ; import ; import .recorder_view.AudioRecorderButton; import .recorder_view.; import .recorder_view.MediaManager; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends Activity { private ListView mListView; private ArrayAdapter<Recorder> mAdapter; private List<Recorder> mDatas = new ArrayList<>(); private AudioRecorderButton mAudioRecorderButton; private View animView; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); mListView = (ListView) findViewById(.id_listview); mAudioRecorderButton = (AudioRecorderButton) findViewById(.id_recorder_button); //The recording is finished, callback (new AudioFinishRecorderListener() { @Override public void onFinish(float seconds, String filePath) { // TODO Auto-generated method stub Recorder recorder = new Recorder(seconds, filePath); (recorder); (); (()-1); } }); mAdapter = new RecorderAdapter(this, mDatas); (mAdapter); (new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (animView !=null) { (); animView = null; } //Play animation animView = (.id_recorder_anim); (.play_anim); AnimationDrawable anim = (AnimationDrawable) (); (); //Play audio ((position).filePath,new () { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub (); } }); } }); } @Override protected void onPause() { // TODO Auto-generated method stub (); (); } @Override protected void onResume() { // TODO Auto-generated method stub (); (); } @Override protected void onDestroy() { // TODO Auto-generated method stub (); (); } /**Internal Class*/ class Recorder{ float time; String filePath; public Recorder(float time, String filePath) { super(); = time; = filePath; } public float getTime() { return time; } public void setTime(float time) { = time; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { = filePath; } } }
RecorderAdapter
package .imooc_recorder; import ; import .imooc_recorder.; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; 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(); ().getMetrics(outMetrics); mMaxItemWidth = (int) ( * 0.7f); mMinItemWidth = (int) ( * 0.15f); mInflater = (context); } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder = null; if (convertView == null) { convertView = (.item_recorder, parent, false); holder = new ViewHolder(); = (TextView) (.id_recorder_time); = (.id_recorder_length); (holder); }else { holder = (ViewHolder) (); } ((getItem(position).time)+"\""); lp = (); = (int) (mMinItemWidth +(mMaxItemWidth/60f*getItem(position).time)); return convertView; } private class ViewHolder { private TextView seconds; private View length; } }
AudioRecorderButton
package .recorder_view; import .imooc_recorder.R; import .recorder_view.; import ; import ; import ; import ; import ; import ; import ; import ; public class AudioRecorderButton extends Button implements AudioStateListener { private static final int DISTANCE_Y_CANCEL = 50; /** Prompt box management tool class */ private DialogManager mDialogManager; /** Normal, release your finger to cancel sending, slide your finger to cancel sending */ private static final int STATE_NORMAL = 1; private static final int STATE_RECORDING = 2; private static final int STATE_WANT_TO_CANCEL = 3; /** Initial status: Normal */ private int mCurState = STATE_NORMAL; /** Is the recording currently being recorded */ private boolean isRecording = false; /** Recording Tools */ private AudioManager mAudioManager; /** Total recording time */ private float mTime; /** Whether to trigger button */ private boolean mReady; /***************************************************************************************************** */ public AudioRecorderButton(Context context) { super(context); } public AudioRecorderButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public AudioRecorderButton(Context context, AttributeSet attrs) { super(context, attrs); mDialogManager = new DialogManager(getContext()); String dir = () + "/imooc_recorder_audios"; mAudioManager = (dir); (this); /** Listen to callback */ setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { mReady = true; // Recording preparation--After preparing, there is an interface method to implement (); return false; } }); } /****************************************************************************************** * Callback function to complete recording preparation */ @Override public void wellPrepared() { // Prepare to send a message (MSG_AUDIO_PREPARE); } private Handler mHandler = new Handler() { public void handleMessage( msg) { switch () { case MSG_AUDIO_PREPARE: (); isRecording = true; new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (isRecording) { try { (100); // Recording time mTime += 0.1f; (MSG_VOICE_CHANGED); } catch (InterruptedException e) { (); } } } }).start(); break; case MSG_VOICE_CHANGED: ((7)); break; case MSG_DIALOG_DIMISS: (); break; default: break; } (msg); }; }; private static final int MSG_AUDIO_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 = (); int x = (int) (); int y = (int) (); switch (action) { case MotionEvent.ACTION_DOWN: changeState(STATE_RECORDING); break; case MotionEvent.ACTION_MOVE: if (isRecording) { // According to xy coordinates. Determine whether you want to cancel if (wantToCancel(x, y)) { changeState(STATE_WANT_TO_CANCEL); } else { changeState(STATE_RECORDING); } } break; case MotionEvent.ACTION_UP: if (!mReady) { // Onclick is not triggered reset(); return (event); } if (!isRecording || mTime < 0.6f) { // prapred has not been completed and has been uploaded (); (); (MSG_DIALOG_DIMISS, 1300); } else if (mCurState == STATE_RECORDING) { (); (); if (audioFinishRecorderListener != null) { (mTime, ()); } } else if (mCurState == STATE_WANT_TO_CANCEL) { (); (); } // Restore flag bit reset(); break; default: break; } return (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 means that it is outside the range of the button 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(.button_recorder_normal); setText(.str_recorder_nomal); break; case STATE_RECORDING: setBackgroundResource(.button_recorder_recording); setText(.str_recorder_recording); if (isRecording) { // to do (); } break; case STATE_WANT_TO_CANCEL: setBackgroundResource(.button_recorder_recording); setText(.str_recorder_want_cancel); (); break; default: break; } } } /** Complete recording callback */ public interface AudioFinishRecorderListener { void onFinish(float seconds, String filePath); } private AudioFinishRecorderListener audioFinishRecorderListener; public void setAudioFinishRecorderListener( AudioFinishRecorderListener audioFinishRecorderListener) { = audioFinishRecorderListener; } }
DialogManager
package .recorder_view; import .imooc_recorder.R; import ; import ; import ; import ; import ; import ; public class DialogManager { private Dialog mDialog; private ImageView mIcon; private ImageView mVoice; private TextView mLabel; private Context mContext; public DialogManager(Context context) { = context; } public void showRecordingDailog() { mDialog = new Dialog(mContext, .Theme_AudioDialog); LayoutInflater inflater = (mContext); View view = (.dialog_recorder, null); (view); mIcon = (ImageView) (.id_recorder_dialog_icon); mVoice = (ImageView) mDialog .findViewById(.id_recorder_dialog_voice); mLabel = (TextView) (.id_recorder_dialog_label); (); } public void recording() { if (mDialog != null && ()) { (); (); (); (); ("Swipe your fingers, cancel sending"); } } public void wantToCancle() { if (mDialog != null && ()) { (); (); (); (); ("Relax your finger and cancel sending"); } } public void tooShort() { if (mDialog != null && ()) { (); (); (); (.voice_to_short); ("The recording time is too short"); } } public void dimissDailog() { if (mDialog != null && ()) { (); mDialog = null; } } public void updateVoiceLeve(int level) { if (mDialog != null && ()) { // (); // (); // (); // Find the resource through the method name int resId = ().getIdentifier("v" + level, "drawable", ()); (resId); } } }
AudioManager
package .recorder_view; import ; import ; import ; import ; import ; import ; public class AudioManager { private MediaRecorder mMediaRecorder; private String mDir; private String mCurrentFilepath; private static AudioManager mInstance; private boolean isPrepare; /** Interface callback */ public interface AudioStateListener { void wellPrepared(); } public AudioStateListener mAudioStateListener; public void setOnAudioStateListener(AudioStateListener audioStateListener) { mAudioStateListener = audioStateListener; } private AudioManager(String dir) { = dir; } public static AudioManager getInstance(String dir) { if (mInstance == null) { synchronized () { if (mInstance == null) { mInstance = new AudioManager(dir); } } } return mInstance; } public void prepareAndio() { isPrepare = false; File dir = new File(mDir); if (!()) { (); } String fileName = geneFileName(); File file = new File(dir, fileName); mCurrentFilepath = (); mMediaRecorder = new MediaRecorder(); // Set the output file (()); // Audio source (); // Set audio format (.RAW_AMR); // Set audio encoding (.AMR_NB); try { (); (); isPrepare = true; if (mAudioStateListener != null) { (); } } catch (Exception e) { // TODO Auto-generated catch block (); } } private String geneFileName() { return ().toString() + ".amr"; } public int getVoiceLevel(int maxLevel) { if (isPrepare) { try { // () 1~32767 // return maxlevel*()/32768+1; return maxLevel * new MediaRecorder().getMaxAmplitude() / 32768 + 1; } catch (Exception e) { } } return 1; } public void release() { (); mMediaRecorder = null; } public void cancel() { release(); if (mCurrentFilepath != null) { File file = new File(mCurrentFilepath); (); mCurrentFilepath = null; } } public String getCurrentFilePath() { // TODO Auto-generated method stub return mCurrentFilepath; } }
MediaManager
package .recorder_view; import ; import ; import ; import ; import ; 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(); (new OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { (); return false; } }); }else { (); } try { (AudioManager.STREAM_MUSIC); (onCompletionListener); (filePath); (); } catch (Exception e) { (); } (); } public static void pause(){ if (mediaPlayer!=null && ()) { (); isPause = true; } } public static void resume(){ if (mediaPlayer!=null && isPause) { (); isPause = false; } } public static void release(){ if (mediaPlayer!=null) { (); mediaPlayer = null; } } }
This article has been compiled intoAndroid WeChat development tutorial summary》, everyone is welcome to learn and read.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.