}
private boolean wantCancel(int x, int y) {
if (x < 0 || x > getWidth()) {
return true;
}
// 零点在左下角?
if (y < -DISTANCE_CANCEL_Y || y > getHeight() + DISTANCE_CANCEL_Y) {
return true;
}
return false;
}
private void changeState(int state) {
if (currentState != state) {
currentState = state;
switch (state) {
case STATE_NORMAL:
setBackgroundResource(R.drawable.btn_recorder_normal);
setText(R.string.btn_recorder_normal);
break;
case STATE_RECORDING:
setBackgroundResource(R.drawable.btn_recorder_normal);
setText(R.string.btn_recorder_recording);
if (isRecording) {
dialogManager.stateRecording();
}
break;
case STATE_WANT_CANCEL:
setBackgroundResource(R.drawable.btn_recorder_normal);
setText(R.string.btn_recorder_want_cancel);
dialogManager.stateWantCancel();
break;
default:
break;
}
}
}
}
自定义Dialog
package com.zms.wechatrecorder.view;
import com.zms.wechatrecorder.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 AudioRecordDialog {
private Dialog dialog;
private ImageView imageRecord, imageVolume;
private TextView textHint;
private Context context;
public AudioRecordDialog(Context context) {
this.context = context;
}
public void showDialog() {
dialog = new Dialog(context, R.style.Theme_RecorderDialog);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.dialog, null);
dialog.setContentView(view);
imageRecord = (ImageView) dialog.findViewById(R.id.imageRecord);
imageVolume = (ImageView) dialog.findViewById(R.id.imageVolume);
textHint = (TextView) dialog.findViewById(R.id.textHint);
dialog.show();
}
public void stateRecording() {
if (dialog != null && dialog.isShowing()) {
imageRecord.setVisibility(View.VISIBLE);
imageVolume.setVisibility(View.VISIBLE);
textHint.setVisibility(View.VISIBLE);
imageRecord.setImageResource(R.drawable.icon_dialog_recording);
textHint.setText(“手指上滑,取消发送”);
}
}
public void stateWantCancel() {
if (dialog != null && dialog.isShowing()) {
imageRecord.setVisibility(View.VISIBLE);
imageRecord.setImageResource(R.drawable.icon_dialog_cancel);
imageVolume.setVisibility(View.GONE);
textHint.setVisibility(View.VISIBLE);
textHint.setText(“松开手指,取消发送”);
}
}
public void stateLengthShort() {
if (dialog != null && dialog.isShowing()) {
imageRecord.setVisibility(View.VISIBLE);
imageRecord.setImageResource(R.drawable.icon_dialog_length_short);
imageVolume.setVisibility(View.GONE);
textHint.setVisibility(View.VISIBLE);
textHint.setText(“录音时间过短”);
}
}
public void dismissDialog() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
dialog = null;
}
}
/**
-
更新音量
-
@param level
*/
public void updateVolumeLevel(int level) {
if (dialog != null && dialog.isShowing()) {
// imageRecord.setVisibility(View.VISIBLE);
// imageVolume.setVisibility(View.VISIBLE);
// textHint.setVisibility(View.VISIBLE);
int volumeResId = context.getResources().getIdentifier(
“icon_volume_” + level, “drawable”,
context.getPackageName());
imageVolume.setImageResource(volumeResId);
}
}
}
VoiceListAdapter:
package com.zms.wechatrecorder;
import java.util.List;
import com.zms.wechatrecorder.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.TextView;
public class VoiceListAdapter extends ArrayAdapter {
private List mDatas;
private Context context;
private int minItemWidth;
private int maxItemWidth;
private LayoutInflater inflater;
public VoiceListAdapter(Context context, List datas) {
super(context, -1, datas);
this.context = context;
mDatas = datas;
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
maxItemWidth = (int) (outMetrics.widthPixels * 0.8);
maxItemWidth = (int) (outMetrics.widthPixels * 0.2);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_item_voice, parent,
false);
holder = new ViewHolder();
holder.seconds = (TextView) convertView
.findViewById(R.id.textLength);
holder.length = convertView.findViewById(R.id.voiceAnim);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.seconds.setText(Math.round(getItem(position).audioLength) + “”");
// ViewGroup.LayoutParams params = holder.length.getLayoutParams();
// params.width = (int) (minItemWidth + maxItemWidth / 60f
// * getItem(position).audioLength);
// holder.length.setLayoutParams(params);
return convertView;
}
private class ViewHolder {
TextView seconds;
View length;
}
}
MyAudioManager:
package com.zms.wechatrecorder;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import android.media.MediaRecorder;
public class MyAudioManager {
private MediaRecorder mediaRecorder;
private String dir;
private String currentFilePath;
private static MyAudioManager audioInstance; // 单例
public boolean isPrepared = false;
private MyAudioManager(String dir) {
this.dir = dir;
}
public interface AudioStateChangeListener {
void wellPrepared();
}
public AudioStateChangeListener audioStateChangeListener;
public void setOnAudioStateChangeListener(AudioStateChangeListener listener) {
audioStateChangeListener = listener;
}
public static MyAudioManager getInstance(String dir) {
if (audioInstance == null) {
synchronized (MyAudioManager.class) {
if (audioInstance == null) {
audioInstance = new MyAudioManager(dir);
}
}
}
return audioInstance;
}
public void prepareAudio() {
try {
isPrepared = false;
File fileDir = new File(dir);
if (!fileDir.exists())
fileDir.mkdirs();
String fileName = generateFileName();
File file = new File(fileDir, fileName);
currentFilePath = file.getAbsolutePath();
mediaRecorder = new MediaRecorder();
// 设置输出文件
mediaRecorder.setOutputFile(file.getAbsolutePath());
// 设置音频源
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 设置音频格式
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
// 设置音频编码
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.prepare();
mediaRecorder.start();
// 准备结束
isPrepared = true;
//
if (audioStateChangeListener != null) {
audioStateChangeListener.wellPrepared();
}
} catch (IllegalStateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
-
随机生成文件名称
-
@return
*/
private String generateFileName() {
return UUID.randomUUID().toString() + “.amr”;
}
public int getVoiceLevel(int maxLevel) {
if (isPrepared) {
try {
// 振幅范围mediaRecorder.getMaxAmplitude():1-32767
return maxLevel * mediaRecorder.getMaxAmplitude() / 32768 + 1;
} catch (Exception e) {
}
}
return 1;
}
public void release() {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
}
public void cancel() {
release();
if (currentFilePath != null) {
File file = new File(currentFilePath);
file.delete();
currentFilePath = null;
}
}
public String getCurrentPath() {
return currentFilePath;
}
}
MediaManager
package com.zms.wechatrecorder;
import java.io.IOException;
import android.media.AudioManager;
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.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(onCompletionListener);
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalArgumentException | SecurityException
| IllegalStateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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;
}
}
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
![](https://i-blog.csdnimg.cn/blog_migrate/8b6f1812c31fc8125e43b98518592b8f.jpeg)
最后我还整理了很多Android中高级的PDF技术文档。以及一些大厂面试真题解析文档。
Android高级架构师之路很漫长,一起共勉吧!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-LTmjztLd-1712515331230)]
[外链图片转存中…(img-ABn6P1wa-1712515331231)]
[外链图片转存中…(img-SIxgl0JA-1712515331231)]
[外链图片转存中…(img-vJakDTuR-1712515331232)]
[外链图片转存中…(img-7lweIb4I-1712515331232)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
![](https://i-blog.csdnimg.cn/blog_migrate/8b6f1812c31fc8125e43b98518592b8f.jpeg)
最后我还整理了很多Android中高级的PDF技术文档。以及一些大厂面试真题解析文档。
[外链图片转存中…(img-Ln1If8jo-1712515331233)]
Android高级架构师之路很漫长,一起共勉吧!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!