实例介绍
【实例截图】
【核心代码】
package com.kingpeng.main;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Chronometer.OnChronometerTickListener;
import com.kingpeng.util.SoundMeter;
public class CustomSoundActivity extends Activity{
private Button bntRecord;
private int flag=1;
private View rcChat_popup;
private Handler mHandler = new Handler();
private long startVoiceT, endVoiceT;
private String voiceName;
private SoundMeter mSensor;
private static final int POLL_INTERVAL = 300;
private ImageView volume;
private LinearLayout voice_rcd_hint_rcding,sound_file;
private TextView txtName;//显示声音文件
private ImageButton use_bnt; //是否使用声音文件按钮
public static boolean isUsed=false;//是否使用自定义声音文件
private Chronometer timedown;//显示倒计时
//保存使用状态的文件
private SharedPreferences sp;
private long timeTotalInS = 0;
private long timeLeftInS = 0;
private MediaPlayer player ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_sound);
bntRecord=(Button)findViewById(R.id.bntRecord);
rcChat_popup = this.findViewById(R.id.rcChat_popup);
volume = (ImageView) this.findViewById(R.id.volume);
voice_rcd_hint_rcding = (LinearLayout)this.findViewById(R.id.voice_rcd_hint_rcding);
sound_file=(LinearLayout)this.findViewById(R.id.sound_file);
txtName=(TextView)findViewById(R.id.show_sound);
timedown=(Chronometer)findViewById(R.id.timedown);
use_bnt=(ImageButton)findViewById(R.id.used_bnt);
voiceName="MySound.mp3";
mSensor = new SoundMeter();
//触摸录音按钮触发事件
bntRecord.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_DOWN){
bntRecord.setBackgroundColor(getResources().getColor(R.color.bule));
bntRecord.setText("松开 结束");
bntRecord.setTextColor(getResources().getColor(R.color.white));
int[] location = new int[2];
bntRecord.getLocationInWindow(location); // 获取在当前窗口内的绝对坐标
int btn_rc_Y = location[1];
int btn_rc_X = location[0];
if(flag==1){
if (!Environment.getExternalStorageDirectory().exists()) {
Toast.makeText(CustomSoundActivity.this, "No SDCard", Toast.LENGTH_LONG).show();
return false;
}
System.out.println("2");
System.out.println(event.getY() "..." btn_rc_Y "...." event.getX() "...." btn_rc_X);
if (event.getY() < btn_rc_Y && event.getX() > btn_rc_X) {//判断手势按下的位置是否是语音录制按钮的范围内
System.out.println("3");
rcChat_popup.setVisibility(View.VISIBLE);
mHandler.postDelayed(new Runnable() {
public void run() {
}
}, 300);
startVoiceT = SystemClock.currentThreadTimeMillis();
// voiceName = startVoiceT ".amr";
start(voiceName);
//设置录音时间
timedown.setVisibility(View.VISIBLE);
initTimer(5);
timedown.start();
flag = 2;
}
}
}else if(event.getAction()==MotionEvent.ACTION_UP){
bntRecord.setBackgroundColor(getResources().getColor(R.color.white));
bntRecord.setText("按住 说话");
bntRecord.setTextColor(Color.BLACK);
timedown.stop();
if(flag==2){
rcChat_popup.setVisibility(View.GONE);
timedown.setVisibility(View.GONE);
stop();
flag = 1;
soundUse(voiceName);
// File file = new File(android.os.Environment.getExternalStorageDirectory() "/hq_100/" voiceName);
// if (file.exists()) {
// file.delete();
// }
}else {
voice_rcd_hint_rcding.setVisibility(View.GONE);
stop();
endVoiceT = SystemClock.currentThreadTimeMillis();
flag = 1;
int time = (int) ((endVoiceT - startVoiceT) / 1000);
System.out.println(time);
}
}
return false;
}
});
soundUse(voiceName);
}
private void soundUse(String fileName){
//判断sd卡上是否有声音文件,有的话就显示名称并播放
final String path=Environment.getExternalStorageDirectory().getAbsolutePath() "/hq_100/" voiceName;
File file=new File(path);
if(file.exists()){
sound_file.setVisibility(View.VISIBLE);
String soundName=file.getName();
txtName.setText(soundName);
//点击声音文件播放声音
sound_file.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
sound_file.setBackgroundColor(getResources().getColor(R.color.bule));
player= new MediaPlayer();
try {
player.setDataSource(path);
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(event.getAction()==MotionEvent.ACTION_UP){
sound_file.setBackgroundColor(getResources().getColor(R.color.white));
}
return true;
}
});
//获取是按钮使用状态,初始化按钮文本
sp=getSharedPreferences("customSound", Activity.MODE_PRIVATE);
isUsed=sp.getBoolean("ISUSED", false);
if(isUsed){
use_bnt.setBackgroundResource(R.drawable.on);
}else{
use_bnt.setBackgroundResource(R.drawable.off);
}
//点击使用按钮替换车找到的后声音提示
use_bnt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view){
// TODO Auto-generated method stub
if(isUsed==false){
isUsed=true;
sp=getSharedPreferences("customSound", Activity.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putBoolean("ISUSED", true);
editor.commit();
use_bnt.setBackgroundResource(R.drawable.on);
}else{
isUsed=false;
sp.edit().putBoolean("ISUSED", false).commit();
use_bnt.setBackgroundResource(R.drawable.off);
}
}
});
}
}
private Runnable mSleepTask = new Runnable() {
public void run() {
stop();
}
};
private Runnable mPollTask = new Runnable(){
public void run() {
double amp = mSensor.getAmplitude();
updateDisplay(amp);
mHandler.postDelayed(mPollTask, POLL_INTERVAL);
}
};
private void start(String name) {
mSensor.start(name);
mHandler.postDelayed(mPollTask, POLL_INTERVAL);
}
private void stop() {
mHandler.removeCallbacks(mSleepTask);
mHandler.removeCallbacks(mPollTask);
mSensor.stop();
volume.setImageResource(R.drawable.amp1);
}
private void updateDisplay(double signalEMA) {
switch ((int) signalEMA) {
case 0:
case 1:
volume.setImageResource(R.drawable.amp1);
break;
case 2:
case 3:
volume.setImageResource(R.drawable.amp2);
break;
case 4:
case 5:
volume.setImageResource(R.drawable.amp3);
break;
case 6:
case 7:
volume.setImageResource(R.drawable.amp4);
break;
case 8:
case 9:
volume.setImageResource(R.drawable.amp5);
break;
case 10:
case 11:
volume.setImageResource(R.drawable.amp6);
break;
default:
volume.setImageResource(R.drawable.amp7);
break;
}
}
/**
* 初始化计时器,计时器是通过widget.Chronometer来实现的
* @param total 一共多少秒
*/
private void initTimer(long total) {
this.timeTotalInS = total;
this.timeLeftInS = total;
timedown.setOnChronometerTickListener(new OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
if (timeLeftInS <= 0) {
Toast.makeText(CustomSoundActivity.this, "录音时间到", Toast.LENGTH_SHORT).show();
timedown.stop();
//录音停止
stop();
rcChat_popup.setVisibility(View.GONE);
timedown.setVisibility(View.GONE);
return;
}
timeLeftInS--;
refreshTimeLeft();
}
});
}
private void refreshTimeLeft() {
this.timedown.setText("录音时间剩余:" timeLeftInS);
//TODO 格式化字符串
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(player!=null){
player.stop();
player.release();
}
}
}
标签: 录音
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论