-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #917 from baijihui416/master
- Loading branch information
Showing
35 changed files
with
910 additions
and
13 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901101/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="edu.hzuapps.androidworks.homework.com1314080901101"> | ||
|
||
<uses-permission android:name="android.permission.RECORD_AUDIO" /> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@drawable/ic_launcher" | ||
android:label="@string/app_name"> | ||
<activity | ||
android:name="edu.hzuapps.androidworks.homework.com1314080901101.MainActivity" | ||
android:label="@string/app_name"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
116 changes: 116 additions & 0 deletions
116
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901101/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package edu.hzuapps.androidworks.homework.com1314080901101; | ||
|
||
import android.app.Activity; | ||
import android.graphics.drawable.AnimationDrawable; | ||
import android.media.MediaPlayer; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.AdapterView; | ||
import android.widget.ArrayAdapter; | ||
import android.widget.ListView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import edu.hzuapps.androidworks.homework.com1314080901101.view.AudioRecorderButton; | ||
import edu.hzuapps.androidworks.homework.com1314080901101.view.MediaManager; | ||
|
||
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) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.com1314080901101_activity_main); | ||
|
||
mListView = (ListView) findViewById(R.id.lv); | ||
mAudioRecorderButton = (AudioRecorderButton) findViewById(R.id.recorder_button); | ||
mAudioRecorderButton.setAudioFinishRecorderListener(new AudioRecorderButton.AudioFinishRecorderListener() { | ||
@Override | ||
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 RecorderAdapter(this, mDatas); | ||
mListView.setAdapter(mAdapter); | ||
|
||
//listView的item点击事件 | ||
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | ||
@Override | ||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | ||
if (animView != null) { | ||
animView.setBackgroundResource(R.drawable.com1314080901101_adj); | ||
animView = null; | ||
} | ||
//播放动画 | ||
animView = view.findViewById(R.id.recorder_anim); | ||
animView.setBackgroundResource(R.drawable.com1314080901101_play_anim); | ||
AnimationDrawable anim = (AnimationDrawable) animView.getBackground(); | ||
anim.start(); | ||
//播放音频 | ||
MediaManager.playSound(mDatas.get(position).filePath, new MediaPlayer.OnCompletionListener() { | ||
|
||
@Override | ||
public void onCompletion(MediaPlayer mp) { | ||
animView.setBackgroundResource(R.drawable.com1314080901101_adj); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected void onPause() { | ||
super.onPause(); | ||
MediaManager.pause(); | ||
} | ||
|
||
@Override | ||
protected void onResume() { | ||
super.onResume(); | ||
MediaManager.resume(); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
MediaManager.release(); | ||
} | ||
|
||
class Recorder { | ||
float time; | ||
String filePath; | ||
|
||
public Recorder(float time, String filePath) { | ||
super(); | ||
this.time = time; | ||
this.filePath = filePath; | ||
} | ||
|
||
public void setTime(float time) { | ||
this.time = time; | ||
} | ||
|
||
public void setFilePath(String filePath) { | ||
this.filePath = filePath; | ||
} | ||
|
||
public float getTime() { | ||
|
||
return time; | ||
} | ||
|
||
public String getFilePath() { | ||
return filePath; | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901101/RecorderAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package edu.hzuapps.androidworks.homework.com1314080901101; | ||
|
||
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; | ||
|
||
import java.util.List; | ||
|
||
import edu.hzuapps.androidworks.homework.com1314080901101.MainActivity.Recorder; | ||
|
||
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(mContext); | ||
} | ||
|
||
|
||
@Override | ||
public View getView(int position, View convertView, ViewGroup parent) { | ||
ViewHolder holder; | ||
if (convertView == null) { | ||
convertView = mInflater.inflate(R.layout.com1314080901101_item_recorder, parent, false); | ||
holder = new ViewHolder(); | ||
holder.seconds = (TextView) convertView.findViewById(R.id.recorder_time); | ||
holder.length = convertView.findViewById(R.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 { | ||
TextView seconds; //时间 | ||
View length; //长度 | ||
} | ||
} |
13 changes: 0 additions & 13 deletions
13
...in/java/edu/hzuapps/androidworks/homeworks/com1314080901101/com1314080901101Activity.java
This file was deleted.
Oops, something went wrong.
140 changes: 140 additions & 0 deletions
140
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901101/view/AudioManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package edu.hzuapps.androidworks.homework.com1314080901101.view; | ||
|
||
import android.media.MediaRecorder; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
public class AudioManager { | ||
private MediaRecorder mMediaRecorder; | ||
private String mDir; | ||
private String mCurrentFilePath; | ||
private boolean isPrepared; | ||
|
||
private static AudioManager mInstance; | ||
|
||
public static AudioManager getmInstance(String dir) { | ||
if (mInstance == null) { | ||
synchronized (AudioManager.class) { | ||
if (mInstance == null) { | ||
mInstance = new AudioManager(dir); | ||
} | ||
} | ||
} | ||
return mInstance; | ||
} | ||
|
||
public AudioManager(String dir) { | ||
this.mDir = dir; | ||
} | ||
|
||
public AudioStateListener mListener; | ||
|
||
/** | ||
* 回调准备完毕 | ||
*/ | ||
public interface AudioStateListener { | ||
void wellPrepared(); | ||
} | ||
|
||
public void setOnAudioStateListener(AudioStateListener listener) { | ||
mListener = listener; | ||
} | ||
|
||
/** | ||
* 准备 | ||
*/ | ||
public void prepareAudio() { | ||
|
||
try { | ||
isPrepared = false; | ||
|
||
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(file.getAbsolutePath()); | ||
//设置MediaRecorder的音频源为麦克风 | ||
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); | ||
//设置音频的格式 | ||
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); | ||
//设置音频的编码为amr | ||
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); | ||
//准备录音 | ||
mMediaRecorder.prepare(); | ||
//开始录音 | ||
mMediaRecorder.start(); | ||
//准备结束 | ||
isPrepared = true; | ||
if (mListener != null) { | ||
mListener.wellPrepared(); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* 随机生成文件的名称 | ||
* | ||
* @return | ||
*/ | ||
private String generateFileName() { | ||
return UUID.randomUUID().toString() + ".amr"; | ||
} | ||
|
||
/** | ||
* 获得音量等级 | ||
* | ||
* @return | ||
*/ | ||
public int getVoiceLevel(int maxLevel) { | ||
if (isPrepared) { | ||
try { | ||
//mMediaRecorder.getMaxAmplitude() 范围:1-32767之间 | ||
return maxLevel * mMediaRecorder.getMaxAmplitude() / 32768 + 1; | ||
} catch (Exception e) { | ||
|
||
} | ||
} | ||
return 1; | ||
} | ||
|
||
/** | ||
* 释放 | ||
*/ | ||
public void release() { | ||
// mMediaRecorder.stop(); | ||
mMediaRecorder.release(); | ||
mMediaRecorder = null; | ||
} | ||
|
||
/** | ||
* 取消 | ||
*/ | ||
public void cancel() { | ||
release(); | ||
//删除文件 | ||
if (mCurrentFilePath != null) { | ||
File file = new File(mCurrentFilePath); | ||
file.delete(); | ||
mCurrentFilePath = null; | ||
} | ||
} | ||
|
||
/** | ||
* 获取当前文件存储路径 | ||
* | ||
* @return | ||
*/ | ||
public String getCurrentFilePath() { | ||
return mCurrentFilePath; | ||
} | ||
} |
Oops, something went wrong.